Skip to content

fix(core): infer display precision from the numbers beancount samples - #3072

Merged
robcohen merged 2 commits into
mainfrom
fix/display-precision-sampling
Nov 28, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/display-precision-sampling

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

DisplayContext::from_directives now samples, for each currency, the numbers beancount's parser samples:

  • every number written in a cost spec: a compound cost {143.3998 # 4.61 USD} is a 4 dp and a 2 dp sample. It fell through total().or_else(per_unit()).unwrap_or_default() to a single 0, a 0 dp sample nobody wrote. A component left out ({# 5.60 USD}, held as a scale-0 zero) is still not sampled.
  • not a balance's tolerance: 4773.26 ~ 0.05 USD samples the amount only, as beancount does.

Why

Either could move a currency's most-common precision, which rledger query and the reports render amounts with, so the same ledger rendered with a different number of decimals than bean-query. Black-box probes of beancount 3.2.3 (options_map["dcontext"]): balance A 1 ~ 0.005 USD gives {0: 1}; {1.234 # 5.6 USD} gives {3: 1, 1: 1}; {# 5.6 USD} gives {1: 1}.

It was the largest category of the semantic differential (#3071): 173 of 10,730 ledgers, options|display_precision; minimized:

2010-05-28 *
  Assets:Account1     {150 # 5 USD}
  Assets:Account2     120.00 USD

With this PR the category drops to 5 (compat files whose custom amounts are a declared deviation's neighbours).

No cache bump: neither cache archives the display context; it is rebuilt from the directives on every load.

How to test

cargo test -p rustledger-core --lib display_context

New: a_balance_tolerance_does_not_inform_precision, a_compound_cost_samples_both_written_numbers, an_omitted_compound_component_is_not_sampled, per_unit_and_total_costs_are_sampled_as_written. The core, query, loader, CLI and WASM suites pass.

After #3071 merges, its manifest's options|display_precision count is ratcheted with scripts/compat-semantic-parity.py --update (here or in a follow-up, whichever lands second).

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 828
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 59.5ms ± 0.7ms 726.8ms ± 0.9ms 12.2x 45.2 MB / 51.0 MB
investment 85.8ms ± 0.9ms 2749.3ms ± 34.8ms 32.1x 47.0 MB / 59.2 MB
fifo 86.2ms ± 1.0ms 16964.3ms ± 175.9ms 196.8x 49.0 MB / 58.7 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 59.1ms ± 0.6ms 59.5ms ± 0.7ms +0.8% ✅ within noise 45.0 MB → 45.2 MB (+0.4%)
investment 86.8ms ± 0.9ms 85.8ms ± 0.9ms -1.1% ✅ within noise 47.0 MB → 47.0 MB (-0.0%)
fifo 86.1ms ± 0.7ms 86.2ms ± 1.0ms +0.2% ✅ within noise 49.1 MB → 49.0 MB (-0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.4ms, investment 0.6ms, fifo 0.5ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.70130% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-core/src/display_context.rs 98.70% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen force-pushed the fix/display-precision-sampling branch from 5df3347 to ed4bc07 Compare November 27, 2026 16:56
robcohen and others added 2 commits November 27, 2026 18:03
A compound cost {143.3998 # 4.61 USD} was sampled as a single 0 (its total
fell through to a zero default), a 0 dp sample nobody wrote, where
beancount samples 143.3998 and 4.61; and a balance tolerance
(4773.26 ~ 0.05 USD) was sampled beside its amount, where beancount
samples the amount only. Either could move a currency most common
precision, which queries and reports render amounts with. A component
left out of a compound cost is still not sampled.

Found by the semantic differential (scripts/compat-semantic-parity.py),
where it was the largest category.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/display-precision-sampling branch from ed4bc07 to e17c353 Compare November 28, 2026 00:06
@robcohen
robcohen merged commit f33a4bb into main Nov 28, 2026
55 of 56 checks passed
@robcohen
robcohen deleted the fix/display-precision-sampling branch November 28, 2026 00:23
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