Persist routes indexed by name in RouteCollector for improved performance. #3235
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.
In our application, we have what I think is a reasonable number of routes defined (a few dozen), and we're often calling
RouteParserInterface::relativeUrlFor
and its related functions. Under the hood, these all callRouteCollectorInterface::getNamedRoute
, and right now, that's iterating through every single route, checking the name, and returning the result if it finds it.Individually, these calls aren't a big deal, but they add up fast. If, say, you're returning an API response with several records with several links in each row, this tends to result in a huge number of calls to
RouteInterface::getName()
.As a result of their exposed methods, the nature of the
RouteCollectorInterface
andRouteParserInterface
means that it isn't possible to implement a lookup table like this in one's own code without reimplementing the entireRouteParserInterface::relativeUrlFor
functionality.However, fortunately that also means that this change can be made at the Slim level without any change to the interface. The logic here is identical to how it functions currently (the first route with a given name is returned by
getNamedRoute()
).