Repository navigation
[perf] Skip unused-import name resolution when the node has no use statement - #8701
Merged
Merged
Conversation
…atement UnusedImportRemovingPostRector::enterNode() fires for both the FileNode root and the inner Namespace_. To find which imports are unused it resolves every referenced name via two full-AST traversals (findNonUseImportNames + findNamesInDocBlocks), and that resolution is not cached. In a namespaced file the `use` statements live inside the Namespace_, so the FileNode-level call did the full double walk and then found no `use` statement to act on - pure waste. Add a cheap guard: if the node has no direct `use` statement there is nothing to remove, so return before the resolution walks. Behavior is unchanged (non-namespaced files keep their `use` at FileNode level and still resolve). Adds bench/unused_import_removing_bench.php.
This was referenced Oct 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
To drop unused
useimports,UnusedImportRemovingPostRector::enterNode()resolves every name referenced in the file via two full-AST traversals (findNonUseImportNames()+findNamesInDocBlocks()), and that resolution is not cached.enterNode()fires for both theFileNoderoot and the innerNamespace_. In a namespaced file theusestatements live inside theNamespace_, so theFileNode-level call did the full double walk and then found nousestatement to act on - pure wasted work, on every post-rector pass.What
Add a cheap guard: if the node has no direct
usestatement, there is nothing to remove, so return before the resolution walks. Behavior is unchanged - non-namespaced files keep theiruseatFileNodelevel and still resolve.Bench
Adds
bench/unused_import_removing_bench.php, which drives the realenterNode()on the root nodes a file actually hits (FileNode + Namespace_). Measured onbench/Source/ManyNamesSource.php(namespaced, all imports used), 40 repeats x 400 enters:~2x on a namespaced file - the wasted FileNode-level double walk is gone.
Tests
rules-tests/CodingStyle,tests/Issues(incl.NamespacedUse,NoNamespaced,NamespacedUseAutoImport), and the Import/UnusedImport filters all green. ECS and PHPStan level 8 clean.