Skip to content

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

Merged
akrabat merged 6 commits into
slimphp:4.xfrom
BusterNeece:4.x
Nov 6, 2022
Merged

akrabat merged 6 commits into
slimphp:4.xfrom
BusterNeece:4.x

Conversation

@BusterNeece

Copy link
Copy Markdown
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()).

BusterNeece and others added 2 commits November 5, 2022 11:19

@akrabat akrabat left a comment

Copy link
Copy Markdown
Member

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.

Comment thread Slim/Routing/RouteCollector.php
Comment thread Slim/Routing/RouteCollector.php Outdated
Comment thread Slim/Routing/RouteCollector.php Outdated
@BusterNeece

Copy link
Copy Markdown
Contributor Author

@akrabat Changes should be incorporated now.

@coveralls

coveralls commented Nov 6, 2022 •

Copy link
Copy Markdown

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

akrabat commented Nov 6, 2022

Copy link
Copy Markdown
Member

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.

3 participants