Skip to content

perf: skip redundant work in RouteCollection::create() during route registration #10617

Description

@lonnieezell

Problem

Without route caching, the route table is rebuilt on every request. Each $routes->get() (or post(), add(), and so on) runs RouteCollection::create(), which costs about 1.9 µs per route. That's about 8 times more than the worst-case cost of matching a route.

Two steps in create() do work that is usually wasted:

  • The placeholder loop. create() runs str_ireplace(':' . $tag, ...) once for every registered placeholder (6 by default), even when $from has no : and nothing can match. That costs about 0.50 µs per route.
  • helper('array'). It's called on every create(). RouteCollection no longer uses any array-helper function, but the call still loads the helper for the app as a side effect. Repeat calls cost about 0.24 µs per route.

Measurements

PHP 8.3 on macOS, OPcache on, production mode, warm. Config construction cost was turned off to approximate the state after #10615. The routes are a realistic mix: a third static, a third with one placeholder, a third with two, spread across groups.

Routes Build route table Match last route 404
30 78 µs 15 µs 11 µs
100 248 µs 32 µs 27 µs
300 608 µs 85 µs 79 µs
1000 2,030 µs 263 µs 252 µs

Here's the cost breakdown of one $routes->get() call:

Step Cost
esc(strip_tags($from)) 0.49 µs
Placeholder str_ireplace loop (6 placeholders) 0.50 µs
helper('array') 0.24 µs
Everything else ~0.6 µs
Total ~1.87 µs

Proposal

  1. In create(), run the placeholder loop only when the route key contains :. If it has no :, no str_ireplace can match, and no replacement can add a :. So the output is identical. That includes custom placeholders whose patterns contain :, such as (?:...).
  2. Call helper('array') only on the first create() of each collection, using a private flag. The helper still loads at the same point as today, so apps that quietly depend on it keep working. A short comment will explain that the call stays only for this side effect.

Estimated savings are about 0.74 µs per route, roughly 40% of the build cost:

  • 100 routes: about 75 µs per request
  • 1000 routes: about 0.75 ms per request

Compatibility

  • No change to public or protected APIs. The only new state is private.
  • The route table comes out the same: same keys, handlers, names, and options.
  • No changelog or upgrade-guide entry is planned.

Not changing

  • esc() stays in create(). Apps can replace esc() in app/Common.php, so skipping the call would quietly bypass a custom version on route paths. A faster esc() for the html context could be a separate issue.
  • The matcher (Router::checkRoutes()). A FastRoute-style matcher was prototyped. Without route caching, building its lookup structure on each request cost about as much as the linear scan it replaced. So the net gain was small at typical route counts.

Tests

Add cases to tests/system/Router/RouteCollectionTest.php that lock in today's behavior:

  • A route with no placeholders keeps its exact key.
  • A custom placeholder whose pattern contains (?:...), mixed with built-in placeholders, still expands correctly.

Run RouteCollectionTest.php and RouterTest.php.

Related

Activity

  1. michalsn commented on Oct 10, 2026

    @michalsn
    Member

    If the estimated savings hold up and compatibility is preserved, this seems worth pursuing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions