Repository navigation
perf(visitor): reuse arguments - faster schema validation - #297
Conversation
Reuse callback arguments and cache visitor indices. Skip action checks for None results while preserving traversal behavior. Co-Authored-By: Aiden <aiden@weco.ai>
Merging this PR will improve performance by 35%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing |
Cito
left a comment
There was a problem hiding this comment.
Thanks! Randomized ParallelVisitor runs gave results identical to main, and I can confirm the speedup: with this and #299 applied, about 48% less time on the empty parallel visitor benchmark and 10% on SDL validation. This will be part of 3.3.2.
TL;DR
ParallelVisitorenter and leave callbacks.None.PYTHONPATH=src python -m pytest tests -qpassed 3,613 tests; 18 skipped.Refs #296
What & Why
Each node visit rebuilt arguments and index pairs for every callback.
Build the argument tuple once per node callback and store indices with the cached handlers.
Check action values only when a callback returns a value.
The same handlers receive the same arguments in the same order.
Validation
These are median times from at least 50 rounds on CPython 3.12.13, Linux x86-64, and an AMD EPYC 9R14.
Both versions ran sequentially on the same isolated core.
Differences below 3% are treated as measurement variation.
The largest gain comes from empty callbacks; practical validation gains depend on the query and rules.
Single-visitor traversal and pickle decoding do not use
ParallelVisitor.Run on the base commit and this change:
Ruff and mypy passed for the changed module.
Why this is safe and easy to maintain
The small change stays in one method and uses ordinary tuples and loops.
It preserves output, edits, skip state, break state, and exception handling.
The existing
tests/language/test_visitor.pytests cover traversal arguments, replacements, removals, skips, stops, and parallel visitors.Scope
This changes Python callback dispatch without adding features, dependencies, or public API changes.
AI assistance
Codex gpt-6.1-sol generated candidates tracked with Weco Observe.
Codex also helped run automated checks and prepare this description.
Weco share link: https://dashboard.weco.ai/share/26ij8orE48QjRUY4w8ufQ9eLDNM9zuin