Skip to content

[perf] Match array_key_first/last in a single statement traversal - #416

Merged
TomasVotruba merged 1 commit into
mainfrom
perf-downgrade-array-key-first-last-single-traversal
Oct 21, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
perf-downgrade-array-key-first-last-single-traversal

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

What

DowngradeArrayKeyFirstLastRector::refactor() ran ExprInTopStmtMatcher::match() twice per statement - one full subtree traversal to find array_key_first(), then a second full traversal for array_key_last(). For the overwhelming majority of statements (which contain neither) that is two full traversals of the expression tree where one suffices.

This matches either name in a single traversal and dispatches on the matched name. array_key_first keeps priority (checked first on the matched node), so behaviour is unchanged.

Numbers

Micro bench (bench/array_key_first_last_single_traversal_bench.php, shipped here), refactor() over 300 statements containing neither call, 200 repeats, CPU (user+sys):

us/call
before (two traversals) ~59.1
after (one traversal) ~40.5

~1.46x on the common path. The rule's own test (15 cases) stays green.

@TomasVotruba
TomasVotruba merged commit 471c4e6 into main Oct 21, 2026
9 checks passed
@TomasVotruba
TomasVotruba deleted the perf-downgrade-array-key-first-last-single-traversal branch October 21, 2026 08:24
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