perf(laravel): memoize the Symfony RouteCollection in Router#8342
Merged
soyuka merged 1 commit intoJun 22, 2026
Merged
Conversation
Router::getRouteCollection() rebuilt the entire Symfony RouteCollection on every call via Illuminate\Routing\AbstractRouteCollection::toSymfonyRouteCollection(), converting the whole Laravel route table each time. generate() is called once per IRI during normalization, so a single response with several relations triggered the full conversion dozens of times (~65 for ~8 relations in profiling). Routes are static within a request and Router is a per-request singleton, so the built collection is cached on the instance. Also type SkolemIriConverter's constructor against Symfony\Component\Routing\RouterInterface instead of the final concrete Router, so consumers are no longer pinned to the concrete class. Closes api-platform#8337
37b86cf to
d000a6b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ApiPlatform\Laravel\Routing\Router::generate()callsgetRouteCollection(), which rebuilds the entire SymfonyRouteCollectionon every call viaIlluminate\Routing\AbstractRouteCollection::toSymfonyRouteCollection()— converting the whole Laravel route table each time, with no memoization.generate()runs once per IRI during normalization (once per relation/sub-resource reference). A single response with several relations triggers the full route-table conversion repeatedly — ~65 times for a resource with ~8 relations in the reporter's Xdebug profiling.php artisan route:cachedoesn't help; this conversion happens after Laravel's own route loading.Fix
Routes are static within a request and
Routeris registered as a per-request singleton (ApiPlatformProvider), so the builtRouteCollectionis safely cached on the instance:Only the
RouteCollectionis memoized, not theUrlGenerator— the generator depends on$this->context(mutable viasetContext()), while the collection is context-independent and is the expensive part. Memoizing the generator would risk a stale-context bug.IriConverterandSkolemIriConverterinject the concreteRouterdirectly, so the fix lands inRouteritself rather than in a decorator of the interface binding.SkolemIriConverter typing
As noted in the issue,
SkolemIriConverterhard-typed thefinalconcreteRouterin its constructor, blocking any userland wrapper (a replacement must stayinstanceof Router, impossible for afinalclass). It only uses$this->router->generate(), so its constructor now type-hintsSymfony\Component\Routing\RouterInterface(matchingIriConverter), removing the dependency on the concrete class.Tests
Added
Tests/Unit/Routing/RouterTest.phpassertingtoSymfonyRouteCollection()is invoked once across multiplegetRouteCollection()calls and that the same instance is returned. Functional suites (JsonLdTest,JsonApiTest) pass unchanged.