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

feat: speed up route matching #1981

Merged
merged 3 commits into from
Oct 27, 2023
Merged

feat: speed up route matching #1981

merged 3 commits into from
Oct 27, 2023

Conversation

marvinhagemeister
Copy link
Collaborator

We can significantly speed up route matching by not using URLPattern and opting for a custom constructed RegExp instead. This is roughly 150-180x faster and I confirm that in live traces this greatly reduces CPU time.

benchmark      time (avg)        iter/s             (min … max)       p75       p99      p995
--------------------------------------------------------------- -----------------------------
pattern       160.12 µs/iter       6,245.2 (151.88 µs … 712.62 µs) 161.04 µs 191.46 µs 221.21 µs
regex         889.44 ns/iter   1,124,305.9 (870.65 ns … 965.37 ns) 892.66 ns 965.37 ns 965.37 ns

I've added an additional optimization that first checks if we're dealing with a pattern or an exact path match in the first place. If it's an exact match we can skip parsing it to a pattern entirely, which is even faster.

@marvinhagemeister marvinhagemeister marked this pull request as ready for review October 27, 2023 09:56
@marvinhagemeister marvinhagemeister merged commit 67c7957 into main Oct 27, 2023
8 checks passed
@marvinhagemeister marvinhagemeister deleted the route-matching branch October 27, 2023 09:56
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.

1 participant