Skip to content

Conversation

@mosabbirrakib
Copy link
Contributor

@mosabbirrakib mosabbirrakib commented Nov 25, 2025

This PR optimizes the getByAction() method in CompiledRouteCollection by replacing Collection usage with a simple foreach loop.

Optimized matchAgainstRoutes() by avoiding unnecessary Collection operations, this change eliminates the overhead of:

  • Creating a new Collection instance
  • Using the first() method with a closure

Before:

$attributes = (new Collection($this->attributes))->first(function (array $attributes) use ($action) {
    if (isset($attributes['action']['controller'])) {
        return trim($attributes['action']['controller'], '\\') === $action;
    }
    return $attributes['action']['uses'] === $action;
});

After:

foreach ($this->attributes as $attributes) {
    if (isset($attributes['action']['controller'])) {
        if (trim($attributes['action']['controller'], '\\') === $action) {
            return $this->newRoute($attributes);
        }
    } elseif (($attributes['action']['uses'] ?? null) === $action) {
        return $this->newRoute($attributes);
    }
}

This method is used by UrlGenerator::action() when generating URLs to controller actions. While not in the hot path of request handling, this optimization reduces memory allocation and improves performance.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants