Skip to content

perf(macros): optimize hot paths and eliminate per-call heap allocations - #170

Open
lilith wants to merge 1 commit into
feat/neural-vision-roadmapfrom
perf/macro-hotpaths
Open

lilith wants to merge 1 commit into
feat/neural-vision-roadmapfrom
perf/macro-hotpaths

Conversation

@lilith

@lilith lilith commented Nov 14, 2026

Copy link
Copy Markdown
Member

Summary

Eliminates avoidable per-invocation AST reparsing, redundant token-tree walks, and short-lived heap allocations across archmage-macros (PR 1 of 3 in the compile-time optimization stack):

  • Fast-path attribute deduplication (drop_attrs_equal_to): Skip non-#[cfg] attributes (doc comments, #[inline], lint attributes) before formatting attribute tokens to String, and short-circuit immediately when the guard is empty or no #[cfg] attribute is present.
  • Guarded Self/self token walks: Gate replace_self_in_tokens and replace_self_value_in_tokens behind tokens_contain_ident in #[arcane(_self = Type)] and #[autoversion(in_trait, _self = Type)], and skip unrelated delimited groups inside replace_self_value_in_tokens.
  • Direct token-stream emission (no parse_quote! / syn::parse_str on hot paths): Emit #[inline], #[target_feature(enable = ...)], #[allow(...)], and ::archmage::* token paths directly into TokenStream (InlinePolicy::tokens, colon_path_tokens), inspect _self = Type via find_token_in_self_type without cloning syn::Signature + parse_quote!, and avoid cloning function bodies in attune proof wrappers.
  • Stack-first identifier & small-buffer paths: Replace Ident::to_string() keyword/registry dispatches and small per-call Vec buffers with stack-backed with_ident_str ([u8; 64]), 'static token/trait string slices, borrowed TokenTypeInfo<'a>, and safe inline SmallVec<T, N>.

Measured Impact (default opt-level = 0, 20-thread Linux x86_64)

Per-invocation allocations (expansion_tests::profile_allocations)

Macro shape Base allocs (bytes) PR allocs (bytes) Allocation / byte delta
#[arcane] 302 (15,525 B) 261 (12,080 B) −13.6% / −22.2%
#[rite] 231 (12,245 B) 182 (8,794 B) −21.2% / −28.2%
#[attune] direct family 393 (14,854 B) 299 (10,812 B) −23.9% / −27.2%
#[attune(all)] 1,237 (45,403 B) 942 (30,943 B) −23.8% / −31.8%
#[attune] composed 1,601 (57,214 B) 1,282 (40,952 B) −19.9% / −28.4%
#[magetypes] 1,859 (69,687 B) 1,700 (57,721 B) −8.6% / −17.2%
#[autoversion] 1,272 (55,933 B) 1,062 (40,890 B) −16.5% / −26.9%

Crate expansion (rustc -Ztime-passes, median of 5 runs)

Target Base macro_expand_crate PR macro_expand_crate Delta
magetypes --features "std avx512" 0.219 s 0.193 s −26 ms (−11.9%)
1,500 #[arcane] functions + 3,000 doc lines 0.219 s 0.193 s −26 ms (−11.9%)
400 #[attune(_*, dispatch)] families 0.456 s 0.378 s −78 ms (−17.1%)

Verification

  • cargo test -p archmage-macros --features avx512 (164 unit + raw_snapshots byte-exact expansion tests)
  • cargo test --features "std avx512" (all macrotest, trybuild compile-fail UI tests, soundness_exploits, attune_*, and magetypes suites)
  • cargo clippy -p archmage-macros --all-targets --features avx512 -- -D warnings
  • cargo run -p xtask -- validate-registry, validate, soundness

@lilith lilith self-assigned this Nov 14, 2026
@codecov-commenter

codecov-commenter commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.00%. Comparing base (a4eb0cb) to head (bf379d5).

Additional details and impacted files
@@                      Coverage Diff                       @@
##           feat/neural-vision-roadmap     #170      +/-   ##
==============================================================
- Coverage                       81.15%   79.00%   -2.16%     
==============================================================
  Files                               6        6              
  Lines                            1348     1348              
==============================================================
- Hits                             1094     1065      -29     
- Misses                            254      283      +29     
Flag Coverage Δ
aarch64 82.96% <ø> (ø)
x86_64 76.35% <ø> (-2.77%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Eliminate avoidable per-invocation AST reparsing, token-tree walks, and
short-lived heap allocations across archmage-macros:

- Skip non-#[cfg] attributes (such as doc comments and #[inline]) in
  drop_attrs_equal_to before formatting attribute tokens to String, and
  short-circuit when the guard is empty or no #[cfg] attribute exists.
- Guard Self/self token-stream rewrites in #[arcane(_self = Type)] and
  #[autoversion(in_trait, _self = Type)] with tokens_contain_ident, and
  skip unrelated group subtrees in replace_self_value_in_tokens.
- Inspect _self = Type directly via find_token_in_self_type and
  check_single_token_param instead of cloning syn::Signature and running
  parse_quote! on every receiver boundary.
- Emit #[inline], #[target_feature], #[allow(...)], and ::archmage::*
  token paths directly into TokenStream without parse_quote! or
  syn::parse_str, and avoid cloning function bodies in attune proof
  wrappers.
- Replace Ident::to_string() keyword/registry dispatches and small
  per-call Vec buffers with stack-backed with_ident_str ([u8; 64]),
  &'static str token/trait lookups, borrowed TokenTypeInfo<'a>, and
  safe inline SmallVec<T, N>.
@lilith
lilith force-pushed the perf/macro-hotpaths branch from f53a367 to bf379d5 Compare November 14, 2026 08:20

This branch has not been deployed

No deployments
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.

2 participants