Boxing optional structs in Rust cut a 895MB program down to 420MB
A Rust developer deserializing thousands of AWS Smithy JSON shape files into nested structs found the in-memory representation ballooning to 895MB. The cause was a quirk of Rust struct layout: an Option
The fix was to wrap optional sub-structs in Box, turning Option
The trade-off: deserialization does slightly more CPU work since each candidate struct is built before being discarded, and heavy use of Box fragments the heap. In this case the overall job ran faster anyway, because reducing memory pressure outweighed the per-object cost. The post is a useful reminder that idiomatic Rust struct composition has very different memory characteristics from GC’d languages, and that data-shape analysis matters when deserializing large schemas.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.