Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Guid.cs (C#)

Code before:
                return false;
            }

            // Check if we have extra characters at the end
            if (numStart + numLen + 1 != guidString.Length - 1)
            {
                result.SetFailure(ParseFailure.Format_ExtraJunkAtEnd);
                return false;
            }

            return true;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static byte DecodeByte<TChar>(TChar ch1, TChar ch2, ref int invalidIfNegative) where TChar : unmanaged, IUtfChar<TChar>
        {
            // TODO: https://github.com/dotnet/runtime/issues/116593
            // Restore to (sbyte)lookup[c1] and (sbyte)lookup[c2] once issue is resolved.

            ReadOnlySpan<byte> lookup = HexConverter.CharToHexLookup;
            Debug.Assert(lookup.Length == 256);

            uint c1 = typeof(TChar) == typeof(byte) ? TChar.CastToUInt32(ch1) : Math.Min(TChar.CastToUInt32(ch1), 0x7F);
            uint c2 = typeof(TChar) == typeof(byte) ? TChar.CastToUInt32(ch2) : Math.Min(TChar.CastToUInt32(ch2), 0x7F);
            int upper = (sbyte)Unsafe.Add(ref MemoryMarshal.GetReference(lookup), c1);
            int lower = (sbyte)Unsafe.Add(ref MemoryMarshal.GetReference(lookup), c2);

            int result = (upper << 4) | lower;
            invalidIfNegative |= result;
            return (byte)result;
        }

        private static bool TryParseHex<TChar>(ReadOnlySpan<TChar> guidString, out ushort result, ref bool overflow) where TChar : unmanaged, IUtfChar<TChar>
        {
            bool success = TryParseHex(guidString, out uint tmp, ref overflow);
            result = (ushort)tmp;
            return success;
        }

        private static bool TryParseHex<TChar>(ReadOnlySpan<TChar> guidString, out uint result) where TChar : unmanaged, IUtfChar<TChar>
        {
            bool overflowIgnored = false;
            return TryParseHex(guidString, out result, ref overflowIgnored);
        }

        private static bool TryParseHex<TChar>(ReadOnlySpan<TChar> guidString, out uint result, ref bool overflow) where TChar : unmanaged, IUtfChar<TChar>
        {
            if (guidString.Length > 0)
            {
                if (guidString[0] == TChar.CastFrom('+'))
                {

Review comment (suggestion):
Are you saying a [redundant zero extension](https://github.com/dotnet/runtime/issues/116593) is worth replacing with unsafe code?

The original change happened 2 years ago, Parse benchmarks were improved since then e.g. recent runs:

![image](https://github.com/user-attachments/assets/ff23143e-244d-41c4-924a-dedb8630f33a)

![image](https://github.com/user-attachments/assets/26df8781-ca70-4257-91f9-34eb2c736e3e)

I understand that you observe some wins from that, but I think they can wait for a proper JIT change.
We appreciate any change that replaces unsafe with safe, the opposite action require a strong motivation and micro-benchmarks aren't typically the case

Code after:
                    result.SetFailure(
                        overflow ? ParseFailure.Overflow_UInt32 :
                        byteVal > byte.MaxValue ? ParseFailure.Overflow_Byte :
                        ParseFailure.Format_GuidInvalidChar);
                    return false;
                }
                Unsafe.Add(ref result._d, i) = (byte)byteVal;
            }

            // Check for last '}'
            if (numStart + numLen + 1 >= guidString.Length || guidString[numStart + numLen + 1] != TChar.CastFrom('}'))
            {
                result.SetFailure(ParseFailure.Format_GuidEndBrace);
                return false;
            }

            // Check if we have extra characters at the end
            if (numStart + numLen + 1 != guidString.Length - 1)
            {
                result.SetFailure(ParseFailure.Format_ExtraJunkAtEnd);
                return false;
            }

            return true;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static byte DecodeByte<TChar>(TChar ch1, TChar ch2, ref int invalidIfNegative) where TChar : unmanaged, IUtfChar<TChar>
        {
            ReadOnlySpan<byte> lookup = HexConverter.CharToHexLookup;
            Debug.Assert(lookup.Length == 256);

            uint c1 = typeof(TChar) == typeof(byte) ? TChar.CastToUInt32(ch1) : Math.Min(TChar.CastToUInt32(ch1), 0x7F);
            uint c2 = typeof(TChar) == typeof(byte) ? TChar.CastToUInt32(ch2) : Math.Min(TChar.CastToUInt32(ch2), 0x7F);
            int upper = (sbyte)lookup[(int)c1];
            int lower = (sbyte)lookup[(int)c2];

            int result = (upper << 4) | lower;
            invalidIfNegative |= result;
            return (byte)result;
        }

        private static bool TryParseHex<TChar>(ReadOnlySpan<TChar> guidString, out ushort result, ref bool overflow) where TChar : unmanaged, IUtfChar<TChar>
        {
            bool success = TryParseHex(guidString, out uint tmp, ref overflow);
            result = (ushort)tmp;
            return success;
        }

        private static bool TryParseHex<TChar>(ReadOnlySpan<TChar> guidString, out uint result) where TChar : unmanaged, IUtfChar<TChar>
        {