Repository navigation
perf(parser): pre-size lexer token vectors to kill reallocation churn (-10.8% heap) - #1587
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
Performance-focused change in rustledger-parser to reduce heap allocation churn during lexing/token stream construction by reserving capacity up front for the primary token vectors.
Changes:
- Pre-allocate capacity for the AST lexer token output vector in
tokenize_inner. - Pre-allocate capacity for the CST “lossless kind token” output vector in
lossless_kind_tokens.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/rustledger-parser/src/logos_lexer.rs | Pre-sizes the lexer output Vec to reduce reallocations during tokenization. |
| crates/rustledger-parser/src/cst/lossless_tokens.rs | Pre-sizes the CST lossless (SyntaxKind, Range) output Vec to reduce reallocations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📊 Benchmark Results
vs main branch (2026-06-25)
What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
|
Good catch — capped both reservations at |
dhat profiling (the new profiling data branch) ranked tokenize_inner and lossless_kind_tokens as the #1/#2 allocators -- both grew an unsized Vec via push, reallocating ~17x. Reserve Vec::with_capacity(source.len() / 4) (~4 bytes per beancount token). Measured on a 10k-txn workload: total heap allocated 158.7MB -> 141.6MB (-10.8%); tokenize_inner 17 allocations -> 1. Parser tests + clippy clean; allocation-only, behavior-preserving.
f5daf70 to
afb0745
Compare
What
Profiling-driven optimization. Pre-sizes the two lexer token vectors that the new nightly profiling (the
profilingdata branch) flagged as the largest allocations — eliminating reallocation churn.tokenize_inner(logos_lexer.rs) andlossless_kind_tokens(cst/lossless_tokens.rs) both built their outputVecfromVec::new()and grew viapush, reallocating ~17× as the geometric doubling kicked in. A beancount token averages ~4 bytes of source, soVec::with_capacity(source.len() / 4)reserves close to the final count up front.Measured impact (10k-txn workload, dhat + cachegrind)
tokenize_innerallocations(
lossless_kind_tokensdropped out of the top-4 allocators entirely.)Why it's safe
Allocation-only change — no behavioral difference.
cargo test -p rustledger-parserpasses; clippy-D warningsclean. The win will show up as a step down inheap_total_byteson the next nightlyprofilingtrend point.How it was found
This is the payoff of the profiling work (slices 1–4): the dhat heap profile ranked these two
Vec::new()sites #1 and #2 by bytes, and cachegrind confirmed ~12% of CPU was inmalloc/free. More targets remain (theTransaction::clone×2 per txn, the 190k-allocationwalk_descendants_once, happy-path error-scan allocations) — follow-ups.🤖 Generated with Claude Code