Box to Save Memory in Rust

(dystroy.org)

34 points | by emschwartz 3 days ago

2 comments

  • mstange 41 minutes ago
    Are there any tools that help finding these kinds of things? Like a profiler that says "80% of the allocated bytes are objects of this type, with 95% of those having that field set to None"
    • gizmo686 21 minutes ago
      The closest I am aware of is clippy (`cargo clippy` in a standard Rust project will run it with default configurations).

      Clippy is essentially a linter; and one of its checks catches cases where different enum variants have a significantly different size; with a suggestion to Box the larger variant.

      Since this is just a linter, it doesn't actually have any knowledge of how frequently each variant is actually used. It also doesn't address the situation in the article at all.

  • krautsauer 56 minutes ago
    Box<str> or Box<&str> might also have been usable routes. (example: https://play.rust-lang.org/?version=stable&mode=debug&editio... )
    • stebalien 46 minutes ago
      Box<str> is still two words (length and pointer). That's better than the 3 words (length, pointer, capacity) for strings, but Box<String> is one word (not including the heap allocation).