Normalize long trailing decimal zero suffixes - #403
Open
perfloop-agent wants to merge 2 commits into
Open
Conversation
Measure the digit comparison fallback directly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a long ambiguous decimal falls back to big-integer comparison,
digit_compmaterializes every digit into the bigint — including an arbitrarily long suffix of trailing zeros, which inflates limb construction and the subsequent comparison for no informational gain.This PR trims a long trailing-zero suffix before materializing the coefficient:
has_trailing_zerois an O(1) last-character check, so inputs without a trailing zero pay a single branch.trim_zeros_from_endscans backward (word-at-a-time, then per-character); suffixes shorter than 16 code units are kept as-is (reverse-scan setup doesn't pay for itself below that).sci_exp + 1 - digits) by exactly K — the represented valuemantissa × 10^expis unchanged, so rounding decisions are identical.120.3000keeps the zero in120), and an all-zero coefficient is never reduced to empty spans.The scientific exponent is computed from the original parsed number before trimming; only the local spans passed to
parse_mantissachange.Performance
Measured with the opt-in benchmark added in this PR (
-DFASTFLOAT_TRAILING_ZERO_BENCHMARK=ON), over ambiguous 20-, 30-, and 120-digit cores with 64–4096 trailing zeros:digit_comp(per conversion)from_chars<double>(per parse)Independently reproduced on Apple Silicon (clang, arm64): ~1035 ns → ~114 ns per digit comparison on the same shapes.
Testing
basictestand a sanitizer run of the new trailing-zeros test.trailing_zeros_testcovers the 16-unit cutoff boundary, integer-vs-fraction trimming (120.3000-style), all-zero coefficients, and wide-character variants.double,float, andchar16_t/char32_t/wchar_toutputs (bits, end pointer, and error code) are identical before and after.Note: this touches the same slow-path file as #402; whichever lands second will need a trivial test-file rebase.
Full verification record: https://app.perfloop.ai/t/oss/case_xwm9h4yejp