60d71da90e
Cuts CPU on the `engine/v1/worker-actions/*` routes a managed supervisor calls, and adds the benchmark harness the numbers come from. Measured on a local stack: **on-CPU per completed run 9.07ms → 6.59ms (−27%)**, busy fraction 45.6% → 33.8%, with every worker-action p50 down 23–27%. Load was 5,000 runs / 24 virtual supervisors / 90s window / 30,120 requests / 0 errors. Query-count work from the same investigation is deliberately **not** here — it will follow as a separate PR. ## The three changes **1. Split the event-loop monitor in two (~14% of on-CPU, plus ~5pp of GC).** `eventLoopMonitor.server.ts` installs a global `async_hooks` hook: `init` writes a `Map` entry for *every* async resource the process creates, `before` calls `process.hrtime()` and `context.active()` on every one. Enabling any async hook also puts V8 on the slow path for promise instrumentation process-wide. `EVENT_LOOP_MONITOR_ENABLED` defaulted to `"1"`, so this was the shipping configuration. The blocked-loop detector is now opt-in (`EVENT_LOOP_MONITOR_ENABLED`, default `0`). The event-loop *utilization* gauge — a single interval timer with no per-request cost — moves to its own flag (`EVENT_LOOP_UTILIZATION_MONITOR_ENABLED`, default `1`) and stays on, so the useful half survives without the expensive half. A/B under identical load: | | monitor on | monitor off | change | |---|---|---|---| | on-CPU per run | 9.08ms | 7.25ms | −20% | | GC self time | 9.80% | 5.05% | −4.75pp | | dequeue p50 | 76.6ms | 62.8ms | −18% | | attempts/start p50 | 56.3ms | 43.5ms | −23% | **2. Bucket route matching by first static path segment (10.4% → 3.9% of on-CPU).** `patches/@remix-run__router@1.23.3.patch` already memoized flattened branches and compiled path regexes. What remained was the linear scan: `matchRouteBranch` walked the ranked branch list calling `matchPath` per branch across 521 route files, so every worker-action request paid a scan proportional to the whole route table. Branches are now indexed by their lowercased leading segment, with one always-considered list for branches whose leading segment is dynamic, splat or optional (and for root/pathless paths). A request walks only its own bucket merged with that list. Route-matching self time dropped 64% (3.6s → 1.3s over a 90s window). Ordering is preserved exactly: both lists hold indexes into the already rank-sorted branch array and are walked in ascending-index order, so the first match found is the same branch the full scan would have found. Bucketing lowercases on both sides, so case-insensitive matching still resolves and `caseSensitive: true` routes are still rejected by `matchPath` itself. A pathname whose own leading segment can't be bucketed falls back to the full scan. Verified equivalent to the unpatched matcher over 20,050 pathnames (literal, dynamic, splat, optional, case variants, basenames, percent-encoded) with zero mismatches. `apps/webapp/test/routeMatchingPatch.test.ts` pins the matching semantics rather than the optimisation, so it still passes without the patch. **3. Demote per-heartbeat and per-dequeue `info` logs to `debug`.** These are the two highest-rate engine calls and each wrote a synchronous structured log line on every request. Synchronous `console` writes can block the loop when stdout backs up, which costs more than the ~1.3% CPU share suggests. ## The harness Two benchmarks, neither in the default suite (they run for minutes, attach the V8 profiler, and report numbers rather than assert on them). See `apps/webapp/test/bench/README.md`. - `apps/webapp/test/bench/engineHttp.bench.test.ts` — spawns a real webapp against throwaway Postgres/Redis containers, seeds a production environment with a promoted managed deployment, and drives a closed-loop supervisor pool through the full lifecycle. Profiling runs over CDP rather than `--cpu-prof` so it covers only the measured window instead of being swamped by boot, and `performance.eventLoopUtilization()` is sampled *inside* the webapp process. - `internal-packages/run-engine/src/engine/bench/runEngineLifecycle.bench.test.ts` — drives `RunEngine` directly, profiling enqueue and lifecycle separately so engine cost isn't mixed with request-stack overhead. - `apps/webapp/test/bench/analyzeProfile.ts` — dependency-free `.cpuprofile` analyzer that symbolicates through the build's source maps and ranks CPU by package, self time and total time. Percentages are shares of on-CPU time (V8's `(idle)`/`(program)` excluded). `startWebapp` gains `overrideEnv`, applied after the worker-disable defaults, so the HTTP bench can re-enable the run engine worker that drains the master queue into the worker queues a supervisor dequeues from. The local OTel collector gains a traces pipeline. It only defined a metrics pipeline, so pointing `INTERNAL_OTEL_TRACE_EXPORTER_URL` at it locally failed and the webapp silently fell back to the console span logger. ## Configuration For operators upgrading: - `EVENT_LOOP_MONITOR_ENABLED` (now defaults to `0`) — the per-async-resource blocked-loop detector. Set to `1` to restore the previous behaviour and keep emitting `event-loop-blocked` spans. - `EVENT_LOOP_UTILIZATION_MONITOR_ENABLED` (new, defaults to `1`) — the `nodejs.event_loop.utilization` gauge. Unchanged in behaviour; it just has its own flag now so it survives turning the detector off. ## Notes for review - `pnpm-lock.yaml` changes only because the router patch content changed, which changes its patch hash. - One thing the profile ruled out: with a real OTLP collector receiving spans, tracing costs ~1.7% of on-CPU at 100% sampling and ~0.8% at the production rate. Span shipping is not a hidden cost, so nothing here touches it. - Caveats on the numbers: a laptop, not production hardware, so DB and Redis *latency* are unrepresentative (client-side CPU is what's ranked); single webapp process; throughput varies ~5% run to run, which is why the claims rest on on-CPU per run rather than req/s. ## Verification - 20,050-pathname router equivalence check vs the unpatched matcher, zero mismatches - `apps/webapp/test/routeMatchingPatch.test.ts` (12 cases) passes - webapp e2e smoke suite (68 tests) passes through the patched router - run-engine suites covering the snapshot/attempt paths pass - `typecheck`, `format`, `lint`, `knip` clean
145 lines
6.0 KiB
Diff
145 lines
6.0 KiB
Diff
diff --git a/dist/router.cjs.js b/dist/router.cjs.js
|
|
index 6aa7db6fb5a7182afcdf17b16a3356abfa1e7945..5ae0b0e632a356493c3a8b0c88ebd396e8f5305b 100644
|
|
--- a/dist/router.cjs.js
|
|
+++ b/dist/router.cjs.js
|
|
@@ -783,6 +783,51 @@ function convertRoutesToDataRoutes(routes, mapRouteProperties, parentPath, manif
|
|
*
|
|
* @see https://reactrouter.com/v6/utils/match-routes
|
|
*/
|
|
+// trigger.dev perf patch — memoize per-request route matching. See patches/README.md
|
|
+// (backports the idea in react-router PR #14866, which was closed in favor of the partial
|
|
+// fix #14967; maintainer suggested patch-package until the Remix 3 route-pattern rewrite).
|
|
+let __branchCache = new WeakMap();
|
|
+let __compileCache = new Map();
|
|
+/**
|
|
+ * trigger.dev perf patch 2 — bucket ranked branches by their first static path
|
|
+ * segment so a request scans only branches that could match it, rather than the
|
|
+ * whole 500+ route table. See patches/README.md.
|
|
+ */
|
|
+let __bucketCache = new WeakMap();
|
|
+/**
|
|
+ * Returns the lowercased leading segment when it is static, or null when the
|
|
+ * branch can match any first segment (dynamic, splat or optional leading
|
|
+ * segment, or a root/pathless path) and so must always be considered.
|
|
+ */
|
|
+function __firstStaticSegment(path) {
|
|
+ if (!path || path === "/") return null;
|
|
+ let start = path.charCodeAt(0) === 47 ? 1 : 0;
|
|
+ let end = path.indexOf("/", start);
|
|
+ let seg = end === -1 ? path.slice(start) : path.slice(start, end);
|
|
+ if (seg === "") return null;
|
|
+ if (seg.indexOf(":") !== -1 || seg.indexOf("*") !== -1 || seg.indexOf("(") !== -1 || seg.indexOf("?") !== -1) {
|
|
+ return null;
|
|
+ }
|
|
+ return seg.toLowerCase();
|
|
+}
|
|
+function __buildBuckets(branches) {
|
|
+ let byFirstSegment = new Map();
|
|
+ let always = [];
|
|
+ for (let i = 0; i < branches.length; ++i) {
|
|
+ let seg = __firstStaticSegment(branches[i].path);
|
|
+ if (seg === null) {
|
|
+ always.push(i);
|
|
+ continue;
|
|
+ }
|
|
+ let list = byFirstSegment.get(seg);
|
|
+ if (!list) {
|
|
+ list = [];
|
|
+ byFirstSegment.set(seg, list);
|
|
+ }
|
|
+ list.push(i);
|
|
+ }
|
|
+ return { byFirstSegment, always };
|
|
+}
|
|
function matchRoutes(routes, locationArg, basename) {
|
|
if (basename === void 0) {
|
|
basename = "/";
|
|
@@ -795,18 +840,51 @@ function matchRoutesImpl(routes, locationArg, basename, allowPartial) {
|
|
if (pathname == null) {
|
|
return null;
|
|
}
|
|
- let branches = flattenRoutes(routes);
|
|
- rankRouteBranches(branches);
|
|
+ // flatten+rank depend only on `routes` (static) — cache per route-tree ref.
|
|
+ let branches = __branchCache.get(routes);
|
|
+ if (!branches) {
|
|
+ branches = flattenRoutes(routes);
|
|
+ rankRouteBranches(branches);
|
|
+ __branchCache.set(routes, branches);
|
|
+ }
|
|
let matches = null;
|
|
let decoded = decodePath(pathname);
|
|
- for (let i = 0; matches == null && i < branches.length; ++i) {
|
|
- // Incoming pathnames are generally encoded from either window.location
|
|
- // or from router.navigate, but we want to match against the unencoded
|
|
- // paths in the route definitions. Memory router locations won't be
|
|
- // encoded here but there also shouldn't be anything to decode so this
|
|
- // should be a safe operation. This avoids needing matchRoutes to be
|
|
- // history-aware.
|
|
- matches = matchRouteBranch(branches[i], decoded, allowPartial);
|
|
+ // Incoming pathnames are generally encoded from either window.location
|
|
+ // or from router.navigate, but we want to match against the unencoded
|
|
+ // paths in the route definitions. Memory router locations won't be
|
|
+ // encoded here but there also shouldn't be anything to decode so this
|
|
+ // should be a safe operation. This avoids needing matchRoutes to be
|
|
+ // history-aware.
|
|
+ let buckets = __bucketCache.get(branches);
|
|
+ if (!buckets) {
|
|
+ buckets = __buildBuckets(branches);
|
|
+ __bucketCache.set(branches, buckets);
|
|
+ }
|
|
+ let requestSegment = __firstStaticSegment(decoded);
|
|
+ if (requestSegment === null) {
|
|
+ for (let i = 0; matches == null && i < branches.length; ++i) {
|
|
+ matches = matchRouteBranch(branches[i], decoded, allowPartial);
|
|
+ }
|
|
+ return matches;
|
|
+ }
|
|
+ /**
|
|
+ * Both lists hold indexes into the already rank-sorted `branches`, so walking
|
|
+ * them in ascending-index order preserves the exact evaluation order the
|
|
+ * unbucketed scan would have used.
|
|
+ */
|
|
+ let scoped = buckets.byFirstSegment.get(requestSegment);
|
|
+ let always = buckets.always;
|
|
+ let si = 0;
|
|
+ let ai = 0;
|
|
+ let scopedLength = scoped === undefined ? 0 : scoped.length;
|
|
+ while (matches == null && (si < scopedLength || ai < always.length)) {
|
|
+ let index;
|
|
+ if (si < scopedLength && (ai >= always.length || scoped[si] < always[ai])) {
|
|
+ index = scoped[si++];
|
|
+ } else {
|
|
+ index = always[ai++];
|
|
+ }
|
|
+ matches = matchRouteBranch(branches[index], decoded, allowPartial);
|
|
}
|
|
return matches;
|
|
}
|
|
@@ -1115,6 +1193,12 @@ function compilePath(path, caseSensitive, end) {
|
|
if (end === void 0) {
|
|
end = true;
|
|
}
|
|
+ // perf patch: cache the compiled [regexp, params] by pattern (see patches/README.md).
|
|
+ let __ck = path + "\0" + caseSensitive + "\0" + end;
|
|
+ let __cc = __compileCache.get(__ck);
|
|
+ if (__cc !== void 0) {
|
|
+ return __cc;
|
|
+ }
|
|
warning(path === "*" || !path.endsWith("*") || path.endsWith("/*"), "Route path \"" + path + "\" will be treated as if it were " + ("\"" + path.replace(/\*$/, "/*") + "\" because the `*` character must ") + "always follow a `/` in the pattern. To get rid of this warning, " + ("please change the route path to \"" + path.replace(/\*$/, "/*") + "\"."));
|
|
let params = [];
|
|
let regexpSource = "^" + path.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
|
|
@@ -1147,7 +1231,11 @@ function compilePath(path, caseSensitive, end) {
|
|
regexpSource += "(?:(?=\\/|$))";
|
|
} else ;
|
|
let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
|
|
- return [matcher, params];
|
|
+ let __res = [matcher, params];
|
|
+ // Bounded: route patterns are a static set; the cap guards any dynamic matchPath() use.
|
|
+ if (__compileCache.size >= 2000) __compileCache.clear();
|
|
+ __compileCache.set(__ck, __res);
|
|
+ return __res;
|
|
}
|
|
function decodePath(value) {
|
|
try {
|