Skip to content

Don't deep-clone the generated impl in builtin derives [-0.02%, below significance] - #66

Draft
xmakro wants to merge 1 commit into
perf/base-0713from
perf/derive-avoid-impl-clone
Draft

Don't deep-clone the generated impl in builtin derives [-0.02%, below significance]#66
xmakro wants to merge 1 commit into
perf/base-0713from
perf/derive-avoid-impl-clone

Conversation

@xmakro

@xmakro xmakro commented Jul 28, 2026

Copy link
Copy Markdown
Owner

Attaching the forwarded lint attributes cloned the entire freshly generated derive impl — every generated method body, which for a Debug derive on a wide struct is substantial AST — purely to override the attrs field of an item already owned. It also cloned the attribute list twice, the second copy being discarded immediately by the struct-update syntax. This fires for every #[derive] on every type.

Equivalence: attribute order and content are identical (own attrs first, then the filtered forwarded ones). ast::Item's other fields are copied identically — the item comes from cx.item, which hardcodes DUMMY_NODE_ID and tokens: None, so no fresh NodeId was ever being minted and there is no token cache to desync.

Honest measurement: -0.02% geomean, zero significant cells. Algorithmically strictly less work, but not visible on this benchmark set. Included for the reduction in allocation and AST copying rather than the number.

Measured in isolation against a clean base (ThinLTO stage2, glibc, instructions:u, full scenario, 5 crates x 3 profiles = 15 cells). This change was measured alone, not as part of a batch.

Reviewed adversarially before submission; findings that survived are reflected in the code and described above.

Attaching the forwarded lint attributes cloned the entire freshly
generated derive impl -- every generated method body -- just to override
the attrs field of an item we already own, and cloned the attribute list
twice in the process. Extend the attributes in place instead.

Attribute order and content are unchanged, and ast::Item's remaining
fields are copied identically (the item comes from cx.item, which
hardcodes DUMMY_NODE_ID and tokens: None).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant