Skip redundant fractional truncation scans - #402
Open
perfloop-agent wants to merge 2 commits into
Open
Conversation
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
parse_mantissaunconditionally scans the fraction span for nonzero digits to decide truncation, even when the integer-suffix scan has already found one. For inputs with a long integer part and a very long fraction, that second scan is pure overhead: once any nonzero digit is known, the rounding decision is already determined.This PR short-circuits the fraction scan when
truncatedis already true:Semantics are unchanged by construction (
a |= f()→if (!a) a = f()for a side-effect-freef): the same inputs round the same way, and zero-integer-suffix inputs still scan the fraction as before.Performance
Measured with the opt-in benchmark added in this PR (
-DFASTFLOAT_TRUNCATED_FRACTION_BENCHMARK=ON), on a 770-digit integer with a nonzero suffix followed by a 4 MiB zero fraction:parse_mantissa(GCC 14.2, x86-64)from_charsIndependently reproduced on Apple Silicon (clang, arm64): ~150 µs → ~0.85 µs on the direct shape.
This path is only reached by long ambiguous decimals that fall back to big-integer comparison, so ordinary parsing is unaffected; the win matters for adversarially long inputs, where the removed scan is O(fraction length) per parse.
Testing
basictest).basictest.cppfor long nonzero-suffix + long-zero-fraction inputs, includingwchar_t/char16_t/char32_t.Full verification record: https://app.perfloop.ai/t/oss/case_9k6jbbrrax