memoryallocationperformance

Boxing values that fit comfortably on the stack

Box<T> reached for as a default container instead of by need.

Why this looks tempting

Box turns lifetime puzzles into 'static, and the type signatures look cleaner.

Why wrong

Box<T> is a heap allocation and an indirection. For small values this is cache- unfriendly and slower than just owning the value. Box is for recursion, type erasure (dyn Trait), or moving large values without copying — not for ergonomics.

Fix

Own values directly until you have a measured reason to indirect: a recursive type (Box<Node>), a trait object (Box<dyn Error>), or a value too big to copy on the stack. Reach for Box deliberately, not reflexively.

Canonical alternative

performance/memory-optimization