diff --git a/src/System.Text.ValueBuilder/System/Text/ValueStringBuilder.cs b/src/System.Text.ValueBuilder/System/Text/ValueStringBuilder.cs index 790479b8899..4bc8b991e0e 100644 --- a/src/System.Text.ValueBuilder/System/Text/ValueStringBuilder.cs +++ b/src/System.Text.ValueBuilder/System/Text/ValueStringBuilder.cs @@ -203,7 +203,7 @@ public unsafe void Append(char* value, int length) _pos += length; } - public unsafe void Append(ReadOnlySpan value) + public void Append(ReadOnlySpan value) { int pos = _pos; if (pos > _chars.Length - value.Length) diff --git a/src/System.Text.ValueBuilder/System/Variant.cs b/src/System.Text.ValueBuilder/System/Variant.cs index 7bfd82a763a..3452670453b 100644 --- a/src/System.Text.ValueBuilder/System/Variant.cs +++ b/src/System.Text.ValueBuilder/System/Variant.cs @@ -58,12 +58,7 @@ public unsafe bool TryGetValue(out T value) where T : unmanaged || (typeof(T) == typeof(uint) && Type == VariantType.UInt32) || (typeof(T) == typeof(ulong) && Type == VariantType.UInt64)) { - // The JIT is able to generate more efficient code when including the - // code for CastTo() directly. - fixed (void* u = &_union) - { - value = *(T*)u; - } + value = CastTo(); success = true; } @@ -71,12 +66,9 @@ public unsafe bool TryGetValue(out T value) where T : unmanaged } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private unsafe T CastTo() where T : unmanaged + private T CastTo() where T : unmanaged { - fixed (void* u = &_union) - { - return *(T*)u; - } + return Unsafe.As(ref Unsafe.AsRef(_union)); } // We have explicit constructors for each of the supported types for performance