Commit b035fda
gh-NNNN: Add FP fast path for Ryu mode 2 (%e / %g)
Ryu's d2exp runs the full table-driven algorithm on every call (~230 ns
on this machine), while Gay's dtoa has a double-precision FP
approximation path that short-circuits easy inputs in ~150 ns. This is
the algorithmic gap that left %e / %g / %.4g 10–15% slower than main
after the Ryu switchover; prior commits could not close it at the
adapter level.
Add _pyryu_fast_mode2 — a ~60-line FP path that parallels Gay's
approach:
1. k = floor(log10(d)) gives a first-digit-position estimate.
2. Scale v = d · 10^(P-k) (or d / 10^(k-P)) using a table of the
exactly-representable powers 10^0..10^22, so v* (the mathematical
truth) and v (FP) differ by at most 0.5·ULP(v).
3. Verify v ∈ [10^P, 10^(P+1)) so FP log10 rounding can't silently
produce a 10× off answer.
4. Slop check: fail if 0.5 − |v − rint(v)| < |v|·2⁻⁵², which is the
only regime where rint(v) can disagree with rint(v*) at the tied
half-integer.
5. rint(v) → uint64_t → decimal digits.
Precision limit: P ≤ 14, so the rounded mantissa fits in 2^53 exactly
and the cast to uint64_t is lossless. Scale limit: |P-k| ≤ 22 (the
range of exact powers). Everything outside these bounds falls back to
d2exp, preserving Ryu's correctness guarantees for extreme magnitudes,
subnormals, inf/nan, and high-precision (>14-digit) requests.
Correctness: cross-checked against decimal.quantize(ROUND_HALF_EVEN)
on 2,000,000 random (d, P) pairs plus ~19,000 edge-case inputs
(near-tie values, near-powers-of-10, random bit patterns). Zero
mismatches. The v-range check in particular was added after a fuzz
test caught FP log10 producing k = −8 for d ≈ 9.99999999999999e−09
(whose true k is −9); the original digit-count check accepted the
resulting mi = 10^14 as a valid P+1-digit result, silently emitting
"1.00000000000000e-08" instead of "9.99999999999999e-09".
Benchmark (PYTHON_JIT=0, main vs this commit, geomean over 17 cases):
before: 1.80x speedup vs Gay, regressions 0.87–0.90x on %e/%g
after : 1.79x speedup vs Gay, regressions 0.89–0.95x on %e/%g
%.6e: 0.89 → 0.95 (within 5% of Gay)
%.2e: 0.89 → 0.95
%g : 0.84 → 0.89
%.4g: 0.85 → 0.93
f'{x:.6g}': 0.86 → 0.91
On easy inputs alone ('%.6e' % 1.5: 201 ns vs Gay's ~220 ns), the fast
path is now faster than Gay. The residual aggregate regression is
entirely from the hard-value bail cases (1e100, subnormals, inf/nan)
where d2exp still runs slower than Gay's Bignum fallback by ~30 ns.
test_float and test_format pass. Full suite shows no new failures;
the single test_frame failure is a pre-existing JIT crash also
present on main.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent c8851d9 commit b035fda
1 file changed
Lines changed: 172 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
80 | 223 | | |
81 | 224 | | |
82 | 225 | | |
| |||
699 | 842 | | |
700 | 843 | | |
701 | 844 | | |
702 | | - | |
703 | | - | |
| 845 | + | |
| 846 | + | |
704 | 847 | | |
705 | | - | |
706 | | - | |
707 | | - | |
708 | | - | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
709 | 853 | | |
710 | 854 | | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
711 | 877 | | |
712 | 878 | | |
713 | 879 | | |
| |||
0 commit comments