Skip to content

Commit 50df18d

Browse files
lonnieezellclaudemichalsn
authored
perf: skip redundant work in RouteCollection::create() (#10656)
* perf(#10617): skip redundant work in `RouteCollection::create()` Run the placeholder `str_ireplace` loop only when the route key contains `:`, and call `helper('array')` only on the first `create()` per collection. The route table output is unchanged. Route table build, PHP 8.3, OPcache on, median of 7 runs: 100 routes 203 -> 168 µs, 1000 routes 2044 -> 1680 µs (~18%). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Added changelog * Update user_guide_src/source/changelogs/v4.8.0.rst Co-authored-by: Michal Sniatala <michal@sniatala.pl> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Michal Sniatala <michal@sniatala.pl>
1 parent 7326b53 commit 50df18d

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎system/Router/RouteCollection.php‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ class RouteCollection implements RouteCollectionInterface
271271
*/
272272
private ?string $httpHost = null;
273273

274+
/**
275+
* Whether create() has already loaded the array helper.
276+
*/
277+
private bool $arrayHelperLoaded = false;
278+
274279
/**
275280
* Flag to limit or not the routes with {locale} placeholder to App::$supportedLocales
276281
*/
@@ -1399,8 +1404,11 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
13991404

14001405
// Replace our regex pattern placeholders with the actual thing
14011406
// so that the Router doesn't need to know about any of this.
1402-
foreach ($this->placeholders as $tag => $pattern) {
1403-
$routeKey = str_ireplace(':' . $tag, $pattern, $routeKey);
1407+
// Without a ':' no placeholder can match, so skip the loop.
1408+
if (str_contains($routeKey, ':')) {
1409+
foreach ($this->placeholders as $tag => $pattern) {
1410+
$routeKey = str_ireplace(':' . $tag, $pattern, $routeKey);
1411+
}
14041412
}
14051413

14061414
// If is redirect, No processing
@@ -1417,7 +1425,11 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
14171425

14181426
$name = $options['as'] ?? $routeKey;
14191427

1420-
helper('array');
1428+
// Not used here; kept so apps relying on the helper being loaded keep working.
1429+
if (! $this->arrayHelperLoaded) {
1430+
helper('array');
1431+
$this->arrayHelperLoaded = true;
1432+
}
14211433

14221434
// Don't overwrite any existing 'froms' so that auto-discovered routes
14231435
// do not overwrite any app/Config/Routes settings. The app

‎tests/system/Router/RouteCollectionTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,50 @@ public function testAddReplacesCustomPlaceholders(): void
248248
$this->assertSame($expects, $routes);
249249
}
250250

251+
public function testAddWithoutPlaceholdersKeepsExactKey(): void
252+
{
253+
$routes = $this->getCollector();
254+
255+
$routes->add('users/profile-edit', 'controller');
256+
$routes->add('products/([a-z]+)/(\d+)', 'controller');
257+
258+
$expects = [
259+
'users/profile-edit' => '\controller',
260+
'products/([a-z]+)/(\d+)' => '\controller',
261+
];
262+
263+
$this->assertSame($expects, $routes->getRoutes());
264+
}
265+
266+
public function testAddReplacesCustomNonCapturingPlaceholderMixedWithDefaults(): void
267+
{
268+
$routes = $this->getCollector();
269+
$routes->addPlaceholder('slug', '(?:[a-z]+-)*[a-z]+');
270+
271+
$routes->add('blog/(:num)/(:slug)/(:segment)', 'controller');
272+
273+
$expects = [
274+
'blog/([0-9]+)/((?:[a-z]+-)*[a-z]+)/([^/]+)' => '\controller',
275+
];
276+
277+
$this->assertSame($expects, $routes->getRoutes());
278+
}
279+
280+
public function testAddReplacesPlaceholdersCaseInsensitivelyWithoutParentheses(): void
281+
{
282+
$routes = $this->getCollector();
283+
284+
$routes->add('users/(:NUM)', 'controller');
285+
$routes->add('posts/:segment', 'controller');
286+
287+
$expects = [
288+
'users/([0-9]+)' => '\controller',
289+
'posts/[^/]+' => '\controller',
290+
];
291+
292+
$this->assertSame($expects, $routes->getRoutes());
293+
}
294+
251295
public function testAddRecognizesCustomNamespaces(): void
252296
{
253297
$routes = $this->getCollector();

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ Changes
455455
*******
456456

457457
- **Config:** Added the ``md`` key for ``Config\Mimes::$mimes`` for Markdown files.
458+
- **Router:** Reduced route registration overhead in ``RouteCollection::create()`` by skipping placeholder replacement for route keys without ``:`` and calling ``helper('array')`` only once per collection.
458459

459460
************
460461
Deprecations

0 commit comments

Comments
 (0)