PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Add getVisitorsForNode() method to NodeTraverser

Open samsonasik opened this issue 2 months ago • 6 comments

@TomasVotruba here for your request https://github.com/rectorphp/rector-src/pull/6232#issuecomment-3423162697

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:

  • https://github.com/rectorphp/rector-src/pull/6232

samsonasik avatar Oct 20 '25 18:10 samsonasik

Ready for review 👍

samsonasik avatar Oct 20 '25 18:10 samsonasik

In the case of Rector, this was used to optimise the running time by 20-30%. Probably other tools could benefit from similar optimisations

carlos-granados avatar Oct 20 '25 19:10 carlos-granados

The 20-30 % speedup was really noticable across many mid-large size projects :+1: It would also open-up even more advanced speed improvements based on full node class -> visitor tree.

TomasVotruba avatar Oct 20 '25 19:10 TomasVotruba

It looks like adding this method makes NodeTraverser itself about 10% slower.

nikic avatar Oct 20 '25 20:10 nikic

@nikic could you share script to verify the benchmark? Thank you.

samsonasik avatar Oct 22 '25 01:10 samsonasik

@nikic I created benchmark repo for non-cached vs cached node visitor version

https://github.com/samsonasik/php-parser-with-cache-node-visitor-benchmark

which shows faster result on total time.

505803727-a1c139af-a4aa-429a-a1c1-676b2bb32bd4

samsonasik avatar Nov 06 '25 13:11 samsonasik