Skip to content

Commit 7cf001f

Browse files
bench: run each section in its own process (#231) (#310)
* bench: run each section in its own process (#231) jsdom permanently retains memory on every string-path DOMPurify sanitize call, so in one process the bench's string-path fixtures table (~3.5 GB retained) carried into every later section and kept the job at a heap cliff. The bench is now a runner that spawns one child per section, sequentially, and collects each child's memory over IPC. - Each section prints a `memory [...]` line (rss / heapUsed / peak RSS). - The runner ends with a per-section summary (result, wall time, peak RSS) and names any section that failed; other sections still run. - `--section <name>` runs one section in-process; `--in-process` keeps the old single-process behaviour for comparison. - Docs: ARCHITECTURE bench section, a jsdom SSR note in SECURITY, and an updated ci.yml comment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F6vooHKVSXCabQW89yiiDs * bench: settle child on 'close' and 'error'; fix OOM comment Resolve on 'close' so the last IPC memory message is always delivered, record a failed spawn as a failed section instead of hanging, and correct the comment: an OOM abort shows as 'killed SIGABRT', not exit 134. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F6vooHKVSXCabQW89yiiDs --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 730c382 commit 7cf001f

4 files changed

Lines changed: 624 additions & 388 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,18 @@ jobs:
218218
# default Linux shell (`bash -e`, no pipefail) would take `tee`'s exit
219219
# code (0) and stay green, defeating the guard.
220220
shell: bash
221-
# The bench streams several large fixtures through jsdom back to back
222-
# (footnote #110, incremental-scan #111, lazy-quote sections) — and
223221
# jsdom permanently retains the DOMPurify string path's per-call
224222
# DOMParser document (~1 MB per sanitize at bench-sized inputs; it
225223
# survives forced GC, and bare DOMPurify-on-jsdom reproduces it with
226-
# no library code involved — see #231). Thousands of string-path
227-
# sanitizes accumulate multiple GB over one run, so the ceiling must
228-
# clear the whole FIXED-SIZE workload with margin: 6144 sat within
229-
# ~1 GB of the peak and any allocation growth in a PR tipped it into
230-
# OOM (exit 134). jsdom-only — browsers and the sanitizeInto fragment
231-
# path do not retain. The durable fix (per-section subprocesses) is
232-
# tracked in #231.
224+
# no library code involved — see #231). The bench therefore runs each
225+
# section in its own child process, so that retention can no longer
226+
# accumulate across sections: the string-path `fixtures` table peaks
227+
# around 3.6 GB RSS on its own and every other section stays under
228+
# ~400 MB (in one process the run climbed past 4.4 GB, and an earlier
229+
# 6144 MB cap OOM'd with exit 134). NODE_OPTIONS is inherited by every
230+
# child, so this cap now applies per section; it leaves ample room over
231+
# the largest one. The summary table at the end of the output names
232+
# any section that failed and shows each section's peak RSS.
233233
env:
234234
NODE_OPTIONS: --max-old-space-size=12288
235235
run: npm run bench | tee bench-output.txt

‎docs/ARCHITECTURE.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,20 @@ each to completion:
673673
```
674674
npm run bench # defaults: iters=5 warmup=2 chunk=8
675675
npm run bench -- --iters 9 --chunk 4
676+
npm run bench -- --section real-document # one section, in this process
677+
npm run bench -- --in-process # every section in one process
676678
```
677679

680+
By default each section (`fixtures`, `scaling`, `footnote-scaling`,
681+
`incremental-scan`, `code-block`, `real-document`, `work-shape`) runs in its own
682+
child process, one after another. jsdom permanently retains memory on every
683+
string-path DOMPurify `sanitize` call ([#231](https://github.com/copse-dev/streaming-markdown/issues/231)),
684+
so in a single process that retention from the string-path `fixtures` table
685+
(about 3.5 GB) carries into every later section. Each section prints a
686+
`memory [...]` line, and the runner ends with a summary of per-section wall
687+
time, exit status and peak RSS, so a section that OOMs or trips its guard is
688+
named and the rest still run.
689+
678690
| Column | Meaning |
679691
| ----------- | ----------------------------------------------------------------- |
680692
| `string ms` | `renderStreamingMarkdown` — full re-render + sanitize each update |

‎docs/SECURITY.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ can hold different policies without bleed (see [#137 / ADR 0003](decisions/0003-
8686
Never write raw `renderMarkdownUnsafe` output to `innerHTML`.
8787
- Provide a sanitizer backend where the native Sanitizer API is absent (Node,
8888
older browsers): the DOMPurify backend. Without one, `renderMarkdown` throws.
89+
Under **jsdom** specifically, each string-path DOMPurify `sanitize` call
90+
leaves memory behind that forced GC does not reclaim (roughly proportional to
91+
the input size; about 1 MB per call at 4 KB inputs). A long-running jsdom SSR
92+
loop should prefer the DOM emitter (`StreamingMarkdownRenderer`, which
93+
sanitizes through the backend's `sanitizeInto` fragment path and barely
94+
retains) or recycle its worker processes. Real browsers and the native
95+
Sanitizer API are unaffected. See
96+
[#231](https://github.com/copse-dev/streaming-markdown/issues/231).
8997
- If you register a mermaid/KaTeX/highlighter backend, you accept its output as
9098
trusted (post-sink). Pin and review those dependencies.
9199

0 commit comments

Comments
 (0)