Skip to content

Write short decimal floats without Ryu's digit-by-digit loop - #1239

Open
quinnj-bot[bot] wants to merge 1 commit into
mainfrom
claude/write-short-decimals
Open

quinnj-bot[bot] wants to merge 1 commit into
mainfrom
claude/write-short-decimals

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

Floats with a few decimals, such as prices, measurements, and coordinates, wrote at about a third of the speed of random floats. Ryu finds the shortest digits by removing one decimal digit per loop step, so 123.45 takes about a dozen steps before it reaches its five digits. Ryu's reduction alone costs 74 to 91 cycles for such values, against 39 for a random Float64.

What changed

  • _writeshortdecimal handles non-integer values from 1e-9 to 1e15 whose shortest form has at most 15 significant digits. It scales |x| to a 14- or 15-digit integer, rounds it, checks that this decimal rounds back to x (one correctly rounded division by an exact power of ten), and drops trailing zeros.
  • _writereduce tries it after the existing binary-fraction check. Integer-valued floats and every other value still use Ryu.

Why the digits match Ryu

Two different decimals of at most 15 significant digits, both at least 10^E, differ by at least 10^(E-14). A Float64 below 10^(E+1) is closer than 2.3×10^(E-15) to every decimal that rounds to it, which is narrower than that gap. So at most one such decimal rounds to x. When the check finds one, it is the shortest decimal that rounds to x, which is what Ryu returns. Midpoints between Float64 values need more than 15 digits, so no tie rule comes into play.

Scope

  • Output bytes do not change. Float32, Float16, floatformat, and the quoted-float path are untouched.
  • Values with 16 or 17 significant digits pay for the failed check before Ryu.

Tradeoffs

This is not free for full-precision floats. A column of random Float64 values (17 significant digits) runs 5% more instructions. In the quieter final sweep it measured +2.2% cycles (range +0.8% to +3.7%, 5 rounds, load 10 to 22). In the busier per-PR run it measured -0.2% (range -6% to +2%, 7 rounds, load 24 to 79). On CI's Linux runners (Julia 1.13.4, 4 rounds, medians), the 60-column random-float table measured +2.4% on x86 and +6.4% on arm. Short decimals get 1.6x faster. Whether that trade is worth taking is a call for the maintainer.

Verification

  • Short-decimal column (1M rows, 4 columns of 1 to 3 decimals, one core): 512.8 → 320.5 M cycles, 142.8 → 90.2 ms. That is 0.625x the cycles, the median of 7 interleaved fresh-process rounds with a range of 0.608 to 0.633. The 12-column mixed table measures 0.952x. The other cases measure 0.99 to 1.00x.
  • Byte identity: the 1,464-case writer fingerprint from Write Bool cells without hashing their text #1235 hashes the same on main and on this branch.
  • _writereduce equals Base.Ryu.reduce_shortest on 134 million values: random bit patterns, random and rounded decimals at many scales, every k×10^e for k below 10^6 and |e| ≤ 12 with both neighbors, and powers of ten from 1e-340 to 1e310 with neighbors. This was a local script and is not part of the PR.
  • A new test renders 1- to 16-digit decimals across and past the path's range, with neighbors, and compares them with string(x).

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.07%. Comparing base (b9d8f5e) to head (bfc958e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1239      +/-   ##
==========================================
+ Coverage   96.05%   96.07%   +0.01%     
==========================================
  Files           9        9              
  Lines        5885     5905      +20     
==========================================
+ Hits         5653     5673      +20     
  Misses        232      232              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Ryu removes one decimal digit per loop step, so a value like 123.45 runs
about a dozen steps before it reaches its five digits. Short decimals are
common in real tables (prices, measurements, coordinates), and they wrote
at a third of the speed of random floats.

A decimal of at most 15 significant digits is the only such decimal that
rounds to its Float64, so when one exists it is the shortest and Ryu returns
it. _writeshortdecimal scales |x| to a 14- or 15-digit integer, rounds it,
checks that the decimal rounds back to x, and drops trailing zeros. Other
values, and integer-valued floats, use Ryu as before. Output bytes are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quinnj-bot
quinnj-bot Bot force-pushed the claude/write-short-decimals branch from 5c9ab16 to bfc958e Compare November 19, 2026 20:49
@quinnj-bot

quinnj-bot Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

An independent review of bfc958e found the output correct. One question remains for the maintainer.

  • Same digits as Ryu. I compared _writereduce with Base.Ryu.reduce_shortest on about 53 million Float64 values and found 0 mismatches. The values were:
    • 20 million random bit patterns.
    • Random decimals of 1 to 17 digits at exponents -40 to 40, each with both neighbors.
    • Every k×10^e for k ≤ 200,000 and -16 ≤ e ≤ 4, each with both neighbors.
    • Powers of two and ten across the whole range, plus 2 million subnormals.
  • Same text as Base, and it reads back. Full cell rendering matched string(x) and parsed back to the same value for 4 million Float64 values, all 65,536 Float16 values, and 10 million Float32 values. It also matched for ±0.0, NaN, and ±Inf.
  • The reasoning holds. The new path only considers decimals on a grid at least 45 ulps wide, so at most one of them can round to x. The exact division confirms that one. Subnormals, values below 1e-9, integers, and values of 1e14 and up never take the new path.
  • No change to CSV output. I ran a 1,794-case CSV.write/RowWriter fingerprint with all eight open writer and reader PRs applied together. Its output is byte-identical to main.
  • Still open: full-precision random floats cost about 2–6% more cycles, while short decimals get about 1.6x faster. Whether that trade is worth it is still the maintainer's call.

[comment generated via AI]

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant