Reduce the fixed memory cost of each Fluent `Localization`
Categories
(Core :: Internationalization, enhancement)
Tracking
()
People
(Reporter: flod, Unassigned)
References
Details
While measuring the memory cost of Fluent DOM messages in bug 2088603, a DMD (Firefox's heap profiler) run showed that each Localization allocates an 8 KB block the first time it formats a message, and that block is mostly unused.
The allocation comes from the bundle cache in fluent-fallback (Cache in cache.rs), which stores bundles in a ChunkyVec. ChunkyVec allocates chunks of DEFAULT_CAPACITY = 32 elements: 6,400 bytes requested (~200 B per bundle), rounded up to an 8 KB jemalloc block. A Localization normally uses one bundle (en-US) or two (locale + en-US fallback), so roughly 7 KB per Localization is wasted.
Stack from DMD:
chunky_vec::ChunkyVec<T>::push_get
fluent_fallback::bundles::Bundles<G>::format_value_from_iter
fluent_fallback::bundles::Bundles<G>::format_value_sync
localization_ffi::LocalizationRc::format_value_sync
mozilla::intl::Localization::FormatValueSync
This affects every Localization in every process: chrome documents in the parent, and content processes. For comparison, the rest of a Localization's fixed cost is about 1 KB.
Possible solutions:
- Make the first chunk small (e.g. 1–2 elements) and grow later chunks geometrically up to 32.
ChunkyVecneeds elements to stay in place once added (the cache hands out references), so growing chunks keeps that guarantee, while a plainVecwould not. - Let
fluent-fallbackchoose the chunk capacity, since it knows the length of the locale list. - Drop dependency on
chunky-vec(looks like a personal create with one commit?) and use native Vec.
Both crates are vendored from upstream (fluent-fallback from projectfluent/fluent-rs, chunky-vec from djg/chunky-vec).
| Reporter | ||
Comment 1•18 days ago
|
||
This was also filed as https://github.com/projectfluent/fluent-rs/issues/407
| Reporter | ||
Comment 2•5 days ago
|
||
Did some more digging. While it's worth fixing, since it removes an unnecessary dependency and still reduces memory usage, the overall gain is very limited.
The patch saves about 7.9 KiB per formatting Localization. That's 111 KiB in the parent process and 24 KiB in privilegedabout, but only 0.04% of the parent's heap. All six runs are valid and consistent between runs.
Baseline: Fluent's memory vs. the whole heap (en-US build, six tabs, 60 s wait, minimized memory; all six runs are within about ±10 KiB)
┌─────────────────────────┬──────────────────┬────────────────────┬───────────────────────┬──────────────────────────┐
│ Process │ Total heap (DMD) │ Fluent, all frames │ fluent-rs crates only │ Bundle cache (ChunkyVec) │
├─────────────────────────┼──────────────────┼────────────────────┼───────────────────────┼──────────────────────────┤
│ Parent │ 270 MiB │ 7.96 MiB (2.95%) │ 6.30 MiB (2.3%) │ 113.3 KiB, 14 caches │
├─────────────────────────┼──────────────────┼────────────────────┼───────────────────────┼──────────────────────────┤
│ privilegedabout │ 31.4 MiB │ 1.55 MiB (4.95%) │ 1.43 MiB (4.5%) │ 24.3 KiB, 3 caches │
├─────────────────────────┼──────────────────┼────────────────────┼───────────────────────┼──────────────────────────┤
│ file content │ 6.1 MiB │ 39.5 KiB (0.6%) │ 30.8 KiB │ 8.1 KiB, 1 cache │
├─────────────────────────┼──────────────────┼────────────────────┼───────────────────────┼──────────────────────────┤
│ extension, RDD, Utility │ — │ ≈ 0 │ ≈ 0 │ 0 │
└─────────────────────────┴──────────────────┴────────────────────┴───────────────────────┴──────────────────────────┘
More than 70% of Fluent's memory is parsed FTL data (the get_pattern, parse_runtime, add_resource and get_attributes frames). The "all frames" column also counts translated strings the DOM ends up owning. The bundle cache is only about 1.4% of Fluent's memory in the parent.
Patched (ChunkyVec → Vec<Box<T>> in fluent-fallback): fluent-rs memory, 3 runs each
┌─────────────────┬─────────────┬─────────────┬────────────────────┐
│ Process │ Baseline │ Patched │ Change │
├─────────────────┼─────────────┼─────────────┼────────────────────┤
│ Parent │ 6,451.4 KiB │ 6,340.5 KiB │ −110.9 KiB (−1.7%) │
├─────────────────┼─────────────┼─────────────┼────────────────────┤
│ privilegedabout │ 1,462.4 KiB │ 1,438.3 KiB │ −24.1 KiB (−1.6%) │
├─────────────────┼─────────────┼─────────────┼────────────────────┤
│ file content │ 30.8 KiB │ 22.9 KiB │ −7.9 KiB (−26%) │
└─────────────────┴─────────────┴─────────────┴────────────────────┘
Description
•