Fixed #35518 -- Used simpler string operations for converter-less routes #18270
+31
−11
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.
Trac ticket number
ticket-35518
Branch description
The
RoutePattern
assumes all routes provided include some form of converter, and so needs to change it into a regex for matching. However, if no converters are included in the string, the additional overhead of using a regex vs simpler string operations is unnecessary.Replacing this with a simpler string comparison results in between a 50 and 75% reduction in match time, which stacks up quickly as an application generally has numerous URLs, because
RoutePattern.match
is called once per defined route per request.Before
After
(Benchmarked on Python 3.12, running
main
)Theoretically, these improvements get better based on the length of the route pattern (although at this scale, not notably).
This optimisation could be done by adding a different kind of pattern (eg
LiteralPattern
), but the added complexity to a project probably isn't necessary, not to mention the migration effort to take advantage of this.Checklist
main
branch.