Repository navigation
perf: index dynamic app-core routes by last static segment - #955
Conversation
Skip the linear RegExp scan for parameter and catch-all routes by bucketing them on their last static segment, while keeping static exact-match first and preserving specificity plus equal-spec order. Co-authored-by: Jon Wheeler <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Mid-path catch-alls miss matches
- Routes with a catch-all before the last static token are no longer indexed at a fixed pathname slot and fall back to the remainder scan, restoring matches such as /files/*path/raw against /files/a/b/raw.
Or push these changes by commenting:
@cursor push 24468eb901
Preview (24468eb901)
diff --git a/packages/app-core/src/server/router.js b/packages/app-core/src/server/router.js
--- a/packages/app-core/src/server/router.js
+++ b/packages/app-core/src/server/router.js
@@ -72,6 +72,8 @@
/**
* Last static segment of a dynamic route pattern. Positions skip empty
* segments so they line up with a leading-slash pathname split.
+ * Returns null when a catch-all precedes that token: `*` can span slashes,
+ * so the token's pathname index is not fixed and the route is scanned.
*
* @param {string} path
* @returns {{ pos: number, value: string } | null}
@@ -81,17 +83,22 @@
let pos = -1;
let value = '';
let index = 0;
+ let sawCatchAll = false;
+ let catchAllBefore = false;
for (let i = 0; i < raw.length; i++) {
const segment = raw[i];
if (!segment) continue;
const first = segment.charCodeAt(0);
- if (first !== 58 /* : */ && first !== 42 /* * */) {
+ if (first === 42 /* * */) {
+ sawCatchAll = true;
+ } else if (first !== 58 /* : */) {
pos = index;
value = segment;
+ catchAllBefore = sawCatchAll;
}
index++;
}
- if (pos === -1) return null;
+ if (pos === -1 || catchAllBefore) return null;
return { pos, value };
}
diff --git a/packages/app-core/tests/router.test.ts b/packages/app-core/tests/router.test.ts
--- a/packages/app-core/tests/router.test.ts
+++ b/packages/app-core/tests/router.test.ts
@@ -143,4 +143,19 @@
params: { slug: 'guides/setup/' },
});
});
+
+ it('matches a catch-all that sits before a later static segment', function () {
+ const raw = new RenderRoute({ path: '/files/*path/raw', entry: '/src/raw.tsrx' });
+ const router = createRouter([raw]);
+
+ expect(router.match('GET', '/files/a/b/raw')).toEqual({
+ route: raw,
+ params: { path: 'a/b' },
+ });
+ expect(router.match('GET', '/files/a/raw')).toEqual({
+ route: raw,
+ params: { path: 'a' },
+ });
+ expect(router.match('GET', '/files/raw')).toBeNull();
+ });
});You can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit d4a6e8b. Configure here.
A static token after `*rest` is not at a fixed pathname index because `(.+)` can consume several segments. Leave those patterns unindexed so the existing RegExp matcher still finds them. Co-authored-by: Jon Wheeler <[email protected]>
|
READY FOR REVIEW |
|
this needs a merge from main and conflicts resolved. |
Compose the #951 static path Map with last-static-segment dynamic indexing and keep both sides' router tests. Co-authored-by: Jon Wheeler <[email protected]>
|
Merged Static exact lookup stays O(1) via Local re-check after the merge: 9/9 router tests, app-core typecheck, and |
The website Nitro preview playground hover assertion failed on justify-items. Website files match main; the same test passed on the Vite-dev pass in that job. Co-authored-by: Jon Wheeler <[email protected]>
|
CI on The only real failure was Pushed empty commit |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|


Summary
@octanejs/app-coreparameter and catch-all routes by their last static segment socreateRouter().match()does not RegExp-scan the whole dynamic table./users/:idvs/:org/userson/users/users)./files/*rest/download) stay on the linear remainder, because(.+)can shift that token past a fixed index.mainis merged so those two indexes sit together.Why
router-dispatchstill walked every dynamic candidate for a last-row/:value/dynamic-Nhit. Exact-string static matching already beat RegExp, but the remaining scan was the next cost. Linear segment compare without an index was not faster than RegExp. Bucketing by last static segment removes that work without changing match semantics.Changes
packages/app-core/src/server/router.js: split compiled routes into perf: index static app-core routes for O(1) dispatch #951's static path Map, a last-static-segment index, and a no-static remainder (pure params, root catch-alls, and static-after-catchall patterns). Gather overlapping buckets, then walk in specificity/insertion order with the existing RegExp +decodeURIComponentmatcher.packages/app-core/tests/router.test.ts: pin method-miss → catch-all (static and dynamic), same-path static method selection, equal-spec insertion order, param-over-catchall, trailing-slash catch-all capture, and mid-path catch-all + static suffix.@octanejs/app-core(patch).benchmarks/router-dispatch: add a lineardynamic-scancontrol and retarget ratio guards at that control.Benchmarks
Environment: Node v22.22.2, Linux 6.12.94+ x86_64, 4 CPUs. Command:
node benchmarks/router-dispatch/run.mjs 20. Scores are ms/million candidates (lower is better).dynamic-scancontrol stayed at ~112–116 ms/millionnode benchmarks/router-dispatch/run.mjs 8still reported dynamic 0.266 vs scan 113.736node benchmarks/bench.mjs --quick --ratios router-dispatch: 3/3 ratio guards passed (dynamic-routes/dynamic-scan≈ 0.003, ceiling 0.05)Other candidates measured (not in this PR)
escapeHtmlclean-pathindexOfreject was ~2× a regex test; dirty-path char-walk was ~1.9× threereplaceAlls. Real page-level savings are small next to this router win.normalizeClassstring path is already ~444M ops/s.Validation
pnpm format:check(not run; scoped Prettier on the changed JS/TS files)pnpm format:files:checkon the changed JS/TS filestsgo --noEmit -p packages/app-core/tsconfig.typecheck.jsonpnpm test(full suite not run locally; targeted router tests below)packages/app-core/tests/router.test.ts(9 tests, including perf: index static app-core routes for O(1) dispatch #951's static method-miss → catch-all). Equal-spec insertion order was confirmed red when the sort tie-breaker was reversed. Mid-path catch-all was confirmed null before the remainder change, then green after restore.node benchmarks/router-dispatch/run.mjs 20A/B as abovenode benchmarks/bench.mjs --quick --ratios router-dispatchRisk / follow-ups
/:a/:bdo not get the index win.paramsobject; public match identity and decoded params are unchanged.escapeHtmlremains a smaller, separate SSR candidate if someone wants a third pass.Provenance
agent-authored)Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.