Skip to content

[perf] Replace isStringName wildcard loop with single strpbrk scan - #8733

Merged
TomasVotruba merged 1 commit into
mainfrom
tv-isstringname-strpbrk
Oct 20, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
tv-isstringname-strpbrk

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

On a non-matching name - the common case, since a rule tests isName() against many candidates and most fail - isStringName() falls through to a guard that rejects leftover regex/fnmatch patterns. It scanned the needle once per wildcard char via a foreach + str_contains; a single strpbrk() over the same char set does it in one C-level pass.

Behaviour is identical: strpbrk() returns non-false when any of * # ~ / is present, exactly when the old loop threw. The array const became a string const (its only use was the loop). An in-process profile ranked isStringName at ~32M self cost over 164k calls - the busiest Rector-owned function in the run.

Micro A/B (bench/node_name_resolver_is_string_name_bench.php, 200k calls x 60 repeats, PHP 8.4, CPU user+sys):

  • before: cpu 6.96s
  • after: cpu 4.39s (~37% faster on the fall-through path)

Run on both branches to compare:

php bench/node_name_resolver_is_string_name_bench.php

@TomasVotruba
TomasVotruba merged commit 6809b38 into main Oct 20, 2026
49 checks passed
@TomasVotruba
TomasVotruba deleted the tv-isstringname-strpbrk branch October 20, 2026 16:45
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