Skip to content

perf: index static app-core routes for O(1) dispatch - #951

Merged
trueadm merged 4 commits into
mainfrom
cursor/runtime-perf-audit-68b2
Sep 5, 2026
Merged

trueadm merged 4 commits into
mainfrom
cursor/runtime-perf-audit-68b2

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Index exact (static) @octanejs/app-core routes by path so createRouter().match() is O(1) instead of a linear scan of the compiled table.
  • After a static method miss, matching still falls through to parameter and catch-all routes, so a GET-only /api does not hide /*rest.
  • Same-path server methods keep specificity order; render routes still match any method.

Why

router-dispatch measures a 1,000-route table and still walked every candidate for a last-row static hit or a lowercase method miss. Exact-string compare already beat RegExp, but the scan itself dominated. A path map removes that work without changing match semantics.

Changes

  • packages/app-core/src/server/router.js: split compiled routes into a static Map and a dynamic list; consult the map first. Dynamic candidates narrow pattern to RegExp before exec.
  • packages/app-core/tests/router.test.ts: pin method-miss → catch-all fall-through, shared-path method selection, and same-path render fallback.
  • Changeset for @octanejs/app-core (patch).
  • scripts/check-package-packs.mjs: pin packed-consumer PostCSS to 8.5.26 via pnpm-workspace.yaml (pnpm 11; package.json pnpm.overrides does not apply here). postcss 8.5.28's DeclarationProps extends NodeProps fails tsrx-tsc (TS2304) on every current-head pack job, including unrelated PRs. Install asserts the resolved version.

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). A/B/A/B on the same machine:

run static wrong-method dynamic (control)
A1 baseline 5.554 5.312 118.997
B1 indexed 0.032 0.033 121.063
A2 baseline 5.620 5.328 120.327
B2 indexed 0.032 0.032 122.541
  • Static: ~176× faster
  • Wrong-method: ~161–166× faster
  • Dynamic control: +1.7–1.8%, inside observed noise (not claimed as a regression or a win)

Existing ratio guards (static/wrong-method vs dynamic, maxRatio: 0.2) remain valid and become much looser.

Validation

  • pnpm format:check (not run; scoped Prettier on the changed files)
  • pnpm typecheck via CI on 9f099eca1 (required typecheck job green)
  • pnpm test via CI on 9f099eca1 (Node 24 shards, React-parity, lint, examples, Lynx/Three)
  • targeted tests: packages/app-core/tests/router.test.ts (4 tests). The catch-all fall-through case was confirmed red when the static miss returned null before the dynamic scan, then green after restore.
  • node benchmarks/router-dispatch/run.mjs 20 A/B/A/B as above
  • packed-consumer gate: 9f099eca1 workspace-yaml PostCSS pin; validate publishable packages and CI provenance green

Risk / follow-ups

  • Dynamic (RegExp) matching is unchanged aside from an O(1) map miss on tables with no static routes.
  • Callers still receive a fresh params object; public match identity and decoded params are unchanged.
  • The PostCSS pin is a CI consumer override, not an application runtime change. Lift it when postcss types resolve NodeProps under tsrx-tsc.

Provenance

  • An agent produced this diff (agent-authored)
Open in Web Open in Cursor 

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Look up exact paths in a Map instead of scanning the compiled table, and keep falling through to parameter and catch-all routes after a method miss.

Co-authored-by: Jon Wheeler <[email protected]>
@jonkwheeler
jonkwheeler marked this pull request as ready for review September 4, 2026 14:14
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work labels Sep 4, 2026
cursoragent and others added 2 commits September 4, 2026 14:24
Typecheck treats compiled.pattern as string | RegExp. Guard the dynamic scan so exec is only called on a RegExp.

Co-authored-by: Jon Wheeler <[email protected]>
postcss 8.5.28 makes DeclarationProps extend NodeProps, which tsrx-tsc cannot resolve. The packed-consumer smoke now overrides postcss to 8.5.26 so that gate is not broken by the types publish.

Co-authored-by: Jon Wheeler <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Postcss pin ignored under pnpm 11
    • Moved the postcss 8.5.26 pin from ignored package.json pnpm.overrides into pnpm-workspace.yaml via renderPackedExampleWorkspace so pnpm 11 applies it.

Create PR

Or push these changes by commenting:

@cursor push e3052f19fb
Preview (e3052f19fb)
diff --git a/scripts/check-package-packs.mjs b/scripts/check-package-packs.mjs
--- a/scripts/check-package-packs.mjs
+++ b/scripts/check-package-packs.mjs
@@ -478,20 +478,20 @@
 					typescript: typescriptVersion,
 					vite: viteVersion,
 				},
-				// postcss 8.5.28 makes DeclarationProps extend NodeProps, but
-				// tsrx-tsc cannot resolve that named export from node.d.ts
-				// (TS2304). Pin the last types that typecheck here.
-				pnpm: {
-					overrides: {
-						postcss: '8.5.26',
-					},
-				},
 			},
 			null,
 			2,
 		) + '\n',
 	);
+	// postcss 8.5.28 makes DeclarationProps extend NodeProps, but
+	// tsrx-tsc cannot resolve that named export from node.d.ts
+	// (TS2304). Pin the last types that typecheck here. pnpm 11 reads
+	// overrides from pnpm-workspace.yaml, not package.json pnpm.overrides.
 	writeFileSync(
+		path.join(consumerDirectory, 'pnpm-workspace.yaml'),
+		renderPackedExampleWorkspace({ postcss: '8.5.26' }),
+	);
+	writeFileSync(
 		path.join(sourceDirectory, 'App.tsrx'),
 		`import { ApolloClient, InMemoryCache } from '@octanejs/apollo-client';
 import { ApolloProvider, useApolloClient } from '@octanejs/apollo-client/react';

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit c97e755. Configure here.

Comment thread scripts/check-package-packs.mjs Resolved
pnpm 11 ignores package.json pnpm.overrides in this isolated consumer. Write the same override in pnpm-workspace.yaml as the other packed consumers, and assert the resolved postcss version after install.

Co-authored-by: Jon Wheeler <[email protected]>
@cursor

cursor Bot commented Sep 4, 2026

Copy link
Copy Markdown

READY FOR REVIEW

@trueadm
trueadm merged commit 56c850e into main Sep 5, 2026
30 checks passed
@trueadm
trueadm deleted the cursor/runtime-perf-audit-68b2 branch September 5, 2026 17:30
cursor Bot pushed a commit that referenced this pull request Sep 6, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work READY FOR REVIEW

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants