diff --git a/bench/node_name_resolver_is_string_name_bench.php b/bench/node_name_resolver_is_string_name_bench.php new file mode 100644 index 00000000000..db0d8a31663 --- /dev/null +++ b/bench/node_name_resolver_is_string_name_bench.php @@ -0,0 +1,61 @@ +create(); +$rectorConfig->boot(); + +/** @var NodeNameResolver $nodeNameResolver */ +$nodeNameResolver = $rectorConfig->make(NodeNameResolver::class); + +// non-matching, wildcard-free pairs - the hot fall-through path to the guard +$resolved = 'App\\Service\\SomeLongClassName'; +$desired = 'App\\Service\\OtherLongClassName'; + +$cpuSeconds = static function (): float { + $usage = getrusage(); + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; +}; + +$run = static function () use ($nodeNameResolver, $resolved, $desired): void { + for ($i = 0; $i < CALLS; ++$i) { + $nodeNameResolver->isStringName($resolved, $desired); + } +}; + +$run(); // warm up, excluded + +$cpuStart = $cpuSeconds(); +$wallStart = hrtime(true); +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { + $run(); +} + +printf('NodeNameResolver::isStringName benchmark%s', PHP_EOL); +printf(' calls=%d repeat=%d%s', CALLS, REPEAT, PHP_EOL); +printf('%s', PHP_EOL); +printf(' isStringName cpu=%.4fs wall=%.4fs%s', $cpuSeconds() - $cpuStart, (hrtime(true) - $wallStart) / 1_000_000_000, PHP_EOL); diff --git a/src/NodeNameResolver/NodeNameResolver.php b/src/NodeNameResolver/NodeNameResolver.php index 90e5158b20e..1eb49fdd4e5 100644 --- a/src/NodeNameResolver/NodeNameResolver.php +++ b/src/NodeNameResolver/NodeNameResolver.php @@ -34,7 +34,7 @@ final class NodeNameResolver /** * Used to check if a string might contain a regex or fnmatch pattern */ - private const array REGEX_WILDCARD_CHARS = ['*', '#', '~', '/']; + private const string REGEX_WILDCARD_CHARS = '*#~/'; /** * @var array @@ -181,12 +181,10 @@ public function isStringName(string $resolvedName, string $desiredName): bool return true; } - foreach (self::REGEX_WILDCARD_CHARS as $char) { - if (str_contains($desiredName, $char)) { - throw new ShouldNotHappenException( - 'Matching of regular expressions is no longer supported. Use $this->getName() and compare with e.g. str_ends_with() or str_starts_with() instead.' - ); - } + if (strpbrk($desiredName, self::REGEX_WILDCARD_CHARS) !== false) { + throw new ShouldNotHappenException( + 'Matching of regular expressions is no longer supported. Use $this->getName() and compare with e.g. str_ends_with() or str_starts_with() instead.' + ); } return false;