Skip to content

perf(cli): mimalloc global allocator for the rledger binary (-7% load) - #1599

Merged
robcohen merged 3 commits into
mainfrom
perf/mimalloc-experiment
Jun 25, 2026
Merged

robcohen merged 3 commits into
mainfrom
perf/mimalloc-experiment

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Set mimalloc as the global allocator for the rledger binary.

Why

After the green-conversion campaign (#1594/#1595/#1596/#1598) cut the parser ~30%, a fresh cachegrind breakdown showed the allocator machinery is now the #1 cost (~14%) of the load — _int_malloc / free / malloc_consolidate / … The load → process pipeline makes thousands of small, short-lived allocations (green-tree build + AST conversion + booking); glibc malloc is a poor fit for that pattern.

Result

Measured on the 10k-txn workload (cachegrind, same harness, only the allocator differs):

glibc malloc = 740,408,953
mimalloc     = 688,729,631   (-7.0%, -51.7M instructions)

cachegrind counts instructions; mimalloc's wall-clock win is typically larger (thread-local heaps, cache locality, less fragmentation). And it's process-wide — every command (check / query / format) benefits, not just parse.

Design

  • Binary-only. #[global_allocator] lives in src/bin/rledger.rs, never the library — embedders pick their own allocator.
  • Not on wasm. Gated cfg(not(target_arch = "wasm32")) (mimalloc is a C lib).
  • Default-on, opt-out. New default mimalloc feature; --no-default-features drops it (e.g. a build host without a C toolchain).

Tradeoff

Adds libmimalloc-sys (a vendored C library) — build time, binary size, and a non-Rust dep for cargo deny. Pure-Rust allocators were evaluated and rejected: ferroc doesn't compile on rustc 1.96 (let-chains/edition) across all published versions, and talc v5 is an embedded/wasm-oriented rewrite with no std-heap drop-in. The root-cause pure-Rust fix (arena-allocating the conversion) is a larger follow-up.

Verification

rledger check on the workload: ✓ No errors found — output identical (allocator swap, no logic change).

🤖 Generated with Claude Code

Post-green-conversion, the cachegrind breakdown shows allocator machinery is
the #1 cost (~14%) of the load — the pipeline makes many small short-lived
allocations that glibc malloc handles poorly. Set mimalloc as the rledger
binary's global allocator: -7.0% instructions on the 10k-txn workload (likely
more wall-clock), process-wide so every command benefits.

Binary-only (never the library — embedders choose their own allocator), gated
off wasm (C lib), default-on via a new mimalloc feature (opt out with
--no-default-features). Pure-Rust allocators evaluated + rejected (ferroc won't
compile on the toolchain; talc v5 is embedded-oriented). rledger check smoke
test: identical output.
Copilot AI review requested due to automatic review settings June 25, 2026 20:05
@robcohen
robcohen requested a review from a team as a code owner June 25, 2026 20:05
@github-actions

github-actions Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.5ms ± 0.7ms 23.5 MB
beancount 900.3ms ± 3.3ms 50.7 MB
Comparison 26.1x faster 2.2x less

vs main branch (2026-06-25)

Metric main this PR Change
Time 37.4ms 34.5ms -7.8% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an optional mimalloc global allocator for the rledger CLI binary to reduce allocator overhead during the load → process pipeline.

Changes:

  • Add #[global_allocator] using mimalloc::MiMalloc in the rledger binary behind cfg(feature = "mimalloc") and not(wasm32).
  • Add a new mimalloc Cargo feature and dependency, and enable it via default features.
  • Update Cargo.lock for the new allocator dependency chain (mimalloc / libmimalloc-sys).

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/rustledger/src/bin/rledger.rs Installs mimalloc as the global allocator for the rledger binary under a feature + non-wasm cfg gate.
crates/rustledger/Cargo.toml Adds the mimalloc feature/dependency and enables it by default.
Cargo.lock Records the new allocator dependencies.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger/Cargo.toml Resolved
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

…umers

Review fix (Copilot): mimalloc was in the rustledger crate default features,
but the crate has a [lib] target published to crates.io — default features
apply to library consumers, forcing a vendored C dependency (+ C toolchain) on
every embedder. Move it out of default to an opt-in mimalloc feature; the
binary global allocator is still gated on it (+ not wasm). Release binaries
enable it explicitly (follow-up wires --features mimalloc into release-build.yml).
@robcohen

Copy link
Copy Markdown
Member Author

Good catch — fixed in 7083694. Moved mimalloc out of default features to an opt-in mimalloc feature, so it no longer pulls the vendored C dep into library consumers. Confirmed via cargo tree: 0 in the default tree, present only under --features mimalloc. Release binaries will enable it explicitly (a follow-up wires --features mimalloc into release-build.yml, alongside producing the ag-rledger artifact).

@robcohen
robcohen merged commit 66bb72d into main Jun 25, 2026
45 checks passed
@robcohen
robcohen deleted the perf/mimalloc-experiment branch June 25, 2026 21:14
robcohen added a commit that referenced this pull request Jun 25, 2026
…1601)

* ci(release): enable mimalloc in release rledger binaries (-7% load)

Follow-up to #1599 (mimalloc opt-in feature) + #1600. The release builds use
--no-default-features, so the opt-in feature wasn't reaching shipped binaries.
Add --features mimalloc to the rledger builds (PGO Unix/Windows + native
non-cross) so released binaries get the ~7% lower-load allocator win.

Cross-compiled targets are intentionally excluded: mimalloc's vendored C build
isn't validated under cross here, and a release shouldn't hinge on it — they
ship without the allocator. Feature resolution verified locally (cargo build
--no-default-features --features mimalloc resolves at the virtual workspace
root); rledger output is unchanged (allocator-only).

* ci(release): clarify mimalloc cross-vs-native comments (review)
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.

2 participants