perf(docker): stop loading ssh2, which allocates a 16 MiB WASM heap nobody uses

Profiling at the memory high-water mark - not at idle, which is what every earlier
round measured - found the largest single retainer in every snapshot was one V8
Context holding a 16.00 MiB ArrayBuffer. Expanding it through the dominator tree
named it: ssh2's crypto module, whose POLY1305_WASM_MODULE instantiates a
WebAssembly memory for ChaCha20-Poly1305 as soon as it is required.

Homarr has no SSH feature and does not depend on ssh2. The chain is
dockerode -> docker-modem -> ssh2, and docker-modem/lib/modem.js requires './ssh'
at module scope while using it in exactly one place, already guarded by
`protocol === 'ssh'`. A pnpm patch moves the require inside that branch. SSH-based
Docker hosts still work - require() is cached, so they pay it once - and every
socket-based install, which is everyone else, never loads it.

Measured over four alternated runs on a restored real backup, external memory:

                     before        after
  board loaded    25.4, 25.1    8.3, 11.2
  nav-01          28.5, 25.4   10.1,  8.6
  multi-tab       31.1, 35.8   13.0, 10.0
  post-soak       21.4, 21.5    5.5,  7.2

Consistently -15 to -18 MiB, ranges non-overlapping, and 30 fewer files loaded
(1470 -> 1440).

Peak RSS does NOT improve, and that is worth stating plainly: V8 sizes its heap
against available headroom, so freeing native memory lets it grow further before
collecting. What changes is the composition - a fixed 16 MiB of unusable WASM
buffer becomes headroom the app can actually use, which matters under a container
memory limit but does not move the peak number.

Also adds the instruments this needed. A heap snapshot can never explain a peak,
because V8 runs a full GC before serialising one and reports only survivors:

  - memory-probe.cjs gains in-process allocation sampling, on-demand snapshots via
    v8.writeHeapSnapshot, an smaps endpoint, and require-graph recording.
  - analyze-allocations.mjs diffs two stages to isolate what one stage allocated.
  - analyze-smaps-stages.mjs splits each stage's RSS into JS heap, JIT, native and
    file-backed pages.
  - analyze-heap-dominators.mjs can now expand the largest retainer, which is what
    identified ssh2. A plain graph walk escapes through __proto__ and is useless.

One trap recorded in the findings: taking the peak snapshot over CDP delivers it as
JS string chunks and added ~134 MiB of ArrayBuffers to the process being measured,
producing a fake 693 MiB reading. v8.writeHeapSnapshot streams from C++ instead, and
the probe now reports its own observerCostMiB so the cost of looking is visible.
This commit is contained in:
Thomas Camlong committed 2026-08-09 22:17:25 +02:00
1 parent 3bc25509cf
commit 3cc6ff33df
9 files changed
+598 -32

No files matched your search

@@ -198,6 +198,17 @@ const label = (o) => {
return `${type}:${name}`;
};
/** The property name a parent reaches a child through — that is what names the contents. */
const edgeLabelBetween = (from, to) => {
for (let e = firstEdge[from]; e < firstEdge[from + 1]; e++) {
if (edges[e * eStride + eTo] / nStride !== to) continue;
const type = edgeTypes[edges[e * eStride + eType]];
const raw = edges[e * eStride + eName];
return type === "element" || type === "hidden" ? `[${raw}]` : (strings[raw] ?? String(raw));
}
return "(indirect)";
};
const candidates = [];
for (let i = 0; i < reachableCount; i++) {
if (i === rootPost) continue;
@@ -228,6 +239,26 @@ for (const i of candidates) {
if (shown.length >= topN) break;
}
// Expanding the single largest retainer is usually the whole point: "a Context retains 16 MiB"
// is not actionable until its contents are named. Only the dominator tree can do this — a
// plain graph walk escapes through __proto__ into the entire heap and reports nonsense.
if (shown.length) {
const top = shown[0];
const childrenOfTop = [];
for (let i = 0; i < reachableCount; i++) {
if (i !== top && dom[i] === top) childrenOfTop.push(i);
}
childrenOfTop.sort((a, b) => retained[b] - retained[a]);
const topNode = postOrderNodes[top];
console.log(`\n=== inside the largest retainer (${label(topNode)}, ${MiB(retained[top])} MiB) ===`);
console.log(` it dominates ${childrenOfTop.length} direct children; heaviest:`);
for (const child of childrenOfTop.slice(0, 20)) {
const node = postOrderNodes[child];
const edgeName = edgeLabelBetween(topNode, node);
console.log(` ${MiB(retained[child]).padStart(8)} MiB ${edgeName} -> ${label(node).slice(0, 78)}`);
}
}
// Grouping by constructor over retained size shows which *kind* of thing dominates, which
// is what a shallow histogram cannot express.
const byName = new Map();