-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add getVisitorsForNode() method to NodeTraverser #1122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Ready for review 👍 |
|
In the case of Rector, this was used to optimise the running time by 20-30%. Probably other tools could benefit from similar optimisations |
|
The 20-30 % speedup was really noticable across many mid-large size projects 👍 |
lib/PhpParser/NodeTraverser.php
Outdated
| /** | ||
| * @return NodeVisitor[] | ||
| */ | ||
| public function getVisitorsForNode(Node $node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be protected and have array return type?
Would also be good to add a doc comment to explain that this is an extension point, as the method doesn't make a lot of sense in itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, updated to protected, add array return type, and add doc comment why it is needed.
|
It looks like adding this method makes NodeTraverser itself about 10% slower. |
|
@nikic could you share script to verify the benchmark? Thank you. |
@TomasVotruba here for your request rectorphp/rector-src#6232 (comment)
This is template config for
getVisitorsForNode(), which on rector use case, we use for caching visitor by node.https://github.com/rectorphp/rector-src/blob/4b30bc5d10e7c79867a6a13da3a75a1b2c844675/src/PhpParser/NodeTraverser/RectorNodeTraverser.php#L65-L82
so we can just override the method there :), we currently using vendor-patch for it:
https://github.com/rectorphp/vendor-patches/blob/18e174cbea7beaccb140cfcf03cca3097e95ee92/patches/nikic-php-parser-lib-phpparser-nodetraverser-php.patch
Original PR for this usage for caching on Rector side: