-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Optimize HexConverter.FromChar
#81146
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,13 +275,13 @@ public static bool TryDecodeFromUtf16(ReadOnlySpan<char> chars, Span<byte> bytes | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int FromChar(int c) | ||
{ | ||
return c >= CharToHexLookup.Length ? 0xFF : CharToHexLookup[c]; | ||
return (uint)c >= 'f' + 1 ? 0xFF : CharToHexLookup[c]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int FromUpperChar(int c) | ||
{ | ||
return c > 71 ? 0xFF : CharToHexLookup[c]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an off-by-one error in the original code since 'F' is 70. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not an actual error because |
||
return (uint)c >= 'F' + 1 ? 0xFF : CharToHexLookup[c]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would make sense if the size of CharToHexLookup could be reduced. Otherwise, what is the benefit of this optimization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JIT seems to depend on the GE operand to perform the elimination (#35348).
https://csharp.godbolt.org/z/Peqfx8KfM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already do this for
FromUpperChar
. One benefit is code size is reduced, see sharplabI'm also assuming it's beneficial to avoid an unnecessary load.
runtime/src/libraries/Common/src/System/HexConverter.cs
Line 284 in 9b76c28
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #81160
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean CharToHexLookup size could be reduced to 128.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look so - there are places where it's accessed without cheking bounds using a byte indexer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have seen a whole series of PRs trying to reduce the size of this library. So the question is whether it's important enough to add one command but reduce the size.