perf: bound V8's young generation and unbundle server-only deps

Two further wins, both measured on a restored 28-widget board behind a real
demo session (scripts/benchmarks/stress-restore.mts).

--max-semi-space-size=4 in run.sh. --max-old-space-size only bounds the old
generation; V8 also sizes the young generation generously (16 MB per
semi-space) and that slack is pure resident memory. 4 MB cut idle anonymous
memory 120.0 -> 97.2 MiB and peak 710.6 -> 560.2 MiB. It costs CPU, as any
GC-space reduction does — 15.8 -> 17.1 CPU-seconds over the run, ~8% — with
no board-load latency regression (median 594 -> 570 ms). Overridable via
HOMARR_MAX_SEMI_SPACE_SIZE.

serverExternalPackages for the remaining server-only dependencies. Bundling
a package inlines its transitive graph into a chunk that is compiled at boot
even when the code path never runs; leaving it external makes it a lazy
require. Adding winston, drizzle-orm, ical.js, jszip, ldapts, node-unifi,
@kubernetes/client-node and linkedom took idle to 90.3 MiB and board-loaded
209.6 -> 164.6 MiB, with slightly lower CPU (16.3s) and latency (549 ms).
The trade-off is image size: 388 -> 401 MB, because those packages now ship
in node_modules instead of being tree-shaken into chunks.

Cumulative vs the merge-base: idle 141.7 -> 90.3 MiB (-36%), board loaded
257.8 -> 164.6 MiB (-36%), under load 392.7 -> 302.5 MiB (-23%).

The harness now proves the session rather than inferring it: it asserts an
anonymous request to the private board is denied, requires the Auth.js
session cookie after sign-in, and reports whether it had to modify the
restored data at all (now "none"). It also records cgroup CPU seconds and
board-load latency, so a memory win bought with CPU cannot hide.

pnpm test 144/144 files, 1795 tests; typecheck and lint clean.
This commit is contained in:
Thomas Camlong committed 2026-08-09 01:43:06 +02:00
1 parent 9826000722
commit 79ea987761
4 files changed
+134 -16

No files matched your search

+23
View File
@@ -23,6 +23,12 @@ export type StressCheckpoint = {
anonymousBytes: number;
fileBytes: number;
};
/**
* Cumulative cgroup CPU time. Needed because most ways of lowering memory
* (smaller GC spaces, tighter heap caps) buy it with CPU, and a win that just
* moves the cost somewhere unmeasured is not a win.
*/
cpu: { usageUsec: number; userUsec: number; systemUsec: number } | null;
processes: ProcessSample[];
redis: { usedMemoryBytes: number; peakMemoryBytes: number } | null;
};
@@ -39,6 +45,7 @@ echo "memory_current=$(cat /sys/fs/cgroup/memory.current 2>/dev/null || echo 0)"
memory_peak="$(cat /sys/fs/cgroup/memory.peak 2>/dev/null || true)"
case "$memory_peak" in ''|'max') ;; *) echo "memory_peak=$memory_peak" ;; esac
awk '/^anon / { print "cgroup_anon=" $2 } /^file / { print "cgroup_file=" $2 }' /sys/fs/cgroup/memory.stat 2>/dev/null
awk '/^usage_usec / { print "cpu_usage_usec=" $2 } /^user_usec / { print "cpu_user_usec=" $2 } /^system_usec / { print "cpu_system_usec=" $2 }' /sys/fs/cgroup/cpu.stat 2>/dev/null
for status_file in /proc/[0-9]*/status; do
[ -r "$status_file" ] || continue
process_dir="$(dirname "$status_file")"
@@ -84,6 +91,7 @@ export const parseStressSnapshot = (name: string, elapsedMs: number, output: str
const redisUsed = readNumericLine(output, "redis_used_memory");
const redisPeak = readNumericLine(output, "redis_used_memory_peak");
const cpuUsage = readNumericLine(output, "cpu_usage_usec");
return {
name,
@@ -95,6 +103,14 @@ export const parseStressSnapshot = (name: string, elapsedMs: number, output: str
anonymousBytes: readNumericLine(output, "cgroup_anon") ?? 0,
fileBytes: readNumericLine(output, "cgroup_file") ?? 0,
},
cpu:
cpuUsage === null
? null
: {
usageUsec: cpuUsage,
userUsec: readNumericLine(output, "cpu_user_usec") ?? 0,
systemUsec: readNumericLine(output, "cpu_system_usec") ?? 0,
},
processes,
redis: redisUsed === null || redisPeak === null ? null : { usedMemoryBytes: redisUsed, peakMemoryBytes: redisPeak },
};
@@ -183,5 +199,12 @@ export const summarizeStress = (checkpoints: StressCheckpoint[], soakPrefix = "s
const peaks = checkpoints.flatMap((c) => (c.redis ? [c.redis.peakMemoryBytes] : []));
return peaks.length === 0 ? null : toMiB(Math.max(...peaks));
})(),
// Total CPU the container burned over the run, so a memory reduction bought
// with extra GC work shows up instead of hiding.
cpuSeconds: (() => {
const samples = checkpoints.flatMap((c) => (c.cpu ? [c.cpu.usageUsec] : []));
if (samples.length < 2) return null;
return Math.round(((Math.max(...samples) - Math.min(...samples)) / 1_000_000) * 10) / 10;
})(),
};
};