Skip to content

mbe: cache metavar resolution across repetition iterations [tt-muncher -2.2%] - #82

Draft
xmakro wants to merge 6 commits into
perf/flat-token-cursorfrom
perf/mbe-lookup-cache
Draft

mbe: cache metavar resolution across repetition iterations [tt-muncher -2.2%]#82
xmakro wants to merge 6 commits into
perf/flat-token-cursorfrom
perf/mbe-lookup-cache

Conversation

@xmakro

@xmakro xmakro commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Follow-up to #57. During transcription, resolving a metavar occurrence called lookup_cur_matched, which does an interpolation-map lookup plus a walk over every repetition level, once per occurrence per iteration of the innermost repetition frame. Everything except the innermost repetition index is invariant for the lifetime of that frame.

This caches the resolution prefix (all levels but the innermost) in a small per-frame cache keyed by normalized ident, and applies only the innermost index per occurrence. The cache is cleared whenever the repetition stack changes depth, and capped at 8 entries so lookups stay a short linear scan even for pathological rule bodies.

Performance (ThinLTO stage2, jemalloc, instructions:u, full scenario, measured on top of the perf stack; A/B over 10 crates x Check/Debug):

  • tt-muncher check -2.23%, debug -1.56%
  • 0 regressed cells; all real-world crates within noise (their sequences do not iterate enough per frame for the cache to matter)

Correctness: stage2 build green, x check compiler green on this branch standalone, tests/ui macros subset 510 passed / 0 failed.

The logic is independent of the flat token buffer and would apply to upstream transcribe.rs as well; it is stacked on #57 here because both touch the same function.

xmakro added 6 commits August 1, 2026 08:43
The parser consumes tokens from a pre-flattened entry buffer: advancing
is an index increment, lookahead is direct indexing, and parser
snapshots clone two Arcs instead of a cursor stack.
Lexing a source file now produces the flat entry buffer without
building a token tree first. The few cold callers that still need a
TokenStream rebuild it from the buffer.
mbe transcription writes open/close entries and match indices through
FlatSink. Captured tt fragments splice as buffer slices instead of
being rebuilt as trees and re-flattened on every expansion.
Macro invocation arguments flow to the matcher as views of the source
buffer, materialized to trees only on first tree-level access. Debug
output is bounded to the viewed range and the pre-expansion
KeywordIdents lint scans flat entries, so neither defeats the laziness
by materializing every invocation in the crate.
The parser runs entirely on FlatTokenCursor, so the old tree-walking
TokenCursor and its TokenTreeCursor are unreferenced. Delete both and
retarget the stale break_up_float FIXME at the cursor that now exists.
Resolving a metavar occurrence during transcription looked up the
interpolation map and walked every repetition level on every iteration
of the innermost repetition frame, although everything but the
innermost index is invariant for the frame's lifetime. Cache the
resolution prefix per frame and apply only the innermost index per
occurrence. tt-muncher check instructions drop by 2.2%.
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