Repository navigation
test(loader): bound exact_decimal by digits scanned, not wall-clock time - #2950
Merged
Merged
Conversation
The linearity guard for exact_decimal asserted a 2 s bound on a 160,000-digit input and flaked on a loaded machine. Count the digits exact_decimal and parse_beancount_decimal scan instead, in a cfg(test) thread-local (the BQL parser's LITERAL_BUDGET pattern); outside tests the counter is an empty const fn. The renamed test asserts each call scans between one and three times its input length, so a quadratic scan fails deterministically and load cannot. Closes #2948 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Review round 1 on #2950. The counter noted lengths after the fact, so a quadratic rewrite of a hooked site still counted linearly. Every scan of the value in parse_tolerance_map, parse_beancount_decimal, exact_decimal, parse_exponent and is_python_number_part now reads through scan_bytes / scan_chars, which count each byte or character as it is yielded. exact_decimal no longer copies the coefficient into a Vec, so there is no buffer to grow by concatenation or shift with remove(0). The bound is 10 scans per input byte (measured 3.0 to 7.0), and the inputs now cover trailing whitespace, a long exponent, and separators. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… colons The scan counter cannot see quadratic copying (review round 1's surviving M11a: the comma/space cleaning accumulated by repeated `format!`). A new integration test installs `dhat::Alloc`, a safe counting global allocator, and bounds the bytes `Options::set` allocates for the long tolerance map, the long multiplier and the separated value at 20 per input byte (measured 10.1, 9.2 and 2.1). It is its own crate, so the loader keeps `#![forbid(unsafe_code)]`; it holds exactly one test because dhat's profiler is process-global. The scan-count test gains a tolerance map with 4,000 colons, which catches a per-colon rescan in `parse_tolerance_map`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 12, 2026
robcohen
added a commit
that referenced
this pull request
Nov 12, 2026
#2945 took loader v71 and WASM v99, so this branch keeps loader v73 and moves WASM to v102 (v100 and v101 are held by #2931 and #2952). Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 12, 2026
#2945, which this branch carried, is squash-merged as 526f833; its hunks take main's final form (loader cache v71, `escape_untrusted_text` in `rledger query`, its CHANGELOG notes). With #2947: the FFI's `semantic_validation_errors` keeps #2947's `booking_refused` set-aside and its `is_advisory_only` filter, which replaces this branch's equivalent one. `convert::surface_agreement` now matches each finding's code too (the 4.0.0 wire error carries it) and runs `validate-file` and `load-file` over the shared cases, so the file surfaces are pinned to leave out E1004 and set a refused transaction aside. No cache moves: the loader cache stores only error-free raw loads, and this branch changes no WASM output. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What changed
crates/rustledger-loader/src/options.rs: a#[cfg(test)]thread-localSCANNEDcounter and anote_scanned(n)hook, following the BQL parser'sLITERAL_BUDGET/note_literal_parsedpattern. Outside tests the hook is an empty inlinedconst fn.scan_bytes/scan_chars. These arebytes()/chars()with aninspectthat counts each item as it is yielded. That covers every scan inparse_tolerance_map,parse_beancount_decimal,exact_decimal,parse_exponent,is_python_number_part, and the newtrim_python_space, which replacestrim_matches(is_python_space).exact_decimalreads the coefficient in place and no longer copies it into aVec.test_exact_decimal_is_linear_on_long_inputis nowtest_exact_decimal_linear_digit_scans_on_long_input, with no timing. Each call must scan between 1 and 10 times per input byte. The seven inputs are 4,000 zeros a side, a long fraction, trailing whitespace, a long exponent, separators with surrounding whitespace, and a tolerance map with 4,000 colons.crates/rustledger-loader/tests/option_number_allocation.rs: underdhat's counting global allocator,Options::seton each long option value may allocate at most 20 bytes per input byte. This catches quadratic copying, which no scan counter sees.dhatis already a workspace dependency; it is added to the loader's[dev-dependencies]only.Why
The wall-clock bound flaked once on a heavily loaded machine (#2948). A work count is deterministic, so load cannot fail it. A quadratic scan through the hooks counts thousands of scans per byte, against a bound of 10.
No production behavior change. All existing option tests pass unchanged.
How to test
Review round 1
scan_bytes/scan_chars. A scan that does not, or work that is not a scan (copying or shifting a buffer), is invisible to the guard.exact_decimalfound by re-reading per byte_filter re-reads the text per characterparse_exponentskips leading zeros by re-reading per byteis_python_number_partcounts the dots at every byteformat!What remains: M1's original form (concatenating into the coefficient
Vec) and M2's original form (Vec::remove(0)) have nothing left to mutate, because thatVecis gone. M11a survived round 1: its quadratic is in copying, not scanning, and no scan counter can see it. Round 2 catches it with an allocation count (below).Review round 2
mainon 58M differential inputs: 0 mismatches.unsafe impl GlobalAlloc, ruled out by#![forbid(unsafe_code)]. That was wrong.dhat::Allocis a safe counting global allocator,dhatis already a workspace dependency, and an integration test is its own crate, outside the loader'sforbid(unsafe_code)and outsidescripts/check-unsafe-invariant.sh(which scanssrc/).tests/option_number_allocation.rs,test_options_set_long_number_allocation_is_linear: sets the long tolerance map, the long multiplier and the separated value throughOptions::setunderdhat::Profiler::builder().testing(), and bounds each call'stotal_bytesat 20 per input byte. The file holds exactly one test, because dhat's profiler is process-global. It isignored under Miri, where it checks nothing about memory safety.test_exact_decimal_linear_digit_scans_on_long_inputgainsUSD:1followed by:a4,000 times (2.0 scans per byte).format!parse_tolerance_maprescans the rest of the line at every colonCloses #2948
🤖 Generated with Claude Code