You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit cd25434
Browse filesBrowse the repository at this point in the historyBrowse 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]>
Copy file name to clipboardExpand all lines: src/libraries/Common/src/System/Number.NumberBuffer.cs
+1-11Lines changed: 1 addition & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,6 @@
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
usingSystem.Diagnostics;
5
-
usingSystem.Runtime.CompilerServices;
6
-
usingSystem.Runtime.InteropServices;
7
5
usingSystem.Text;
8
6
9
7
namespaceSystem
@@ -25,22 +23,14 @@ internal static partial class Number
25
23
internalconstintDecimal64NumberBufferLength=16+1+1;// 16 for the longest input + 1 for rounding
26
24
internalconstintDecimal128NumberBufferLength=34+1+1;// 34 for the longest input + 1 for rounding
27
25
28
-
internalunsaferefstructNumberBuffer
26
+
internalrefstructNumberBuffer
29
27
{
30
28
publicintDigitsCount;
31
29
publicintScale;
32
30
publicboolIsNegative;
33
31
publicboolHasNonZeroTail;
34
32
publicNumberBufferKindKind;
35
33
publicSpan<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
-
publicreadonlybyte*DigitsPtr=>(byte*)Unsafe.AsPointer(refMemoryMarshal.GetReference(Digits));// safe since constructor expects Digits to refer to unmovable memory
0 commit comments