A real-world Rust optimization that reduced memory usage from 895 MB to ~435 MB by boxing optional structs. The key insight: unlike Java/Python where a null reference costs only one pointer, a Rust `Option<BigStruct>` still occupies the full size of `BigStruct` even when `None`. The fix is to use `Option<Box<BigStruct>>` so a `None` costs only one word. The post walks through the struct layout math, shows a custom Serde deserializer that discards empty structs instead of storing them, and demonstrates how to measure heap usage with jemalloc via a Cargo feature flag.

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
6 Comments

Sort: