A Rust developer reduced memory usage from 895 MB to 420 MB by boxing optional structs during JSON deserialization. The key insight is that `Option<BigStruct>` in Rust still occupies the full size of `BigStruct` even when `None`, unlike reference-based languages like Java or Python. The fix involves changing `Option<SmithyReference>` fields to `Option<Box<SmithyReference>>` and implementing a custom Serde deserializer that discards empty structs instead of storing them. Memory measurement was done using jemalloc via a Cargo feature flag. The post also notes that while boxing increases heap fragmentation and adds CPU overhead for deserialization, the overall performance improved due to reduced memory pressure.

7m read timeFrom dystroy.org
Post cover image
Table of contents
The real use caseAbout rust structs and memoryThe changes that recovered the memoryVerification: Proving the ImpactConclusion: what's to remember, in a few words
2 Comments

Sort: