Repository navigation
[perf] Add NoLongInstanceofChainRule + scope expr dispatch bench - #8790
Open
TomasVotruba wants to merge 3 commits into
Open
TomasVotruba wants to merge 3 commits into
TomasVotruba wants to merge 3 commits into
Conversation
TomasVotruba
force-pushed
the
tv-instanceof-chain-rule
branch
from
October 23, 2026 20:34
eb18a69 to
99d5bef
Compare
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.
Combines two related pieces of work into one branch.
1. NoLongInstanceofChainRule
A single boolean chain built from many
instanceofchecks is a type dispatch in disguise - past a handful of branches it is slower than a keyed lookup and harder to read.Custom PHPStan rule (
utils/phpstan) that flags any boolean chain (||/&&) with more than 7instanceofchecks, suggesting extraction to a helper method, amatch(), or a keyed lookup. Each chain is reported once (inner-most offending sub-chain), independent of PHPStan's post-order visit. Threshold is a single const (MAX_INSTANCEOF_COUNT = 7).4 existing chains exceed the limit; ignored in
phpstan.neonfor now, to refactor in follow-ups:src/NodeAnalyzer/ExprAnalyzer.php(x2)src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.phpsrc/PhpParser/Node/NodeFactory.php2. bench/scope_expr_dispatch_bench.php
Compares the long instanceof chain in
PHPStanNodeScopeResolveragainstproperty_exists($node, 'expr'). They are not equivalent:property_existsalso matchesAssign/AssignOp/AssignRef(handled by the dedicatedprocessAssignbranch right below), plusArrowFunctionandForeach_, so it cannot be swapped in. The shorter form is ~3.7-4x cheaper per call, but moot given the divergence. The bench prints the divergent node set and the CPU ratio.composer complete-checkis green.