Skip to content

Commit

Permalink
Copied optimization to MimeParser as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Feb 1, 2025
1 parent 79dd8dd commit f478fb3
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions MimeKit/MimeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,28 +1315,39 @@ unsafe void ScanContent (byte* inbuf, ref int nleft, ref bool midline, ref bool[
*inend = (byte) '\n';

while (inptr < inend) {
// Note: we can always depend on byte[] arrays being 4-byte aligned on 32bit and 64bit architectures
int alignment = (startIndex + 3) & ~3;
byte* aligned = inbuf + alignment;
byte* start = inptr;
byte c = *aligned;
uint mask;

*aligned = (byte) '\n';
while (*inptr != (byte) '\n')
// Note: we can always depend on byte[] arrays being 4-byte aligned on 32bit and 64bit architectures
// so we can safely use the startIndex instead of `((long) inptr) & 3` to determine the alignment.
switch (startIndex & 3) {
case 1:
if (*inptr == (byte) '\n')
break;
inptr++;
*aligned = c;
goto case 2;
case 2:
if (*inptr == (byte) '\n')
break;
inptr++;
goto case 3;
case 3:
if (*inptr != (byte) '\n')
inptr++;
break;
}

if (inptr == aligned && c != (byte) '\n') {
if (*inptr != (byte) '\n') {
// -funroll-loops, yippee ki-yay.
uint* dword = (uint*) inptr;

do {
mask = *dword++ ^ 0x0A0A0A0A;
uint mask = *((uint*) inptr) ^ 0x0A0A0A0A;
mask = ((mask - 0x01010101) & (~mask & 0x80808080));
} while (mask == 0);

inptr = (byte*) (dword - 1);
if (mask != 0)
break;

inptr += 4;
} while (true);

while (*inptr != (byte) '\n')
inptr++;
}
Expand Down

0 comments on commit f478fb3

Please sign in to comment.