From ce5873cdccf4ff9db730f2a78d476d73c5133331 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 25 Jan 2023 03:58:34 +0000 Subject: [PATCH 1/2] Eliminate FromChar bounds checks --- src/libraries/Common/src/System/HexConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/src/System/HexConverter.cs b/src/libraries/Common/src/System/HexConverter.cs index fbd5d789c77d8..823511a3446b8 100644 --- a/src/libraries/Common/src/System/HexConverter.cs +++ b/src/libraries/Common/src/System/HexConverter.cs @@ -275,13 +275,13 @@ public static bool TryDecodeFromUtf16(ReadOnlySpan chars, Span bytes [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int FromChar(int c) { - return c >= CharToHexLookup.Length ? 0xFF : CharToHexLookup[c]; + return (uint)c >= CharToHexLookup.Length ? 0xFF : CharToHexLookup[c]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int FromUpperChar(int c) { - return c > 71 ? 0xFF : CharToHexLookup[c]; + return (uint)c >= 72 ? 0xFF : CharToHexLookup[c]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] From 3b3d0db0243da39437d9cb852d1b82c06112dd7f Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 25 Jan 2023 04:15:52 +0000 Subject: [PATCH 2/2] Optimize conditions --- src/libraries/Common/src/System/HexConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/src/System/HexConverter.cs b/src/libraries/Common/src/System/HexConverter.cs index 823511a3446b8..efa17bf55591d 100644 --- a/src/libraries/Common/src/System/HexConverter.cs +++ b/src/libraries/Common/src/System/HexConverter.cs @@ -275,13 +275,13 @@ public static bool TryDecodeFromUtf16(ReadOnlySpan chars, Span bytes [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int FromChar(int c) { - return (uint)c >= CharToHexLookup.Length ? 0xFF : CharToHexLookup[c]; + return (uint)c >= 'f' + 1 ? 0xFF : CharToHexLookup[c]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int FromUpperChar(int c) { - return (uint)c >= 72 ? 0xFF : CharToHexLookup[c]; + return (uint)c >= 'F' + 1 ? 0xFF : CharToHexLookup[c]; } [MethodImpl(MethodImplOptions.AggressiveInlining)]