← Back to Pull requests

perf: reduce build memory and time #1156

Closed
unknown closed this pull request without merging on Oct 13, 2026 15:00 UTC
View on GitHub
unknown unknown commented on Oct 07, 2026 21:49 UTC
Author

Description

Building the Node.js docs peaks at 3.6–3.8GB of memory, while a single page only needs ~100MB. Node core only builds the HTML docs on machines with more than 5GiB of memory, and silently skips them otherwise. That's what happens on its ubuntu-slim doc CI, whose docs artifact has no HTML at all.

This PR brings the peak down to 1.7–2.1GB, and the build time to about a third:

  • Shiki registers grammars on demand. Once a thread highlighted code, the Shiki plugin registered all ~250 grammars Shiki bundles, and importing it imported all of them on every thread loading jsx-ast. The docs use about ten languages. Each one is now registered the first time code in it is highlighted, with the languages it embeds, which takes highlighting from ~17s of CPU to ~4s, and the themes go to Shiki by name, so it stops parsing them again for every block. Code highlights the same whatever a thread highlighted before: fences in a Markdown block are registered with it, and a grammar injecting into others (ts-tags) only does so in its own blocks. @node-core/rehype-shiki is updated to 1.5.1, whose plugin only imports every grammar when it isn't given a highlighter (nodejs/nodejs.org#9212).

  • Highlighted code reaches pages as static markup. Every highlighted token became a hast node, then a JSX element, then generated code, only to be rendered back into the same HTML. A plugin after Shiki writes the code blocks out as HTML with hast-util-to-html, and types and signatures are embedded as they're highlighted, before rehype-raw would parse each of their tokens again. Each thread also highlights a type only once (of the ~20,000 types the docs highlight, 741 differ).

  • all.html renders in the worker pool. It was rendered and minified on the main thread, after every other page, holding the whole site there (~1GB).

  • Output isn't minified by default. Every page went through SWC's HTML minifier, compiled to WASM. Its memory grows to ~12× the biggest page it's given (~400MB for the 33MB all.html) and is never returned, so every thread rendering pages kept it until it ended, and minifying took a fifth of the build's CPU. On Node 24, the worker holding that memory collected garbage on nearly every allocation, which took the build from ~8s to ~52s. In exchange, the 741 pages were 5.9% smaller on disk, but only 3.2% smaller gzipped and 2.1% with brotli, which is how they're served: Cloudflare, Vercel, Netlify and GitHub Pages compress HTML on their own, and so do doc-kit.nodejs.org and nodejs.org. So minify now defaults to false, and the minifier only loads when it's enabled; docs/publishing.md shows how to serve the output compressed. Scripts and styles stay minified, unless vite.build.minify says otherwise.

  • Idle workers end after 250ms instead of a second, so the jsx-ast workers' heaps (~800MB) are gone while the main thread bundles the site, which is when the build peaked.

  • Worker heaps are limited to 512MB. V8 lets a heap grow to several times its live data before collecting it, four times with the 4GB limit workers get on machines with 16GB+. It costs 3–5% of build time, spent collecting garbage. Node's --max-old-space-size replaces the limit, to raise it.

  • Smaller fixes: the bundler is let go before the pages render (~200MB with one thread), and each thread only imports the modules it uses. That includes the logger, whose GitHub Actions transport now writes its workflow commands itself, for the same output, rather than importing @actions/core and undici on every thread (237 modules, 19MB and ~80ms per thread, down to 10 modules, 3MB and 7ms).

  • The legacy HTML generator's Shiki plugin is marked as deprecated, as it will be removed soon.
  • How a build runs

    The main thread schedules the generators, holds their results and bundles the site with Vite. The rest of the work goes to a pool of worker threads, which end once idle:

    sequenceDiagram
      participant M as Main thread
      participant W as Worker pool (Piscina)
    
      Note over W: up to 4 workers, 512MB of heap each
      M->>W: ast, metadata and jsx-ast chunks
      W-->>M: each page's code, highlighted code as static markup
      Note over W: workers idle for 250ms end
      Note over M: Vite bundles the component library and the client assets, then compiles each page program
      M->>W: page tasks, all.html first
      Note over W: import, render, template, write
      W-->>M: written pages
    

    With threads: 1, the main thread runs the chunks itself.

    Validation

  • node --run test (747 tests) passes at every commit, and node --run format:check and node --run lint pass.

  • Every page matches main once both are minified with main's minifier and parsed as HTML (hast-util-to-html spells a few characters in code differently than Preact did, such as < for <), on the web target (71 pages), on Node core's config (741) and on legacy-html (69), except the C++ code blocks of addons, embedding and n-api (and all.html, which holds them), whose grammar now comes from core's own shiki (4.4.3) instead of the one @node-core/rehype-shiki 1.5.0 pinned (4.3.1).

  • With Node's --max-old-space-size, which limits every isolate, the Node core build passes at 384MB (not at 320MB), where main needs 768MB.
  • Benchmarks

    main → this branch, building Node core's docs with its config (legacy-json-all + section-pages) unless noted. Medians of 3 interleaved runs on an M4 Pro; peak memory is the physical footprint of every process in the build.

    | Build | Peak memory | Duration | CPU time |
    | --- | --- | --- | --- |
    | Node 26, 4 threads | 3,626MB → 1,654MB (-54%) | 22.5s → 7.7s | 83.7s → 32.5s |
    | Node 26, 1 thread | 3,787MB → 2,017MB (-47%) | 49.3s → 15.0s | 60.7s → 20.3s |
    | Node 24, 4 threads | 3,823MB → 2,098MB (-45%) | 23.8s → 7.7s | 90.7s → 34.1s |
    | web target, Node 26 | 3,335MB → 1,598MB (-52%) | 17.8s → 5.8s | 58.0s → 23.7s |

    With one thread, the main thread does all the work, and the workers' heap limit doesn't apply. Node's --max-old-space-size=768 brings that build to 1.47GB in about the same time.

    Related Issues

    Refs: #815
    Refs: https://github.com/nodejs/node/pull/62045

    Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.

  • I have run node --run test and all tests passed.

  • I have check code formatting with node --run format:check & node --run lint.

  • I've covered new added functionality with unit tests if necessary.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: araujogui Review duration: 8230m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1156"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%
    Repository