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

router: URL-route matching is expensive #1931

Closed
lucab opened this issue Oct 13, 2023 · 2 comments · Fixed by #1932
Closed

router: URL-route matching is expensive #1931

lucab opened this issue Oct 13, 2023 · 2 comments · Fixed by #1932
Assignees
Labels
bug Something isn't working.

Comments

@lucab
Copy link

lucab commented Oct 13, 2023

After profiling a Fresh application in production, we found out that the current URL/route matching through URLPattern is expensive both in consumed CPU time and memory usage:

fresh/src/server/router.ts

Lines 146 to 152 in d391924

const processedRoutes: InternalRoute<T>[] = [];
processRoutes(processedRoutes, internalRoutes, "internal");
processRoutes(processedRoutes, staticRoutes, "static");
processRoutes(processedRoutes, routes, "route");
return (url: string) => {
for (const route of processedRoutes) {
const res = route.pattern.exec(url);

At the time of this writing our app has ~800 static-files routes, and a total of ~900 routes overall. Each one of those translates to a dedicated URLPattern instance.
On each request, URLPattern.exec(url) is called in a loop until a matching route is found (if any).
Unfortunately at the moment the URLPattern implementation in Deno is known to have some performance issues: denoland/deno#19861.

Under sustained organic traffic, it looks like this quickly becomes an hot tight-loop, consuming most of the CPU active time.
From the same profile, it looks like this is also causing a lot of pressure on V8 memory handler.

@lucab
Copy link
Author

lucab commented Oct 13, 2023

For future reference, this would likely benefit from the proposed URLPatternList: denoland/deno#20899

@marvinhagemeister marvinhagemeister added the bug Something isn't working. label Oct 13, 2023
@marvinhagemeister marvinhagemeister self-assigned this Oct 13, 2023
@marvinhagemeister
Copy link
Collaborator

Yup, I've discovered this yesterday too while inspecting a site with many static files. I'm already working on a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants