From 86372bd52423bbc5ffe9fe4cb972228827fd0cdb Mon Sep 17 00:00:00 2001 From: Rohan-Dedhia Date: Wed, 4 Nov 2026 18:22:49 +0530 Subject: [PATCH] perf(compiler): optimize single /** catch-all route --- src/compiler.ts | 60 +++++++++++++++++++++++++++++++++++++++++++ test/compiler.test.ts | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/src/compiler.ts b/src/compiler.ts index 6569fe25..22480703 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -220,6 +220,20 @@ interface CompilerContext { } function compileRouteMatch(ctx: CompilerContext): string { + const singleWildcard = getSingleWildcardRoute(ctx.router); + if (singleWildcard) { + const data = serializeData(ctx, singleWildcard); + const normalizePathHelper = ctx.opts?.normalize + ? `if(p.includes("/.")){let _r=[],_v;for(_v of p.split("/")){if(_v===".")continue;if(_v==="..")_r.length>1&&_r.pop();else _r.push(_v)}if(_v==="."||_v==="..")_r.push("");p=_r.join("/")||"/"}` + : ""; + const strip = `if(p.charCodeAt(p.length-1)===47)p=p.slice(0,-1);`; + let retCode = `{data:${data},params:w?{"0":w,_:w}:{}}`; + if (ctx.opts?.matchAll) { + retCode = `[${retCode}]`; + } + return `${normalizePathHelper}${strip}const w=p.slice(1);return ${retCode};`; + } + const matchAll = ctx.opts?.matchAll; ctx.rank = hasSuffixTrie(ctx.router.root); let code = compileStaticMatch(ctx); @@ -282,6 +296,52 @@ function compileRouteMatch(ctx: CompilerContext): string { return `${matchAll ? collect : ""}${normalizeHelper}${normalizePathHelper}${strip}${code}${matchAll ? done : ""}`; } +function getSingleWildcardRoute(router: RouterContext): MethodData | undefined { + if (Object.keys(router.static).length > 0) { + return undefined; + } + const root = router.root; + if (root.static && Object.keys(root.static).length > 0) { + return undefined; + } + if (root.param) { + return undefined; + } + if (root.methods && Object.keys(root.methods).length > 0) { + return undefined; + } + + const wc = root.wildcard; + if (!wc || wc.key !== "**") { + return undefined; + } + if (wc.static && Object.keys(wc.static).length > 0) { + return undefined; + } + if (wc.param || wc.wildcard || wc.suffix) { + return undefined; + } + if (!wc.methods) { + return undefined; + } + + const methodKeys = Object.keys(wc.methods); + if (methodKeys.length !== 1 || methodKeys[0] !== "") { + return undefined; + } + + const entries = wc.methods[""]; + if (!entries || entries.length !== 1) { + return undefined; + } + + const entry = entries[0]; + if (entry.route !== "/**" || entry.suffix) { + return undefined; + } + return entry; +} + // Below this many static paths an `else if` chain of `p === "..."` compares // beats a map lookup (repeated/interned path strings compare near // pointer-speed, and dynamic requests miss the whole chain via cheap length diff --git a/test/compiler.test.ts b/test/compiler.test.ts index ae90c257..f758ff8a 100644 --- a/test/compiler.test.ts +++ b/test/compiler.test.ts @@ -206,3 +206,51 @@ describe("compiled params (interpreter parity)", () => { ); }); }); + +describe("compiler: single /** route optimization", () => { + it("emits optimized matcher for single /** route", () => { + const router = createRouter(); + addRoute(router, "", "/**", "handler"); + + const code = compileRouterToString(router); + expect(code).not.toContain('split("/")'); + expect(code).toContain("const w=p.slice(1);"); + + const codeMatchAll = compileRouterToString(router, { matchAll: true }); + expect(codeMatchAll).not.toContain('split("/")'); + expect(codeMatchAll).toContain('[{data:$0,params:w?{"0":w,_:w}:{}}]'); + + const jit = compileRouter(router); + const aot = evalAOT(code); + const jitAll = compileRouter(router, { matchAll: true }); + const aotAll = evalAOT(codeMatchAll); + + const testPaths = ["/", "/foo", "/foo/", "/foo/bar", "/foo/bar/", ""]; + for (const path of testPaths) { + const expected = findRoute(router, "GET", path); + expect(jit("GET", path)).toEqual({ data: expected!.data, params: expected!.params }); + expect(aot("GET", path)).toEqual({ data: expected!.data, params: expected!.params }); + + const expectedAll = findAllRoutes(router, "GET", path); + expect(jitAll("GET", path)).toEqual( + expectedAll.map((r) => ({ data: r.data, params: r.params })), + ); + expect(aotAll("GET", path)).toEqual( + expectedAll.map((r) => ({ data: r.data, params: r.params })), + ); + } + }); + + it("does not optimize if multiple routes or method specified", () => { + const routerMethod = createRouter(); + addRoute(routerMethod, "GET", "/**", "get-only"); + const codeMethod = compileRouterToString(routerMethod); + expect(codeMethod).toContain('split("/")'); + + const routerMultiple = createRouter(); + addRoute(routerMultiple, "", "/**", "all"); + addRoute(routerMultiple, "GET", "/specific", "spec"); + const codeMultiple = compileRouterToString(routerMultiple); + expect(codeMultiple).toContain('split("/")'); + }); +});