RC RANDOM CHAOS

Boxing optional structs in Rust cut a 895MB program down to 420MB

· via Hacker News

Original source

Box to save memory in Rust

Hacker News →

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 field still occupies the full size of the inner struct, even when None. Because most optional fields in the data were absent, every parent struct was carrying hundreds of bytes of empty Option slots regardless.

The fix was to wrap optional sub-structs in Box, turning Option into Option<Box>. Thanks to Rust’s niche optimization, an absent boxed value collapses to a single pointer-sized word — mimicking how nullable object references behave in Java or Python. A custom serde deserializer detects when a sub-struct has all fields empty and stores None instead of allocating. Total memory dropped by 475MB.

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.