-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Vectorize IA5 and Visible strings in ASN.1 encoding and decoding. #131109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
...ries/System.Formats.Asn1/src/System/Formats/Asn1/AsnCharacterStringEncodings.downlevel.cs
This file contains hidden or 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,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Formats.Asn1 | ||
| { | ||
| internal abstract class RestrictedAsciiRangeEncoding : RestrictedAsciiStringEncoding | ||
| { | ||
| protected RestrictedAsciiRangeEncoding(byte minCharAllowed, byte maxCharAllowed) | ||
| : base(minCharAllowed, maxCharAllowed) | ||
| { | ||
| } | ||
| } | ||
| } |
191 changes: 191 additions & 0 deletions
191
src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/AsnCharacterStringEncodings.net.cs
This file contains hidden or 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,191 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Numerics; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System.Formats.Asn1 | ||
| { | ||
| internal abstract class RestrictedAsciiRangeEncoding : SpanBasedEncoding | ||
| { | ||
| private readonly byte _minCharAllowed; | ||
| private readonly byte _range; | ||
|
|
||
| protected RestrictedAsciiRangeEncoding(byte minCharAllowed, byte maxCharAllowed) | ||
| { | ||
| Debug.Assert(minCharAllowed <= maxCharAllowed); | ||
| Debug.Assert(maxCharAllowed <= 0x7F); | ||
|
|
||
| _minCharAllowed = minCharAllowed; | ||
| _range = (byte)(maxCharAllowed - minCharAllowed); | ||
| } | ||
|
|
||
| public override int GetMaxByteCount(int charCount) | ||
| { | ||
| return charCount; | ||
| } | ||
|
|
||
| public override int GetMaxCharCount(int byteCount) | ||
| { | ||
| return byteCount; | ||
| } | ||
|
|
||
| protected override int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, bool write) | ||
| { | ||
| int position = 0; | ||
|
|
||
| if (chars.Length >= Vector<byte>.Count && Vector.IsHardwareAccelerated) | ||
| { | ||
| position = GetBytesVectorized(chars, bytes, write); | ||
| } | ||
|
|
||
| for (; position < chars.Length; position++) | ||
| { | ||
| char c = chars[position]; | ||
|
|
||
| if (!IsAllowed(c)) | ||
| { | ||
| EncoderFallback.CreateFallbackBuffer().Fallback(c, position); | ||
|
|
||
| Debug.Fail("Fallback should have thrown"); | ||
| throw new InvalidOperationException(); | ||
| } | ||
|
|
||
| if (write) | ||
| { | ||
| bytes[position] = (byte)c; | ||
| } | ||
| } | ||
|
|
||
| return chars.Length; | ||
| } | ||
|
|
||
| protected override int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars, bool write) | ||
| { | ||
| int position = 0; | ||
|
|
||
| if (bytes.Length >= Vector<byte>.Count && Vector.IsHardwareAccelerated) | ||
| { | ||
| position = GetCharsVectorized(bytes, chars, write); | ||
| } | ||
|
|
||
| for (; position < bytes.Length; position++) | ||
| { | ||
| byte b = bytes[position]; | ||
|
|
||
| if (!IsAllowed(b)) | ||
| { | ||
| DecoderFallback.CreateFallbackBuffer().Fallback( | ||
| new[] { b }, | ||
| position); | ||
|
|
||
| Debug.Fail("Fallback should have thrown"); | ||
| throw new InvalidOperationException(); | ||
| } | ||
|
|
||
| if (write) | ||
| { | ||
| chars[position] = (char)b; | ||
| } | ||
| } | ||
|
|
||
| return bytes.Length; | ||
| } | ||
|
|
||
| // The vectorization is left out of the GetChars and GetBytes directly to not regress the code size | ||
| // and register allocation for small inputs. Instead they are extracted methods. | ||
| private int GetBytesVectorized(ReadOnlySpan<char> chars, Span<byte> bytes, bool write) | ||
| { | ||
| int available = write ? Math.Min(chars.Length, bytes.Length) : chars.Length; | ||
| int vectorizedLength = available - (available % Vector<byte>.Count); | ||
| int position = 0; | ||
|
|
||
| Debug.Assert(Vector<byte>.Count == 2 * Vector<ushort>.Count); | ||
|
|
||
| // Revisit this cast when Vector<char> is supported: https://github.com/dotnet/runtime/issues/127611 | ||
| ReadOnlySpan<ushort> source = MemoryMarshal.Cast<char, ushort>(chars); | ||
| Vector<ushort> minCharAllowed = new Vector<ushort>(_minCharAllowed); | ||
| Vector<ushort> range = new Vector<ushort>(_range); | ||
|
|
||
| for (; position < vectorizedLength; position += Vector<byte>.Count) | ||
| { | ||
| Vector<ushort> lower = new Vector<ushort>(source.Slice(position)); | ||
| Vector<ushort> upper = new Vector<ushort>(source.Slice(position + Vector<ushort>.Count)); | ||
|
|
||
| if (!IsAllowed(lower, minCharAllowed, range) || !IsAllowed(upper, minCharAllowed, range)) | ||
| { | ||
| // If any element in the vector is not allowed, we break out and return the position before the | ||
| // current vector's width so that it goes down the scalar path. The scalar path will determine the | ||
| // precise location of the invalid element. | ||
| break; | ||
| } | ||
|
|
||
| if (write) | ||
| { | ||
| Vector.Narrow(lower, upper).CopyTo(bytes.Slice(position)); | ||
| } | ||
| } | ||
|
|
||
| return position; | ||
| } | ||
|
|
||
| private int GetCharsVectorized(ReadOnlySpan<byte> bytes, Span<char> chars, bool write) | ||
| { | ||
| int available = write ? Math.Min(bytes.Length, chars.Length) : bytes.Length; | ||
| int vectorizedLength = available - (available % Vector<byte>.Count); | ||
| int position = 0; | ||
|
|
||
| Debug.Assert(Vector<byte>.Count == 2 * Vector<ushort>.Count); | ||
|
|
||
| // Revisit this cast when Vector<char> is supported: https://github.com/dotnet/runtime/issues/127611 | ||
| Span<ushort> destination = write ? MemoryMarshal.Cast<char, ushort>(chars) : Span<ushort>.Empty; | ||
| Vector<byte> minCharAllowed = new Vector<byte>(_minCharAllowed); | ||
| Vector<byte> range = new Vector<byte>(_range); | ||
|
|
||
| for (; position < vectorizedLength; position += Vector<byte>.Count) | ||
| { | ||
| Vector<byte> source = new Vector<byte>(bytes.Slice(position)); | ||
|
|
||
| if (!IsAllowed(source, minCharAllowed, range)) | ||
| { | ||
| // If any element in the vector is not allowed, we break out and return the position before the | ||
| // current vector's width so that it goes down the scalar path. The scalar path will determine the | ||
| // precise location of the invalid element. | ||
| break; | ||
| } | ||
|
|
||
| if (write) | ||
| { | ||
| Vector.Widen(source, out Vector<ushort> lower, out Vector<ushort> upper); | ||
| lower.CopyTo(destination.Slice(position)); | ||
| upper.CopyTo(destination.Slice(position + Vector<ushort>.Count)); | ||
| } | ||
| } | ||
|
|
||
| return position; | ||
| } | ||
|
|
||
| private bool IsAllowed(byte value) | ||
| { | ||
| return (byte)(value - _minCharAllowed) <= _range; | ||
| } | ||
|
|
||
| private bool IsAllowed(char value) | ||
| { | ||
| return (uint)(value - _minCharAllowed) <= _range; | ||
| } | ||
|
|
||
| private static bool IsAllowed(Vector<byte> value, Vector<byte> minCharAllowed, Vector<byte> range) | ||
| { | ||
| Vector<byte> offset = value - minCharAllowed; | ||
| return Vector.LessThanOrEqualAll(offset, range); | ||
| } | ||
|
|
||
| private static bool IsAllowed(Vector<ushort> value, Vector<ushort> minCharAllowed, Vector<ushort> range) | ||
| { | ||
| Vector<ushort> offset = value - minCharAllowed; | ||
| return Vector.LessThanOrEqualAll(offset, range); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.