Skip to content

[perf] Add scope expr dispatch bench (instanceof chain vs property_exists) - #8790

Closed
TomasVotruba wants to merge 1 commit into
mainfrom
tv-instanceof-chain-rule
Closed

TomasVotruba wants to merge 1 commit 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
@TomasVotruba
TomasVotruba force-pushed the tv-instanceof-chain-rule branch from 99d5bef to 18f980c Compare October 28, 2026 08:59
@TomasVotruba
TomasVotruba force-pushed the tv-instanceof-chain-rule branch from 18f980c to 9d72fb5 Compare October 28, 2026 09:00
@TomasVotruba TomasVotruba changed the title [perf] Add NoLongInstanceofChainRule + scope expr dispatch bench [perf] Add scope expr dispatch bench (instanceof chain vs property_exists) Oct 28, 2026
@TomasVotruba

Copy link
Copy Markdown
Member Author

Not the bottleneck really, closing

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.

1 participant