Skip to content

perf(fluent-fallback): stop over-allocating the bundle cache - #408

Open
Goldloli wants to merge 1 commit into
projectfluent:mainfrom
Goldloli:fix/bundle-cache-overallocation
Open

Goldloli wants to merge 1 commit into
projectfluent:mainfrom
Goldloli:fix/bundle-cache-overallocation

Conversation

@Goldloli

@Goldloli Goldloli commented Nov 6, 2026

Copy link
Copy Markdown

Fixes #407

Summary

Each Localization used to allocate an ~8 KB block the first time it formatted a message, because the bundle cache (Cache/AsyncCache in fluent-fallback) stores bundles in a ChunkyVec whose first chunk reserves DEFAULT_CAPACITY = 32 elements (~6.4 KB requested for a ~200 B bundle). A Localization normally caches one bundle (en-US) or two (locale + en-US fallback), so most of that block is never used.

This stores each cached bundle behind its own Box in a plain Vec:

  • References handed out by Cache::get/Cache::push_get stay valid regardless of how the Vec grows, because a Box never moves its contents — this keeps the pin-safety guarantee that motivated ChunkyVec, addressing the caveat in the third option of the issue.
  • The cache now grows one element at a time, so the fixed cost for one or two bundles is a couple of small allocations instead of one 8 KB block.
  • The chunky-vec dependency is dropped (options 1 and 2 would have required changes to that crate, whose API has no way to pick a chunk capacity).

Verification

  • cargo test -p fluent-fallback — 3 unit + 9 integration + 2 doc tests pass.
    • New caching_few_bundles_avoids_chunk_sized_allocations counts allocations in the 6–7 KB band while caching two bundle-sized (200 B) elements via a #[global_allocator]: it fails on main with exactly one 6400-byte request (32 × 200) and passes with this change; run 10× without flakes.
    • New references_stay_stable_across_growth keeps references from push_get across further growth and reads them afterwards, pinning the address-stability guarantee.
  • cargo test --workspace — pass.
  • cargo fmt --all -- --check — clean.
  • cargo clippy --all-features -- -Dwarnings — no new warnings from this change (cache.rs is clean; the remaining must_use error from async_trait on current stable reproduces identically on main).

The bundle cache stored items in a ChunkyVec whose first chunk
reserves 32 elements (~6.4 KB for a bundle). A Localization usually
caches one or two bundles, so most of that block is never used.

Store each cached bundle behind its own Box in a plain Vec instead:
references handed out by the cache stay valid because a Box never
moves its contents, which keeps ChunkyVec's pin-safety guarantee
while the storage now grows one element at a time. This also drops
the chunky-vec dependency.

Fixes projectfluent#407
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.

Unnecessary memory usage for Fluent Localization

1 participant