perf: Group NameResolver::enterNode() checks by node category by samsonasik · Pull Request #1168 · nikic/PHP-Parser · GitHub
Skip to content

perf: Group NameResolver::enterNode() checks by node category - #1168

Merged
nikic merged 2 commits into
nikic:masterfrom
samsonasik:perf-by-category-name
Sep 26, 2026
Merged

nikic merged 2 commits into
nikic:masterfrom
samsonasik:perf-by-category-name

Conversation

@samsonasik

Copy link
Copy Markdown
Contributor

NameResolver::enterNode() ran every node through a single chain of ~30 instanceof checks

This PR checks Stmt / Expr first and hands off to resolveStmt() / resolveExpr(). The branch bodies are unchanged. Nodes that are neither Stmt nor Expr (~37% of nodes) now exit after 3 checks.

Tested on php-parser vendor, laravel framework, it shows faster:

Corpus Nodes visited enterNode() gain (function-level)
PHP-Parser vendor 195,000 48.49 → 28.50 ms (~41%)
laravel-framework 10,242,600 2326.90 → 1224.39 ms (~47%)

@samsonasik

Copy link
Copy Markdown
Contributor Author

Ready to merge 👍

Comment thread lib/PhpParser/NodeVisitor/NameResolver.php Outdated

@nikic nikic left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we rely on subclassing in this code, so I think an even more efficient way would be to do a switch over $node->getType()? That would dispatch in a single check instead of chain.

@samsonasik

Copy link
Copy Markdown
Contributor Author

@nikic ok, updated to use node->getType(), seems faster:

Corpus Nodes visited enterNode() gain (function-level)
PHP-Parser vendor 195,000 49.92 → 22.18 ms (~56%)
laravel-framework 10,242,600 2333.47 → 866.15 ms (~63%)

@samsonasik
samsonasik requested a review from nikic September 26, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants