
Rust's derived Debug code added about 160KB to uv in one case
Rust currently adds inline hints to code generated by several derive macros. In one case, a large Debug implementation contributed enough repeated code for the uv project to cut about 160KB.
Inline hints in Rust derive output
Rust developers commonly use #[derive(...)] to implement core traits such as Debug, Display and Clone. The author discovered that Rust currently emits #[inline] as part of these derivations.
This behavior is not guaranteed, but it is implied by an example in the reference and appears when macros are expanded. In most cases, the generated hint is desirable because derived implementations are often small and can benefit from being inlined. The attribute remains a hint rather than a command.
Why nested errors can expand code
Large implementations do not always benefit from inlining. A hierarchy containing three error structures and an error enum produced a substantial amount of code for their derived Debug implementations. Much of that code could be inlined each time the implementation for the top-level enum was invoked.
Such invocations can occur repeatedly during debug or trace logging. The example hierarchy was described as drastically simplified, since real-world Rust applications often contain deeply nested error enums with nontrivial numbers of fields. The resulting inlined code can represent a nontrivial portion of a Rust binary's total size.
uv binary size reduction
The author and team found that they could shrink the uv binary by approximately 160KB by preventing rustc from inlining a given Debug implementation. The result was surprising because the size cost accumulated quickly and rustc did not appear to limit the size or number of times the implementation was inlined.
To achieve the reduction, the team added its own procedural macro that behaved like derive(Debug) but applied #[inline(never)]. The case demonstrates how generated inline hints can affect binary size when a derived implementation is used through a deeply nested type.
SiTech — AI-powered web development
We build fast, modern websites and bring AI into real business workflows. Have a project or a question? We'd love to help.