forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Encoding to split fast-path and fallback logic (dotnet/corec…
…lr#23098) This refactoring is limited to ASCIIEncoding at the moment, but it can easily be applied to UTF-8 / UTF-16 / UTF-32. High-level changes: - Fallback logic has been split from the fast-path, improving performance of GetBytes and similar routines. - All of the plumbing of when to invoke the fallback logic and how to manage leftover data has been moved into the base class. - Almost all of the logic except for the fast-path is now written in terms of verifiable code (Span and ReadOnlySpan). - Minor bug fixes in EncoderNLS.Convert (see https://github.com/dotnet/coreclr/issues/23020). Commit migrated from dotnet/coreclr@43a5159
- Loading branch information
1 parent
8d27a06
commit 2883820
Showing
13 changed files
with
2,584 additions
and
658 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
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
1,128 changes: 520 additions & 608 deletions
1,128
src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs
Large diffs are not rendered by default.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
src/libraries/System.Private.CoreLib/src/System/Text/ASCIIUtility.cs
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,76 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Text | ||
{ | ||
/* | ||
* Contains naive unoptimized (non-SIMD) implementations of ASCII transcoding | ||
* operations. Vectorized methods can be substituted here as a drop-in replacement. | ||
*/ | ||
|
||
internal unsafe static class ASCIIUtility | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] // the actual implementation won't be inlined, so this shouldn't be either, lest it throw off benchmarks | ||
public static uint GetIndexOfFirstNonAsciiByte(byte* pBytes, uint byteCount) | ||
{ | ||
uint idx = 0; | ||
for (; idx < byteCount; idx++) | ||
{ | ||
if ((sbyte)pBytes[idx] < 0) | ||
{ | ||
break; | ||
} | ||
} | ||
return idx; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] // the actual implementation won't be inlined, so this shouldn't be either, lest it throw off benchmarks | ||
public static uint GetIndexOfFirstNonAsciiChar(char* pChars, uint charCount) | ||
{ | ||
uint idx = 0; | ||
for (; idx < charCount; idx++) | ||
{ | ||
if (pChars[idx] > 0x7Fu) | ||
{ | ||
break; | ||
} | ||
} | ||
return idx; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] // the actual implementation won't be inlined, so this shouldn't be either, lest it throw off benchmarks | ||
public static uint NarrowUtf16ToAscii(char* pChars, byte* pBytes, uint elementCount) | ||
{ | ||
uint idx = 0; | ||
for (; idx < elementCount; idx++) | ||
{ | ||
uint ch = pChars[idx]; | ||
if (ch > 0x7Fu) | ||
{ | ||
break; | ||
} | ||
pBytes[idx] = (byte)ch; | ||
} | ||
return idx; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] // the actual implementation won't be inlined, so this shouldn't be either, lest it throw off benchmarks | ||
public static uint WidenAsciiToUtf16(byte* pBytes, char* pChars, uint elementCount) | ||
{ | ||
uint idx = 0; | ||
for (; idx < elementCount; idx++) | ||
{ | ||
byte b = pBytes[idx]; | ||
if (b > 0x7F) | ||
{ | ||
break; | ||
} | ||
pChars[idx] = (char)b; | ||
} | ||
return idx; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.