Skip to content

Commit cd25434

Browse files
Remove unsafe code from number parsing and formatting (#131913)
Replaces pointer-based parsing and formatting with span-based implementations across `NumberBuffer`, numeric types, `DateTime`, `TimeSpan`, decimal floating-point types, and `BigInteger`. The replacement paths preserve direct writes while avoiding additional allocations, temporary copies, and per-element destination bounds checks. ### Performance Ratio is candidate / baseline; lower is better. | Area | Result | |---|---:| | Double parsing | `0.86-0.91x` | | Exponent formatting | `0.91-0.95x` | | Hex float formatting | `0.71-0.78x` | | DateOnly `R` formatting | `0.66x` | | BigInteger parsing/formatting | generally `0.94-0.97x` | | Default integer decimal formatting | neutral to approximately `24%` faster | | Explicit integer `D`/`X`/`B` formats | neutral or faster; `UInt128 B` remained within approximately `1%` noise | | Grouped/custom span formatting | up to approximately `1.10x` | | TimeSpan standard formatting | approximately `1.03-1.07x` | There were no allocation increases in the 82-benchmark matrix. ---------- The remaining low-level operations are narrow, explicitly audited exceptions: - Integer `ToString` uses `FastAllocateString` and creates a writable span over the fresh exact-length string. The string cannot escape before every character is initialized. - Generic UTF-8/UTF-16 helpers use type- and size-proven `Unsafe.BitCast` operations. - Two-digit writers use `MemoryMarshal.Write` and a two-byte-aligned packed digit-pair representation to retain one store per pair without pointer arithmetic. - Fixed scratch buffers use measured `Unsafe.SkipInit` paths where all consumed elements are initialized before being read. - Existing RVA-table and `BigInteger` arithmetic reinterpretations remain where their sizes and representations are fixed. No executable `unsafe`, pointer writers, pinning, or `fixed` statements remain in the affected paths. The final baseline/candidate harness produced byte-identical output for 2,266 cases. `System.Runtime.Tests` passed 76,641 tests and `System.Runtime.Numerics.Tests` passed 8,422 tests. Fixes #10397 > [!NOTE] > This description was drafted by Copilot. --------- Co-authored-by: Copilot App <[email protected]>
1 parent 0ea1e36 commit cd25434

23 files changed

Lines changed: 1510 additions & 1496 deletions

‎src/libraries/Common/src/System/Number.Formatting.Common.cs‎

Lines changed: 500 additions & 297 deletions
Large diffs are not rendered by default.

‎src/libraries/Common/src/System/Number.NumberBuffer.cs‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5-
using System.Runtime.CompilerServices;
6-
using System.Runtime.InteropServices;
75
using System.Text;
86

97
namespace System
@@ -25,22 +23,14 @@ internal static partial class Number
2523
internal const int Decimal64NumberBufferLength = 16 + 1 + 1; // 16 for the longest input + 1 for rounding
2624
internal const int Decimal128NumberBufferLength = 34 + 1 + 1; // 34 for the longest input + 1 for rounding
2725

28-
internal unsafe ref struct NumberBuffer
26+
internal ref struct NumberBuffer
2927
{
3028
public int DigitsCount;
3129
public int Scale;
3230
public bool IsNegative;
3331
public bool HasNonZeroTail;
3432
public NumberBufferKind Kind;
3533
public Span<byte> Digits;
36-
/// <safety>Converts the ref to Digits into a pointer value via Unsafe.AsPointer and returns it without dereferencing; the result is not GC-tracked, so any use must be in an unsafe context that establishes Digits still refers to unmovable memory.</safety>
37-
public readonly byte* DigitsPtr => (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(Digits)); // safe since constructor expects Digits to refer to unmovable memory
38-
39-
public NumberBuffer(NumberBufferKind kind, byte* digits, int digitsLength) : this(kind, new Span<byte>(digits, digitsLength))
40-
{
41-
Debug.Assert(digits != null);
42-
}
43-
4434
/// <summary>Initializes the NumberBuffer.</summary>
4535
/// <param name="kind">The kind of the buffer.</param>
4636
/// <param name="digits">The digits scratch space. The referenced memory must not be moveable, e.g. stack memory, pinned array, etc.</param>

‎src/libraries/Common/src/System/Number.Parsing.Common.cs‎

Lines changed: 91 additions & 87 deletions
Large diffs are not rendered by default.

‎src/libraries/System.Private.CoreLib/src/System/Byte.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,8 @@ public static bool TryParse(ReadOnlySpan<byte> utf8Text, NumberStyles style, IFo
12041204
// IUtfChar
12051205
//
12061206

1207+
static bool IUtfChar<byte>.IsUtf8 => true;
1208+
12071209
static byte IUtfChar<byte>.CastFrom(byte value) => value;
12081210
static byte IUtfChar<byte>.CastFrom(char value) => (byte)value;
12091211
static byte IUtfChar<byte>.CastFrom(int value) => (byte)value;

‎src/libraries/System.Private.CoreLib/src/System/Char.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,8 @@ static bool INumberBase<char>.TryParsePartial(ReadOnlySpan<byte> utf8Text, Numbe
20732073
// IUtfChar
20742074
//
20752075

2076+
static bool IUtfChar<char>.IsUtf8 => false;
2077+
20762078
static char IUtfChar<char>.CastFrom(byte value) => (char)value;
20772079
static char IUtfChar<char>.CastFrom(char value) => value;
20782080
static char IUtfChar<char>.CastFrom(int value) => (char)value;

‎src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs‎

Lines changed: 186 additions & 226 deletions
Large diffs are not rendered by default.

‎src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4089,7 +4089,7 @@ private static bool ParseJapaneseEraStart(ref __DTString str, DateTimeFormatInfo
40894089

40904090
// Given a specified format character, parse and update the parsing result.
40914091
//
4092-
private static unsafe bool ParseByFormat(
4092+
private static bool ParseByFormat(
40934093
ref __DTString str,
40944094
ref __DTString format,
40954095
scoped ref ParsingInfo parseInfo,

‎src/libraries/System.Private.CoreLib/src/System/Globalization/NumberFormatInfo.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Diagnostics;
55
using System.Runtime.CompilerServices;
6-
using System.Runtime.InteropServices;
76
using System.Text;
87

98
namespace System.Globalization
@@ -103,7 +102,7 @@ private static ReadOnlySpan<byte> GetUtf8Span(ref byte[]? utf8Cache, string valu
103102
{
104103
byte[] utf8 = utf8Cache ?? CreateUtf8Cache(ref utf8Cache, value);
105104
Debug.Assert(utf8.Length > 0);
106-
return MemoryMarshal.CreateReadOnlySpan(ref MemoryMarshal.GetArrayDataReference(utf8), utf8.Length - 1);
105+
return utf8.AsSpan(0, utf8.Length - 1);
107106
}
108107

109108
[MethodImpl(MethodImplOptions.NoInlining)]

‎src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanFormat.cs‎

Lines changed: 89 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal static class TimeSpanFormat
1616
internal static readonly FormatLiterals NegativeInvariantFormatLiterals = FormatLiterals.InitInvariant(isNegative: true);
1717

1818
/// <summary>Main method called from TimeSpan.ToString.</summary>
19-
internal static unsafe string Format(TimeSpan value, string? format, IFormatProvider? formatProvider)
19+
internal static string Format(TimeSpan value, string? format, IFormatProvider? formatProvider)
2020
{
2121
if (string.IsNullOrEmpty(format))
2222
{
@@ -48,7 +48,7 @@ internal static unsafe string Format(TimeSpan value, string? format, IFormatProv
4848
}
4949

5050
/// <summary>Main method called from TimeSpan.TryFormat.</summary>
51-
internal static unsafe bool TryFormat<TChar>(TimeSpan value, Span<TChar> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? formatProvider) where TChar : unmanaged, IUtfChar<TChar>
51+
internal static bool TryFormat<TChar>(TimeSpan value, Span<TChar> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? formatProvider) where TChar : unmanaged, IUtfChar<TChar>
5252
{
5353
Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte));
5454

@@ -81,14 +81,14 @@ internal static unsafe bool TryFormat<TChar>(TimeSpan value, Span<TChar> destina
8181
return result;
8282
}
8383

84-
internal static unsafe string FormatC(TimeSpan value)
84+
internal static string FormatC(TimeSpan value)
8585
{
8686
Span<char> destination = stackalloc char[26]; // large enough for any "c" TimeSpan
8787
TryFormatStandard(value, StandardFormat.C, null, destination, out int charsWritten);
8888
return new string(destination.Slice(0, charsWritten));
8989
}
9090

91-
private static unsafe string FormatG(TimeSpan value, DateTimeFormatInfo dtfi, StandardFormat format)
91+
private static string FormatG(TimeSpan value, DateTimeFormatInfo dtfi, StandardFormat format)
9292
{
9393
string decimalSeparator = dtfi.DecimalSeparator;
9494
int maxLength = checked(25 + decimalSeparator.Length); // large enough for any "g"/"G" TimeSpan
@@ -106,7 +106,7 @@ internal enum StandardFormat
106106
g
107107
}
108108

109-
internal static unsafe bool TryFormatStandard<TChar>(TimeSpan value, StandardFormat format, ReadOnlySpan<TChar> decimalSeparator, Span<TChar> destination, out int written) where TChar : unmanaged, IUtfChar<TChar>
109+
internal static bool TryFormatStandard<TChar>(TimeSpan value, StandardFormat format, ReadOnlySpan<TChar> decimalSeparator, Span<TChar> destination, out int written) where TChar : unmanaged, IUtfChar<TChar>
110110
{
111111
Debug.Assert(format == StandardFormat.C || format == StandardFormat.G || format == StandardFormat.g);
112112

@@ -229,66 +229,114 @@ internal static unsafe bool TryFormatStandard<TChar>(TimeSpan value, StandardFor
229229
return false;
230230
}
231231

232-
fixed (TChar* dest = &MemoryMarshal.GetReference(destination))
232+
int pos = 0;
233+
234+
// Write leading '-' if necessary
235+
if (value.Ticks < 0)
236+
{
237+
destination[pos++] = TChar.CastFrom('-');
238+
}
239+
240+
// Write day and separator, if necessary
241+
if (dayDigits != 0)
233242
{
234-
TChar* p = dest;
243+
Number.WriteDigits(days, destination.Slice(pos, dayDigits));
244+
pos += dayDigits;
245+
destination[pos++] = TChar.CastFrom(format == StandardFormat.C ? '.' : ':');
246+
}
235247

236-
// Write leading '-' if necessary
237-
if (value.Ticks < 0)
248+
// After writing the variable-length prefix into destination[0..pos), write the
249+
// fixed "[h]h:mm:ss[.fraction]" suffix. We branch on hourDigits (1 or 2) and
250+
// initialize suffixLen to the minimum length (8 or 7) before conditionally adding
251+
// the fraction part, giving the JIT a concrete lower bound to hoist bounds checks
252+
// out of the inner writes.
253+
Debug.Assert(hourDigits == 1 || hourDigits == 2);
254+
int suffixLen;
255+
if (hourDigits == 2)
256+
{
257+
int decSepLen = 0;
258+
suffixLen = 8; // hh:mm:ss
259+
if (fractionDigits != 0)
238260
{
239-
*p++ = TChar.CastFrom('-');
261+
decSepLen = format == StandardFormat.C ? 1 : decimalSeparator.Length;
262+
suffixLen += decSepLen + fractionDigits;
240263
}
241-
242-
// Write day and separator, if necessary
243-
if (dayDigits != 0)
264+
// Invariant: suffixLen >= 8 by construction; this check is unreachable but lets
265+
// the JIT prove that all suffix writes at constant offsets 0..7 are in bounds.
266+
if ((uint)suffixLen < 8u)
244267
{
245-
Number.WriteDigits(days, p, dayDigits);
246-
p += dayDigits;
247-
*p++ = TChar.CastFrom(format == StandardFormat.C ? '.' : ':');
268+
ThrowHelper.ThrowUnreachableException();
248269
}
270+
Span<TChar> suffix = destination.Slice(pos, suffixLen);
271+
272+
Number.WriteTwoDigits(hours, suffix.Slice(0, 2));
273+
suffix[2] = TChar.CastFrom(':');
274+
Number.WriteTwoDigits((uint)minutes, suffix.Slice(3, 2));
275+
suffix[5] = TChar.CastFrom(':');
276+
Number.WriteTwoDigits((uint)seconds, suffix.Slice(6, 2));
277+
278+
if (fractionDigits != 0)
279+
{
280+
if (format == StandardFormat.C)
281+
{
282+
suffix[8] = TChar.CastFrom('.');
283+
}
284+
else if (decSepLen == 1)
285+
{
286+
suffix[8] = decimalSeparator[0];
287+
}
288+
else
289+
{
290+
decimalSeparator.CopyTo(suffix.Slice(8, decSepLen));
291+
}
249292

250-
// Write "[h]h:mm:ss
251-
Debug.Assert(hourDigits == 1 || hourDigits == 2);
252-
if (hourDigits == 2)
293+
Number.WriteDigits(fraction, suffix.Slice(8 + decSepLen, fractionDigits));
294+
}
295+
}
296+
else
297+
{
298+
int decSepLen = 0;
299+
suffixLen = 7; // h:mm:ss
300+
if (fractionDigits != 0)
253301
{
254-
Number.WriteTwoDigits(hours, p);
255-
p += 2;
302+
decSepLen = format == StandardFormat.C ? 1 : decimalSeparator.Length;
303+
suffixLen += decSepLen + fractionDigits;
256304
}
257-
else
305+
// Invariant: suffixLen >= 7 by construction; this check is unreachable but lets
306+
// the JIT prove that all suffix writes at constant offsets 0..6 are in bounds.
307+
if ((uint)suffixLen < 7u)
258308
{
259-
*p++ = TChar.CastFrom('0' + hours);
309+
ThrowHelper.ThrowUnreachableException();
260310
}
261-
*p++ = TChar.CastFrom(':');
262-
Number.WriteTwoDigits((uint)minutes, p);
263-
p += 2;
264-
*p++ = TChar.CastFrom(':');
265-
Number.WriteTwoDigits((uint)seconds, p);
266-
p += 2;
267-
268-
// Write fraction and separator, if necessary
311+
Span<TChar> suffix = destination.Slice(pos, suffixLen);
312+
313+
suffix[0] = TChar.CastFrom('0' + (int)hours);
314+
suffix[1] = TChar.CastFrom(':');
315+
Number.WriteTwoDigits((uint)minutes, suffix.Slice(2, 2));
316+
suffix[4] = TChar.CastFrom(':');
317+
Number.WriteTwoDigits((uint)seconds, suffix.Slice(5, 2));
318+
269319
if (fractionDigits != 0)
270320
{
271321
if (format == StandardFormat.C)
272322
{
273-
*p++ = TChar.CastFrom('.');
323+
suffix[7] = TChar.CastFrom('.');
274324
}
275-
else if (decimalSeparator.Length == 1)
325+
else if (decSepLen == 1)
276326
{
277-
*p++ = decimalSeparator[0];
327+
suffix[7] = decimalSeparator[0];
278328
}
279329
else
280330
{
281-
decimalSeparator.CopyTo(new Span<TChar>(p, decimalSeparator.Length));
282-
p += decimalSeparator.Length;
331+
decimalSeparator.CopyTo(suffix.Slice(7, decSepLen));
283332
}
284333

285-
Number.WriteDigits(fraction, p, fractionDigits);
286-
p += fractionDigits;
334+
Number.WriteDigits(fraction, suffix.Slice(7 + decSepLen, fractionDigits));
287335
}
288-
289-
Debug.Assert(p - dest == requiredOutputLength);
290336
}
291337

338+
Debug.Assert(pos + suffixLen == requiredOutputLength);
339+
292340
written = requiredOutputLength;
293341
return true;
294342
}
@@ -496,7 +544,7 @@ internal static FormatLiterals InitInvariant(bool isNegative)
496544
// the constants guaranteed to include DHMSF ordered greatest to least significant.
497545
// Once the data becomes more complex than this we will need to write a proper tokenizer for
498546
// parsing and formatting
499-
internal unsafe void Init(ReadOnlySpan<char> format, bool useInvariantFieldLengths)
547+
internal void Init(ReadOnlySpan<char> format, bool useInvariantFieldLengths)
500548
{
501549
dd = hh = mm = ss = ff = 0;
502550
_literals = new string[6];

‎src/libraries/System.Private.CoreLib/src/System/IUtfChar.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal interface IUtfChar<TSelf> :
1414
IBinaryInteger<TSelf>
1515
where TSelf : unmanaged, IUtfChar<TSelf>
1616
{
17+
/// <summary>Gets whether this type represents UTF-8 code units.</summary>
18+
public static abstract bool IsUtf8 { get; }
19+
1720
/// <summary>Casts the specified value to this type.</summary>
1821
public static abstract TSelf CastFrom(byte value);
1922

0 commit comments

Comments
 (0)