Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Jan 25, 2023

  • Eliminate bounds checks
  • Clarify conditions

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jan 25, 2023
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int FromUpperChar(int c)
{
return c > 71 ? 0xFF : CharToHexLookup[c];
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an actual error because c being 70 will return 0xFF anyway (from the lookup)

@xtqqczze xtqqczze changed the title Eliminate FromChar bounds checks Optimize HexConverter.FromChar Jan 25, 2023
@ghost
Copy link

ghost commented Jan 25, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details
  • Eliminate bounds checks
  • Clarify conditions
Author: xtqqczze
Assignees: -
Labels:

area-System.Runtime, community-contribution

Milestone: -

@@ -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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(uint)c > 'f' ? 0xFF : CharToHexLookup[c];

Copy link
Contributor

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?

Copy link
Contributor Author

@xtqqczze xtqqczze Jan 25, 2023

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

Copy link
Contributor Author

@xtqqczze xtqqczze Jan 25, 2023

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?

We already do this for FromUpperChar. One benefit is code size is reduced, see sharplab

I'm also assuming it's beneficial to avoid an unnecessary load.

return c > 71 ? 0xFF : CharToHexLookup[c];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #81160

Copy link
Contributor

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.

Copy link
Member

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.

It doesn't look so - there are places where it's accessed without cheking bounds using a byte indexer.

Copy link
Contributor

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.

@EgorBo
Copy link
Member

EgorBo commented Jan 25, 2023

Worth noting that it probably doesn't remove bound checks in cases where input is known to be non-zero as those are already handled as is, e.g. consider this:

image

@ghost
Copy link

ghost commented Feb 28, 2023

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 30, 2023
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants