Skip to content

[perf] Add NoLongInstanceofChainRule + scope expr dispatch bench - #8790

Open
TomasVotruba wants to merge 3 commits into
mainfrom
tv-instanceof-chain-rule
Open

TomasVotruba wants to merge 3 commits into
mainfrom
tv-instanceof-chain-rule

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Combines two related pieces of work into one branch.

1. NoLongInstanceofChainRule

A single boolean chain built from many instanceof checks 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 7 instanceof checks, suggesting extraction to a helper method, a match(), 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.neon for now, to refactor in follow-ups:

  • src/NodeAnalyzer/ExprAnalyzer.php (x2)
  • src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
  • src/PhpParser/Node/NodeFactory.php

2. bench/scope_expr_dispatch_bench.php

Compares the long instanceof chain in PHPStanNodeScopeResolver against property_exists($node, 'expr'). They are not equivalent: property_exists also matches Assign/AssignOp/AssignRef (handled by the dedicated processAssign branch right below), plus ArrowFunction and Foreach_, 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-check is green.

@TomasVotruba
TomasVotruba force-pushed the tv-instanceof-chain-rule branch from eb18a69 to 99d5bef Compare October 23, 2026 20:34
@TomasVotruba TomasVotruba changed the title Add NoLongInstanceofChainRule: forbid more than 7 instanceof in one boolean chain Add NoLongInstanceofChainRule + scope expr dispatch bench Oct 23, 2026
@TomasVotruba TomasVotruba changed the title Add NoLongInstanceofChainRule + scope expr dispatch bench [perf] Add NoLongInstanceofChainRule + scope expr dispatch bench Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants