Repository navigation
Cache file-level invariants per file in AbstractRector::enterNode() - #8558
Closed
TomasVotruba wants to merge 1 commit into
Closed
TomasVotruba wants to merge 1 commit into
TomasVotruba wants to merge 1 commit into
Conversation
TomasVotruba
force-pushed
the
worktree-enternode-file-hoist
branch
from
October 9, 2026 07:00
24964e2 to
0115e7b
Compare
TomasVotruba
marked this pull request as draft
October 9, 2026 07:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proof of concept
AbstractRector::enterNode()runs for every node that matches a rule'sgetNodeTypes()(the traverser already filters by node type viagetVisitorsForNode()). On each call it recomputed values that are constant for the whole file:getFile()->getFilePath()is_a($this, HTMLAverseRectorInterface)+File::containsHTML()Skipper::matchSkip($this, $filePath)(depends only on rule + file path, never on the node)For a file with thousands of nodes and many applicable rules, these ran per node x rule although nothing changed within the file.
This PR caches them once per file (keyed on the
Fileinstance) and leaves only the genuinely per-node work in the hot path:shouldSkipCurrentNode(), the clone probe on a skip match,refactor(), and post-processing.Behavior
No behavior change.
matchSkip, HTMLAverse and file path are all file-level invariants, so caching them per file is equivalent. Core test suite green (the only failures are the pre-existingtests/Bin/RectorTestsubprocess cases, unrelated).Benchmark - honest result
Interleaved A/B, cold cache each run, single process, broad rule set (all PHP sets + deadCode + codeQuality + codingStyle + typeDeclarations) over
src/:Average ~ -1%, and the sign flips between rounds. The saving is real but within measurement noise at the full-run level: the hoisted calls are cheap next to parsing, PHPStan scope resolution,
refactor()bodies and printing, andmatchSkip'sresolve()is already memoized.Opening as a proof of concept. The refactor is clean and behavior-neutral; the question is whether the per-node cleanup is worth keeping on its own merit given the macro benchmark is flat.