Rust's derive often implies inline
In Rust, the #[derive] attribute is frequently used to implement core traits such as Debug, Display, and Clone. However, developers may not be aware that Rust automatically adds #[inline] to these derived implementations. This feature, while often beneficial, can sometimes lead to unintended consequences.
Consider an error hierarchy, where each Error level inherits from a Debug implementation. In this case, the Debug implementation for Errors can be inlined numerous times, potentially causing the binary size to increase significantly. In one instance, preventing Rust from inlining these Debug implementations resulted in a 160KB reduction in binary size. This was surprising, as it showed the extent to which inlining can impact binary size.
The issue appears to be that Rust doesn't impose any limits on the number or size of inlined Debug implementations, which can be problematic in larger, more complex Rust applications. While it's likely that rustc made the right decision in many cases, developers should be aware of the potential size implications when dealing with deeply nested error hierarchies containing numerous fields.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.