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

Add AggressiveInlining to Char.IsLatin1 #84414

Closed
wants to merge 1 commit into from
Closed

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Apr 6, 2023

  • Force the JIT to inline this 17 byte method to elide bounds checks.
  • Remove useless uint casts as char is unsigned and ReadOnlySpan<T>.Length is never negative.

* Force the JIT to inline this 17 byte method to elide bounds checks.
* Remove useless `uint` casts as `char` is unsigned and `ReadOnlySpan<T>.Length` is never negative.
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 6, 2023
@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Apr 6, 2023
@@ -81,7 +81,8 @@ namespace System
};

// Return true for all characters below or equal U+00ff, which is ASCII + Latin-1 Supplement.
private static bool IsLatin1(char c) => (uint)c < (uint)Latin1CharInfo.Length;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Copy link
Member

Choose a reason for hiding this comment

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

Do you have an example where it's not inlined currently?

casts are indeed not needed (since net8)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://csharp.godbolt.org/z/fexKr3f4x

However, it looks like IsLatin1 is actually inlined in Char as it is a struct not a class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@EgorBo Is it worth keeping the AggressiveInlining to express intent, or not?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I see it's not inlined, but as far as I remember I already complained about it somewhere, the problem was that RVA spans emit an IL that JIT's inliner is not able analyze properly (it doesn't know that it's foldable)

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@EgorBo Sorry for not being clear, since Char is a struct the method is in fact inlined (sharplab.io).

Copy link
Contributor Author

@xtqqczze xtqqczze Apr 6, 2023

Choose a reason for hiding this comment

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

However, not with crossgen2 (godbolt).

Copy link
Member

Choose a reason for hiding this comment

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

@xtqqczze change host type from struct to class

Copy link
Member

Choose a reason for hiding this comment

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

Ah you meant that in this case it's fine - yes. But overall it showcases a real problem inliner is not able to figure out

Copy link
Member

Choose a reason for hiding this comment

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

Filed #84416

Copy link
Member

Choose a reason for hiding this comment

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

Is it worth keeping the AggressiveInlining to express intent, or not?

No, but thanks

@stephentoub stephentoub added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Apr 6, 2023
@stephentoub stephentoub closed this Apr 6, 2023
@xtqqczze xtqqczze deleted the patch-4 branch April 6, 2023 13:27
@ghost ghost locked as resolved and limited conversation to collaborators May 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community-contribution Indicates that the PR has been added by a community member needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants