Skip to content

Commit f478fb3

Browse files
committed
Copied optimization to MimeParser as well
1 parent 79dd8dd commit f478fb3

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

MimeKit/MimeParser.cs

+25-14
Original file line numberDiff line numberDiff line change
@@ -1315,28 +1315,39 @@ unsafe void ScanContent (byte* inbuf, ref int nleft, ref bool midline, ref bool[
13151315
*inend = (byte) '\n';
13161316

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

1325-
*aligned = (byte) '\n';
1326-
while (*inptr != (byte) '\n')
1320+
// Note: we can always depend on byte[] arrays being 4-byte aligned on 32bit and 64bit architectures
1321+
// so we can safely use the startIndex instead of `((long) inptr) & 3` to determine the alignment.
1322+
switch (startIndex & 3) {
1323+
case 1:
1324+
if (*inptr == (byte) '\n')
1325+
break;
13271326
inptr++;
1328-
*aligned = c;
1327+
goto case 2;
1328+
case 2:
1329+
if (*inptr == (byte) '\n')
1330+
break;
1331+
inptr++;
1332+
goto case 3;
1333+
case 3:
1334+
if (*inptr != (byte) '\n')
1335+
inptr++;
1336+
break;
1337+
}
13291338

1330-
if (inptr == aligned && c != (byte) '\n') {
1339+
if (*inptr != (byte) '\n') {
13311340
// -funroll-loops, yippee ki-yay.
1332-
uint* dword = (uint*) inptr;
1333-
13341341
do {
1335-
mask = *dword++ ^ 0x0A0A0A0A;
1342+
uint mask = *((uint*) inptr) ^ 0x0A0A0A0A;
13361343
mask = ((mask - 0x01010101) & (~mask & 0x80808080));
1337-
} while (mask == 0);
13381344

1339-
inptr = (byte*) (dword - 1);
1345+
if (mask != 0)
1346+
break;
1347+
1348+
inptr += 4;
1349+
} while (true);
1350+
13401351
while (*inptr != (byte) '\n')
13411352
inptr++;
13421353
}

0 commit comments

Comments
 (0)