Skip to content

Commit 79dd8dd

Browse files
committed
Minor optimization for ScanContent's inner loop
1 parent b2f7c38 commit 79dd8dd

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

MimeKit/MimeReader.cs

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

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

1968-
*aligned = (byte) '\n';
1969-
while (*inptr != (byte) '\n')
1963+
// Note: we can always depend on byte[] arrays being 4-byte aligned on 32bit and 64bit architectures
1964+
// so we can safely use the startIndex instead of `((long) inptr) & 3` to determine the alignment.
1965+
switch (startIndex & 3) {
1966+
case 1:
1967+
if (*inptr == (byte) '\n')
1968+
break;
19701969
inptr++;
1971-
*aligned = c;
1970+
goto case 2;
1971+
case 2:
1972+
if (*inptr == (byte) '\n')
1973+
break;
1974+
inptr++;
1975+
goto case 3;
1976+
case 3:
1977+
if (*inptr != (byte) '\n')
1978+
inptr++;
1979+
break;
1980+
}
19721981

1973-
if (inptr == aligned && c != (byte) '\n') {
1982+
if (*inptr != (byte) '\n') {
19741983
// -funroll-loops, yippee ki-yay.
1975-
uint* dword = (uint*) inptr;
1976-
19771984
do {
1978-
mask = *dword++ ^ 0x0A0A0A0A;
1985+
uint mask = *((uint*) inptr) ^ 0x0A0A0A0A;
19791986
mask = ((mask - 0x01010101) & (~mask & 0x80808080));
1980-
} while (mask == 0);
19811987

1982-
inptr = (byte*) (dword - 1);
1988+
if (mask != 0)
1989+
break;
1990+
1991+
inptr += 4;
1992+
} while (true);
1993+
19831994
while (*inptr != (byte) '\n')
19841995
inptr++;
19851996
}

0 commit comments

Comments
 (0)