From 79dd8dd1a6a143232c14a98b7ba1ff9d7a5daf6d Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Sat, 1 Feb 2025 14:05:43 -0500 Subject: [PATCH] Minor optimization for ScanContent's inner loop --- MimeKit/MimeReader.cs | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/MimeKit/MimeReader.cs b/MimeKit/MimeReader.cs index b63ec4d3bc..ac8b6cc4d9 100644 --- a/MimeKit/MimeReader.cs +++ b/MimeKit/MimeReader.cs @@ -1958,28 +1958,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++; }