Skip to content

Comments

Help JIT elide redundant CharToHexLookup range check#124702

Draft
xtqqczze wants to merge 1 commit intodotnet:mainfrom
xtqqczze:bounds-CharToHexLookup
Draft

Help JIT elide redundant CharToHexLookup range check#124702
xtqqczze wants to merge 1 commit intodotnet:mainfrom
xtqqczze:bounds-CharToHexLookup

Conversation

@xtqqczze
Copy link
Contributor

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 21, 2026
@dotnet-policy-service
Copy link
Contributor

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

@EgorBo
Copy link
Member

EgorBo commented Feb 21, 2026

@xtqqczze I'm not convinced those diffs are meaningful. They're just PMI artifacts where we prejit all methods, even with AggressiveInlining. So most of your diffs are just FromUpperChar and FromChar themselves.

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int FromChar(int c)
        {
            return (c >= CharToHexLookup.Length) ? 0xFF : CharToHexLookup[c];
        }

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

here JIT does emit a bound check because noone checks c for being negative so it's expected. But all callers one way or another pass something that is clearly non-negative and after inlining there is no bound check. You may add [AggressiveInlining] over DecodeHexChars if you want.

Also, you can add a Debug.Assert if you want

@xtqqczze
Copy link
Contributor Author

@EgorBo I was already thinking about AggressiveInlining, diffs: MihuBot/runtime-utils#1777

@EgorBo
Copy link
Member

EgorBo commented Feb 21, 2026

The problem that you effectively change the behavior here. The invalid code that used to throw Invalid range exception now silently uses 0xFF

@xtqqczze
Copy link
Contributor Author

The invalid code that used to throw Invalid range exception now silently uses 0xFF

HexConverter is internal code, but point taken. There are some projects targeting .NET Framework using these methods.

@xtqqczze
Copy link
Contributor Author

The problem that you effectively change the behavior here. The invalid code that used to throw Invalid range exception now silently uses 0xFF

When these methods are used as intended, with values that are clearly non-negative, there is no change in behavior, which explains the absence of meaningful diffs.

As such, this change is only expected to improve code generation for projects targeting .NET Framework.

@EgorBo As you suspected, more meaningful diffs can be seen by adding AggressiveInlining to methods like DecodeHexChars: MihuBot/runtime-utils#1778

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Meta 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.

2 participants