-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace MimeReader calls to IndexOf('\n') with new EndOfLine() method
This was an attempt at overcoming the performance degradation of using Span<byte> in place of raw pointers in MimeReader, specifically for the .NET 4.x frameworks. Unfortunately, this new custom implementation (based on the previous pointer-based optimizations I had written) is actually slightly slower than Span<byte>.indexOf('\n'). Need to figure out a better solution...
- Loading branch information
Showing
2 changed files
with
116 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// SpanExtensions.cs | ||
// | ||
// Author: Jeffrey Stedfast <[email protected]> | ||
// | ||
// Copyright (c) 2013-2025 .NET Foundation and Contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace MimeKit.Utils { | ||
static class SpanExtensions | ||
{ | ||
#if NET8_0_OR_GREATER | ||
[MethodImpl (MethodImplOptions.AggressiveInlining)] | ||
public static int EndOfLine (this Span<byte> span) | ||
{ | ||
// Note: Span<byte>.IndexOf(byte) is insanely fast in .NET >= 8.0, so use it. | ||
return span.IndexOf ((byte) '\n'); | ||
} | ||
#else | ||
[MethodImpl (MethodImplOptions.AggressiveInlining)] | ||
public unsafe static int EndOfLine (this Span<byte> span) | ||
{ | ||
fixed (byte* inbuf = &MemoryMarshal.GetReference (span)) { | ||
byte* inptr = inbuf; | ||
|
||
// scan for a linefeed character until we are 4-byte aligned. | ||
switch (((long) inptr) & 0x03) { | ||
case 1: | ||
if (*inptr == (byte) '\n') | ||
return (int) (inptr - inbuf); | ||
inptr++; | ||
goto case 2; | ||
case 2: | ||
if (*inptr == (byte) '\n') | ||
return (int) (inptr - inbuf); | ||
inptr++; | ||
goto case 3; | ||
case 3: | ||
if (*inptr == (byte) '\n') | ||
return (int) (inptr - inbuf); | ||
inptr++; | ||
break; | ||
} | ||
|
||
// -funroll-loops, yippee ki-yay. | ||
uint* dword = (uint*) inptr; | ||
uint mask; | ||
|
||
do { | ||
mask = *dword ^ 0x0A0A0A0A; | ||
mask = ((mask - 0x01010101) & (~mask & 0x80808080)); | ||
|
||
if (mask != 0) | ||
break; | ||
|
||
dword++; | ||
} while (true); | ||
|
||
inptr = (byte*) dword; | ||
int offset = (int) (inptr - inbuf); | ||
|
||
if (inptr[0] == (byte) '\n') | ||
return offset; | ||
|
||
if (inptr[1] == (byte) '\n') | ||
return offset + 1; | ||
|
||
if (inptr[2] == (byte) '\n') | ||
return offset + 2; | ||
|
||
return offset + 3; | ||
} | ||
} | ||
#endif | ||
} | ||
} |