Repository navigation
Description
Description
Follow-up to #10939, which was closed in 2022 with "if there are substantial gains to be had still, someone should feel empowered to re-open the issue with more details". This issue provides those details.
double.ToString() / TryFormat with the shortest round-trippable format (G, R, default) currently run Grisu3 (Number.Grisu3.cs) and fall back to Dragon4 (Number.Dragon4.cs, big-integer arithmetic) when Grisu3 cannot prove its result is the shortest. I ported Ulf Adams' Ryu d2s (shortest, never needs a fallback) into System.Private.CoreLib as Number.Ryu.cs, dispatched from Number.Formatting.cs for double when precision == -1, and measured it against the current implementation in three variants:
- Ryu full table – the standard
d2s_full_table.htables (342 + 326 entries of 128 bits), 128-bit multiply viaMath.BigMul(the JIT emits twomul rdx:rax, i.e. Ryu'sHAS_64_BIT_INTRINSICSpath). - Ryu small table – the
d2s_small_table.hscheme: every 26th power of five plus 5^0..5^25 and two 2-bit correction tables, reconstructing entries on the fly. This addresses the table-size objection raised in Consider implementing Ryu algorithm for double.ToString() #10939. - Ryu full table, UInt128 – as 1 but with three independent
UInt128multiply-shifts (Ryu's portableHAS_UINT128path) instead of the shared-product intrinsic form.
Two additions over a literal port were needed to avoid regressions on short decimals, which Ryu's "common case" handles one digit at a time: Ryu's own small-integer shortcut (d2d_small_int), and, once the candidate interval has shrunk to a single unit, stripping the remaining trailing decimal zeros in 8/4/2/1 chunks (one division per check instead of three). Without these, values such as 12.5, 0.1 and small integers were 25–70 % slower than Grisu3, which is worth knowing for anyone else evaluating Ryu against this code base.
Result: 20–35 % faster end-to-end (TryFormat into a span, invariant culture) on every data set tested, 4× faster on values where Grisu3 bails to Dragon4 (e.g. 1E+23), and identical output.
Configuration
- BenchmarkDotNet v0.16.0-preview.2 (nightly; stable 0.15.8 does not recognise the .NET 12 runtime),
--coreRunagainst four testhost copies that differ only inSystem.Private.CoreLib.dll - dotnet/runtime
mainat 6af607b, Release build, .NET 12.0.0-dev, X64 - Windows 11 (10.0.26200), AMD Ryzen 7 7800X3D (Zen 4, x86-64-v4)
- Whole-data-set benchmarks use
OperationsPerInvoke = count, so numbers are ns per formatted value; data sets are the csFastFloat corpora (canada.txt111,126 values,mesh.txt73,019,synthetic.txt150,000) plus 200k random finite bit patterns, 200k short decimals (1–7 significant digits) and 200k integers below 2^53
Regression?
No — proposed improvement. The prototype is not on main; I can open a PR if there is interest in taking it.
Data
Summary (mean ns; ratio is against the baseline):
| benchmark | value | baseline | Ryu full | Ryu small | Ryu UInt128 | full | small | UInt128 |
|---|---|---|---|---|---|---|---|---|
| Canada | 111k coordinates | 69.4 | 49.8 | 54.0 | 47.5 | 0.72 | 0.78 | 0.68 |
| Mesh | 73k small integers | 45.4 | 33.5 | 35.2 | 32.6 | 0.74 | 0.78 | 0.72 |
| Synthetic | 150k | 70.0 | 53.1 | 54.5 | 50.0 | 0.76 | 0.78 | 0.71 |
| RandomBits | 200k | 90.7 | 62.7 | 66.1 | 61.9 | 0.69 | 0.73 | 0.68 |
| RandomShort | 200k | 64.0 | 51.0 | 50.7 | 49.5 | 0.80 | 0.79 | 0.77 |
| RandomInt | 200k | 64.7 | 43.9 | 42.6 | 42.6 | 0.68 | 0.66 | 0.66 |
| ToString | −65.613616999999977 | 67.5 | 51.4 | 54.9 | 51.8 | 0.76 | 0.81 | 0.77 |
| ToString | 2.2250738585072009E‑308 | 75.7 | 54.0 | 58.5 | 54.5 | 0.71 | 0.77 | 0.72 |
| ToString | 0.1 | 35.9 | 36.5 | 40.2 | 36.3 | 1.02 | 1.12 | 1.01 |
| ToString | 1/3 | 65.2 | 52.4 | 54.8 | 53.0 | 0.80 | 0.84 | 0.81 |
| ToString | 104 | 41.2 | 26.2 | 33.0 | 27.4 | 0.64 | 0.80 | 0.67 |
| ToString | 12345.678 | 54.7 | 47.3 | 51.6 | 49.1 | 0.87 | 0.94 | 0.90 |
| ToString | 1E+23 | 152.6 | 39.1 | 43.5 | 39.7 | 0.26 | 0.29 | 0.26 |
| ToString | double.MaxValue | 80.1 | 56.0 | 60.8 | 57.6 | 0.70 | 0.76 | 0.72 |
Size of System.Private.CoreLib.dll (Release, IL) relative to the baseline 6,042,112 B:
| variant | lookup tables | DLL delta |
|---|---|---|
| Ryu full table | 10,688 B (5,472 + 5,216) | +13,824 B |
| Ryu full table, UInt128 | 10,688 B | +13,312 B |
| Ryu small table | 784 B | +5,120 B |
Tables are emitted as ReadOnlySpan<T> over RVA static data, so there is no allocation or static constructor.
Full BenchmarkDotNet output (toolchain column replaced by the variant name)
BenchmarkDotNet v0.16.0-preview.2, Windows 11 (10.0.26200.9550/25H2/2025Update/HudsonValley2)
AMD Ryzen 7 7800X3D 4.19GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK 11.0.100-rc.1.26420.103
[Host] : .NET 11.0.0 (11.0.0-rc.1.26420.103, 11.0.26.42103), X64 RyuJIT x86-64-v4
Job-KPXZQG : .NET 12.0.0-dev, X64 (baseline testhost)
Job-SYRIVK : .NET 12.0.0-dev, X64 (Ryu full table)
Job-ZHXNYV : .NET 12.0.0-dev, X64 (Ryu small table)
Job-FYOVJF : .NET 12.0.0-dev, X64 (Ryu full table, UInt128)
| Method | Host | value | Mean | Error | StdDev | Ratio | RatioSD |
|---|---|---|---|---|---|---|---|
| Canada | baseline (Grisu3+Dragon4) | 69.37 ns | 1.244 ns | 2.677 ns | 1.00 | 0.00 | |
| Canada | Ryu full table | 49.80 ns | 0.564 ns | 0.440 ns | 0.72 | 0.03 | |
| Canada | Ryu small table | 54.00 ns | 0.709 ns | 0.592 ns | 0.78 | 0.03 | |
| Canada | Ryu full table, UInt128 | 47.50 ns | 0.444 ns | 0.415 ns | 0.69 | 0.03 | |
| Mesh | baseline (Grisu3+Dragon4) | 45.39 ns | 0.353 ns | 0.331 ns | 1.00 | 0.00 | |
| Mesh | Ryu full table | 33.48 ns | 0.652 ns | 0.610 ns | 0.74 | 0.01 | |
| Mesh | Ryu small table | 35.18 ns | 0.669 ns | 0.593 ns | 0.78 | 0.01 | |
| Mesh | Ryu full table, UInt128 | 32.61 ns | 0.211 ns | 0.197 ns | 0.72 | 0.01 | |
| Synthetic | baseline (Grisu3+Dragon4) | 70.02 ns | 1.382 ns | 1.645 ns | 1.00 | 0.00 | |
| Synthetic | Ryu full table | 53.15 ns | 1.014 ns | 0.948 ns | 0.76 | 0.02 | |
| Synthetic | Ryu small table | 54.45 ns | 0.681 ns | 0.604 ns | 0.78 | 0.02 | |
| Synthetic | Ryu full table, UInt128 | 49.99 ns | 0.294 ns | 0.275 ns | 0.71 | 0.02 | |
| RandomBits | baseline (Grisu3+Dragon4) | 90.69 ns | 1.683 ns | 1.574 ns | 1.00 | 0.00 | |
| RandomBits | Ryu full table | 62.72 ns | 1.179 ns | 1.103 ns | 0.69 | 0.02 | |
| RandomBits | Ryu small table | 66.11 ns | 0.873 ns | 0.774 ns | 0.73 | 0.01 | |
| RandomBits | Ryu full table, UInt128 | 61.86 ns | 0.272 ns | 0.242 ns | 0.68 | 0.01 | |
| RandomShort | baseline (Grisu3+Dragon4) | 63.96 ns | 0.322 ns | 0.286 ns | 1.00 | 0.00 | |
| RandomShort | Ryu full table | 50.96 ns | 0.483 ns | 0.428 ns | 0.80 | 0.01 | |
| RandomShort | Ryu small table | 50.74 ns | 0.221 ns | 0.184 ns | 0.79 | 0.00 | |
| RandomShort | Ryu full table, UInt128 | 49.54 ns | 0.315 ns | 0.295 ns | 0.77 | 0.01 | |
| RandomInt | baseline (Grisu3+Dragon4) | 64.72 ns | 1.066 ns | 0.945 ns | 1.00 | 0.00 | |
| RandomInt | Ryu full table | 43.87 ns | 0.666 ns | 0.623 ns | 0.68 | 0.01 | |
| RandomInt | Ryu small table | 42.58 ns | 0.291 ns | 0.272 ns | 0.66 | 0.01 | |
| RandomInt | Ryu full table, UInt128 | 42.59 ns | 0.343 ns | 0.304 ns | 0.66 | 0.01 | |
| ToString_Value | baseline (Grisu3+Dragon4) | -65.61361699999998 | 67.54 ns | 0.448 ns | 0.419 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | -65.61361699999998 | 51.43 ns | 0.407 ns | 0.381 ns | 0.76 | 0.01 |
| ToString_Value | Ryu small table | -65.61361699999998 | 54.93 ns | 0.436 ns | 0.408 ns | 0.81 | 0.01 |
| ToString_Value | Ryu full table, UInt128 | -65.61361699999998 | 51.79 ns | 0.330 ns | 0.308 ns | 0.77 | 0.01 |
| TryFormat_Value | baseline (Grisu3+Dragon4) | -65.61361699999998 | 63.66 ns | 0.463 ns | 0.433 ns | 1.00 | 0.00 |
| TryFormat_Value | Ryu full table | -65.61361699999998 | 49.79 ns | 0.960 ns | 1.248 ns | 0.78 | 0.02 |
| TryFormat_Value | Ryu small table | -65.61361699999998 | 53.68 ns | 0.863 ns | 0.765 ns | 0.84 | 0.01 |
| TryFormat_Value | Ryu full table, UInt128 | -65.61361699999998 | 47.78 ns | 0.368 ns | 0.344 ns | 0.75 | 0.01 |
| ToString_Value | baseline (Grisu3+Dragon4) | 2.225(...)E-308 [22] | 75.69 ns | 0.538 ns | 0.476 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | 2.225(...)E-308 [22] | 54.00 ns | 0.126 ns | 0.105 ns | 0.71 | 0.00 |
| ToString_Value | Ryu small table | 2.225(...)E-308 [22] | 58.55 ns | 0.279 ns | 0.261 ns | 0.77 | 0.01 |
| ToString_Value | Ryu full table, UInt128 | 2.225(...)E-308 [22] | 54.46 ns | 0.308 ns | 0.288 ns | 0.72 | 0.01 |
| ToString_Value | baseline (Grisu3+Dragon4) | 0.1 | 35.86 ns | 0.335 ns | 0.314 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | 0.1 | 36.45 ns | 0.395 ns | 0.369 ns | 1.02 | 0.01 |
| ToString_Value | Ryu small table | 0.1 | 40.19 ns | 0.496 ns | 0.464 ns | 1.12 | 0.02 |
| ToString_Value | Ryu full table, UInt128 | 0.1 | 36.30 ns | 0.384 ns | 0.359 ns | 1.01 | 0.01 |
| ToString_Value | baseline (Grisu3+Dragon4) | 0.3333333333333333 | 65.24 ns | 0.485 ns | 0.430 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | 0.3333333333333333 | 52.35 ns | 0.400 ns | 0.374 ns | 0.80 | 0.01 |
| ToString_Value | Ryu small table | 0.3333333333333333 | 54.76 ns | 0.473 ns | 0.395 ns | 0.84 | 0.01 |
| ToString_Value | Ryu full table, UInt128 | 0.3333333333333333 | 53.01 ns | 0.535 ns | 0.501 ns | 0.81 | 0.01 |
| TryFormat_Value | baseline (Grisu3+Dragon4) | 0.3333333333333333 | 58.46 ns | 0.439 ns | 0.411 ns | 1.00 | 0.00 |
| TryFormat_Value | Ryu full table | 0.3333333333333333 | 47.17 ns | 0.376 ns | 0.352 ns | 0.81 | 0.01 |
| TryFormat_Value | Ryu small table | 0.3333333333333333 | 51.94 ns | 0.577 ns | 0.511 ns | 0.89 | 0.01 |
| TryFormat_Value | Ryu full table, UInt128 | 0.3333333333333333 | 46.73 ns | 0.386 ns | 0.342 ns | 0.80 | 0.01 |
| ToString_Value | baseline (Grisu3+Dragon4) | 104 | 41.18 ns | 0.370 ns | 0.328 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | 104 | 26.16 ns | 0.195 ns | 0.183 ns | 0.64 | 0.01 |
| ToString_Value | Ryu small table | 104 | 33.04 ns | 0.298 ns | 0.279 ns | 0.80 | 0.01 |
| ToString_Value | Ryu full table, UInt128 | 104 | 27.41 ns | 0.267 ns | 0.237 ns | 0.67 | 0.01 |
| TryFormat_Value | baseline (Grisu3+Dragon4) | 104 | 36.46 ns | 0.435 ns | 0.407 ns | 1.00 | 0.00 |
| TryFormat_Value | Ryu full table | 104 | 23.26 ns | 0.133 ns | 0.124 ns | 0.64 | 0.01 |
| TryFormat_Value | Ryu small table | 104 | 22.68 ns | 0.179 ns | 0.167 ns | 0.62 | 0.01 |
| TryFormat_Value | Ryu full table, UInt128 | 104 | 22.87 ns | 0.317 ns | 0.297 ns | 0.63 | 0.01 |
| ToString_Value | baseline (Grisu3+Dragon4) | 12345.678 | 54.69 ns | 0.656 ns | 0.581 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | 12345.678 | 47.33 ns | 0.360 ns | 0.336 ns | 0.87 | 0.01 |
| ToString_Value | Ryu small table | 12345.678 | 51.61 ns | 0.510 ns | 0.477 ns | 0.94 | 0.01 |
| ToString_Value | Ryu full table, UInt128 | 12345.678 | 49.09 ns | 0.409 ns | 0.362 ns | 0.90 | 0.01 |
| TryFormat_Value | baseline (Grisu3+Dragon4) | 12345.678 | 51.35 ns | 0.301 ns | 0.282 ns | 1.00 | 0.00 |
| TryFormat_Value | Ryu full table | 12345.678 | 44.90 ns | 0.780 ns | 0.730 ns | 0.87 | 0.01 |
| TryFormat_Value | Ryu small table | 12345.678 | 47.60 ns | 0.477 ns | 0.446 ns | 0.93 | 0.01 |
| TryFormat_Value | Ryu full table, UInt128 | 12345.678 | 44.19 ns | 0.546 ns | 0.484 ns | 0.86 | 0.01 |
| ToString_Value | baseline (Grisu3+Dragon4) | 1E+23 | 152.61 ns | 0.949 ns | 0.841 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | 1E+23 | 39.12 ns | 0.358 ns | 0.334 ns | 0.26 | 0.00 |
| ToString_Value | Ryu small table | 1E+23 | 43.52 ns | 0.460 ns | 0.431 ns | 0.29 | 0.00 |
| ToString_Value | Ryu full table, UInt128 | 1E+23 | 39.74 ns | 0.360 ns | 0.319 ns | 0.26 | 0.00 |
| ToString_Value | baseline (Grisu3+Dragon4) | 1.797(...)E+308 [23] | 80.11 ns | 1.565 ns | 1.739 ns | 1.00 | 0.00 |
| ToString_Value | Ryu full table | 1.797(...)E+308 [23] | 56.01 ns | 0.585 ns | 0.519 ns | 0.70 | 0.02 |
| ToString_Value | Ryu small table | 1.797(...)E+308 [23] | 60.78 ns | 0.539 ns | 0.478 ns | 0.76 | 0.02 |
| ToString_Value | Ryu full table, UInt128 | 1.797(...)E+308 [23] | 57.59 ns | 0.698 ns | 0.619 ns | 0.72 | 0.02 |
Analysis
- Why it is faster: Grisu3 generates digits with 64-bit DiyFp arithmetic but has to bail out (a fraction of a percent of inputs, and systematically for cases like exact powers of two and
1E+23) into Dragon4's big-integer loop. Ryu needs one 128-bit multiply per candidate (three candidates sharing the products), integer division by constants, and never falls back. - Table size: the full tables cost 10.7 KB, the concern raised in Consider implementing Ryu algorithm for double.ToString() #10939. The small-table variant needs 784 B and is still 21–34 % faster than the baseline; its reconstruction costs a fairly constant 3–5 ns per call (two extra 64×64→128 multiplies and a 128-bit shift/add), which is the whole difference to the full table. For platforms where size dominates (wasm, NativeAOT) that trade looks reasonable; for CoreCLR the full table is the faster choice.
- Intrinsic vs UInt128: on this JIT the portable
UInt128form is a tie with (or 1–2 ns ahead of) the shared-product intrinsic form:UInt128 * ulonglowers to the samemul, and three independent multiplies give more ILP than the carry chains of the shared version. The simpler code could therefore be used without loss. - Short values: Ryu's common path removes one digit per iteration with three divisions. Real-world data is dominated by short decimals and integers, so a literal port loses to Grisu3 there (measured:
mesh.txt+29 %,0.1+70 %). The small-integer shortcut plus chunked trailing-zero stripping once the interval spans a single unit fixes this;0.1ends at parity. - Correctness verification: output byte-identical to the current implementation for
ToString()andToString("R")on 4,343,322 values (the three corpora, 2M random bit patterns, 1M short decimals, 1M integers, and ~9k edge cases including every power of two and ten with neighbours and all subnormal powers); 0 round-trip failures; a DEBUG build of the port compared digit-by-digit against the baseline on 14.3M values with all asserts enabled;DoubleTests/SingleTests/HalfTests/DecimalTests/ConvertTestsfromSystem.Runtime.Tests(5,487 tests) pass on the modified CoreLib. - Scope/licensing: double only (
float/Halfkeep Grisu3→Dragon4; Ryu'sf2swould be a small follow-up). Ryu is dual-licensed Apache-2.0 / Boost-1.0; the prototype adds a Boost notice toTHIRD-PARTY-NOTICES.TXTnext to the existing Grisu3/Dragon4 entries. - Numbers are from one machine (Zen 4); Arm64 and Intel data, e.g. via @EgorBot on a PR, would be useful before deciding.
Activity
dotnet-policy-service commentedon Sep 24, 2026
Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.
tannergooding commentedon Sep 24, 2026
We're planning on switching the Grisu3 fast path to use #131068 instead, so it should be compared against that instead
This doesn't sound like a correct summary or otherwise like it is not correct for all possible inputs. You need something that can handle printing up to the entire 767 significant digit string for double
This is a major disadvantage and likely means Ryu is optimizing for the uncommon case
This needs significantly more testing, including cases like G999 and other scenarios that are very critical to the entire testbed and correctness
This is another major limitation and likely a non-starter. We do not want to maintain 3 algorithms, we barely want to maintain 2.
Mrnikbobjeff commentedon Sep 25, 2026
This is just the double baseline, the same method exists for float and halfs (with a reduced size impact for each table). I think I might have found a test bug for doubles, but otherwise the full suite runs through. The three algorithms would collapse to Ryu for <18 digits and to dragon4 for arbritrary, as it is now. With the optimizations from the linked PR I am able to beat this even further. I ran this for doubles, floats and halfs with the improvements and am able to beat both algorithms. It also seems like we already store the ryu table in the runtime, thus the only change we need is to change the algorithm with an actual final size reduction resulting for the runtime
Mrnikbobjeff commentedon Sep 25, 2026
#134638 Seems to beat out my implementation and the existing baseline and PR, there are optimizations in my branch I would copy over since they are agnostic to the algorithm used (small number optimization, trailing zero optimizations. The only issue is that the implementation from the linked item would increase the bin size far more than this pr (sigma 1)
@tannergooding I would link the new bench results comparing all implementations in an hour when they finish. This should either be closed or restated though.
Mrnikbobjeff commentedon Sep 26, 2026
Re‑measurement (mean ns;
prev= before the strip fix;zmij= compact 670 B cache,zfull= full 9.9 KB cache)Where things stand:
0.134.7 ns beats even Grisu3's one‑digit early exit at 39.9;1E+2337.3), and on every float data set. Only two per‑value float rows (-65.61362f1.05×, the float subnormal 1.03×) sit within noise of unrounded scaling.