Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist routes indexed by name in RouteCollector for improved performance. #3235

Merged
merged 6 commits into from Nov 6, 2022
Merged

Persist routes indexed by name in RouteCollector for improved performance. #3235

merged 6 commits into from Nov 6, 2022

Conversation

BusterNeece
Copy link
Contributor

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 call RouteCollectorInterface::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 and RouteParserInterface means that it isn't possible to implement a lookup table like this in one's own code without reimplementing the entire RouteParserInterface::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()).

Copy link
Member

@akrabat akrabat left a comment

Choose a reason for hiding this comment

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

Thanks for this! Just a few changes that would be useful.

Slim/Routing/RouteCollector.php Show resolved Hide resolved
Slim/Routing/RouteCollector.php Outdated Show resolved Hide resolved
Slim/Routing/RouteCollector.php Outdated Show resolved Hide resolved
@BusterNeece
Copy link
Contributor Author

@akrabat Changes should be incorporated now.

@coveralls
Copy link

coveralls commented Nov 6, 2022

Coverage Status

Coverage decreased (-0.5%) to 99.36% when pulling c0bc6a5 on BusterNeece:4.x into ca629c1 on slimphp:4.x.

@akrabat akrabat self-requested a review November 6, 2022 15:34
Don't output the tip to the log.
These tests aren't testing the log output, so they just clutter up the
PHPUnit output for no reason.
@akrabat akrabat merged commit b0f4ca3 into slimphp:4.x Nov 6, 2022
@akrabat
Copy link
Member

akrabat commented Nov 6, 2022

Thanks

@akrabat akrabat added this to the 4.11.0 milestone Nov 6, 2022
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.

None yet

3 participants