Skip to content

Refactor Router: compile cache, dead code removal, and standards cleanup#1

Merged
Jakiboy merged 1 commit into
masterfrom
copilot/improve-router-performance
Apr 21, 2026
Merged

Refactor Router: compile cache, dead code removal, and standards cleanup#1
Jakiboy merged 1 commit into
masterfrom
copilot/improve-router-performance

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 21, 2026

Router had no caching for the expensive regex compilation step, dead unreachable guards, legacy list() syntax, and null coercions in string concatenation.

Performance

  • Compile cache: compile() memoizes results in $this->cache[$route]; repeated calls for the same pattern return immediately
  • clearRoutes() now resets $cache alongside $routes and $names
  • strpos replaces stripos in method matching — both operands are already uppercased, case-insensitive variant was wasteful

Dead code

  • Removed unreachable guard in addRoutes(): TypeCheck::isArray() is always true for a typed array parameter; instanceof \Traversable is never true on a plain PHP array

Coding standards

  • list()[] destructuring throughout (match(), generate(), compile())
  • null'' in compile() pattern concatenation — eliminates implicit type coercion
  • $strpos$queryPos — avoids shadowing the built-in strpos name
  • foreach/unset loop for stripping numeric capture-group keys → Arrayify::filter(..., ARRAY_FILTER_USE_KEY)
  • Inlined $routeMethodMatch boolean — replaced with a direct continue guard
// Before: recompiles regex on every call for same route
$regex = $this->compile($route); // no caching

// After: cache hit on repeated routes
protected function compile(string $route): string
{
    if ( isset($this->cache[$route]) ) {
        return $this->cache[$route];
    }
    // ... build $compiled ...
    return $this->cache[$route] = "`^{$compiled}$`u";
}

@Jakiboy Jakiboy requested a review from Copilot April 21, 2026 22:29
@Jakiboy Jakiboy marked this pull request as ready for review April 21, 2026 22:29
@Jakiboy Jakiboy merged commit 89f74a2 into master Apr 21, 2026
2 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors Router to improve performance and readability by caching compiled route regexes, removing unreachable/dead code paths, and modernizing syntax.

Changes:

  • Add a compiled-regex memoization cache in compile() and reset it in clearRoutes().
  • Simplify route method matching (use strpos after uppercasing, inline guard/continue).
  • Modernize code style: list()[] destructuring, remove null concatenations, and simplify param-key filtering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Http/Router.php
Comment on lines +336 to +338
if ( isset($this->cache[$route]) ) {
return $this->cache[$route];
}
@floatphp floatphp locked and limited conversation to collaborators Apr 21, 2026
@Jakiboy Jakiboy deleted the copilot/improve-router-performance branch April 21, 2026 22:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants