Skip to content

Give compiler workers a larger minor heap - #9188

Merged
JonoPrest merged 1 commit into
masterfrom
jono/worker-minor-heap
Nov 4, 2026
Merged

JonoPrest merged 1 commit into
masterfrom
jono/worker-minor-heap

Conversation

@JonoPrest

@JonoPrest JonoPrest commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Every minor collection in OCaml 5 stops all domains until the slowest one has emptied its heap, so busy compiler workers stall each other. Workers already raised their minor heap to 2M words. This raises it to 4M.

While JSX translation forced collections (fixed in #9187), the heap size made no difference. With those gone, the larger heap cuts minor collections by about 30% on a 552 module React app.

Clean builds on OCaml 5.5.2, 8 vCPU Linux VM, 15 interleaved pairs against the 2M heap, median with 95% CI:

project CPU wall
React app, 552 modules 0.91 [0.90, 0.94] 0.90 [0.88, 0.92]
backend service, 391 modules 0.93 [0.90, 0.97] 0.97 [0.90, 0.99]
library 0.99 [0.90, 1.02] 0.95 [0.91, 0.98]

Memory, rescript watch after the initial build, median of 3:

React app idle React app peak backend idle backend peak
2M heap 694 MB 806 MB 468 MB 579 MB
4M heap 799 MB 1022 MB 472 MB 695 MB

So builds peak higher (7 x 16 MB more minor heap, plus a larger major heap from fewer collections). OCaml 5.5 decommits a finished domain's minor heap, so the worker heaps aren't kept in watch mode; the remaining idle difference on the React app is major heap. 8M words added memory for little more speed.

Only GC settings change: JS, .cmi, .cmj and .ast are byte identical to master, .cmt readers give identical output, and make test and make test-rewatch pass.

JonoPrest added a commit that referenced this pull request Nov 3, 2026
The entry names #9188, so the changelog points at this PR.
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Build: give compiler workers a larger minor heap, which cuts garbage collection pauses that stop every domain.

Semantic Diff

1 commit · Full page

Give compiler workers a 4M-word minor heap

292e8f9 · JonoPrest · Nov 03, 2026, 07:59 UTC

Why

Every minor collection in OCaml 5 stops all domains until the slowest one has emptied its heap, so busy compiler workers stall each other. With the forced collections of JSX translation gone, a larger minor heap cuts the collections by about 30% and clean build CPU by about 10% on a JSX-heavy project with 7 workers.

The 2M-word heap was chosen while translating JSX still copied all 591 labels of the DOM props record per element. Each copy forced a minor collection of all domains, and 4M words were no faster than 2M then. 8M words added memory for little more speed.

Review

  • What does it cost? 16 MB more minor heap per worker while a build runs, about 200 MB more peak memory on a 552 module app with 7 workers. OCaml 5.5, which the release builds use, decommits a finished domain's minor heap, so idle watch sessions don't keep the worker heaps.

@JonoPrest
JonoPrest added this pull request to stack #9189 November 3, 2026 07:32
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T19:31:39.562378Z 797a894 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 5a838f57be

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JonoPrest
JonoPrest marked this pull request as ready for review November 3, 2026 07:40
@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9188

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9188

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9188

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9188

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9188

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9188

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9188

commit: 292e8f9

@JonoPrest
JonoPrest requested a review from cknitt November 3, 2026 07:56
JonoPrest added a commit that referenced this pull request Nov 3, 2026
The entry names #9188, so the changelog points at this PR.

Signed-off-by: Jono Prest <jjprest@gmail.com>
@JonoPrest
JonoPrest force-pushed the jono/worker-minor-heap branch from 5a838f5 to ed699e0 Compare November 3, 2026 07:59
@cknitt

cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member

This review was written by Claude (Claude Code), on behalf of the maintainer.

Raising the heap to 4M words looks good. The shrink when a worker exits seems unnecessary on the toolchain we now ship:

1. OCaml 5.5 already frees a finished domain's minor heap. That was not true up to 5.4: domain_terminate left the arena committed until a new domain reused the slot. In 5.5.2, domain_terminate calls free_minor_heap_arena() right after stop_active_domain ("The minor heap arena is only valid for STW-participating domains, so we free it when we stop STW participation"), and that decommits it. Master has been on 5.5.2 since #9043. So on our binaries the shrink adds only a forced minor collection per exiting worker (caml_set_minor_heap_size calls caml_minor_collection() when the young heap isn't empty, which stops all domains), with no memory benefit.

The "4M heap, no shrink: 913 MB idle" number was probably measured on a 5.3/5.4 switch. Could you re-measure it on 5.5.2? If it idles at master's level, I'd drop the shrink and keep only the constant change.

2. with Out_of_memory -> () can't be reached. In 5.5.2 both minor-heap resize paths of Gc.set fail with caml_fatal_error, and gc_ctrl.c never raises an exception.

3. Changelog. If the shrink goes, so does "release it when a worker finishes so watch mode keeps its memory". The explicit PR link isn't needed either: per changes/README.md, the release takes (#N) from the squash commit. The same applies to the link commit in #9187.

4. Nit: comment on worker_minor_heap_words. The history (the JSX copy, "591 labels", "4M no faster than 2M while it existed", the 8M experiment) fits better in the commit message. The comment can keep the current reasoning: minor collections stop every domain, and why 4M was picked over 2M and 8M.

Checked and fine: the forced-collection explanation matches the runtime (caml_make_vect with more than 256 elements and a young first element runs caml_minor_collection, which stops every domain), the 4M size and its cost, and having no new tests for a GC-settings-only change with byte-identical output.

JonoPrest added a commit that referenced this pull request Nov 3, 2026
The entry names #9188, so the changelog points at this PR.

Signed-off-by: Jono Prest <jjprest@gmail.com>
@JonoPrest
JonoPrest force-pushed the jono/worker-minor-heap branch from ed699e0 to e03899f Compare November 3, 2026 15:36
JonoPrest added a commit that referenced this pull request Nov 3, 2026
The entry names #9188, so the changelog points at this PR.

Signed-off-by: Jono Prest <jjprest@gmail.com>
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@JonoPrest
JonoPrest force-pushed the jono/worker-minor-heap branch from e03899f to 797a894 Compare November 3, 2026 19:29

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 797a894e05

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread rewatch-ocaml/process.ml Resolved
@cknitt

cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member

Here, too, can you drop the "Link the changelog entry to its pull request" commit? It is not needed.

@JonoPrest
JonoPrest force-pushed the jono/worker-minor-heap branch from 797a894 to c82ab83 Compare November 3, 2026 19:51
Base automatically changed from jono/jsx-props-skip-label-copy to master November 4, 2026 05:46
Why:
    Every minor collection in OCaml 5 stops all domains until the slowest
    one has emptied its heap, so busy compiler workers stall each other.
    With the forced collections of JSX translation gone, a larger minor
    heap cuts the collections by about 30% and clean build CPU by about
    10% on a JSX-heavy project with 7 workers.

The 2M-word heap was chosen while translating JSX still copied all 591
labels of the DOM props record per element. Each copy forced a minor
collection of all domains, and 4M words were no faster than 2M then.
8M words added memory for little more speed.

Review: What does it cost? 16 MB more minor heap per worker while a
build runs, about 200 MB more peak memory on a 552 module app with 7
workers. OCaml 5.5, which the release builds use, decommits a finished
domain's minor heap, so idle watch sessions don't keep the worker heaps.

Signed-off-by: Jono Prest <jjprest@gmail.com>
@JonoPrest
JonoPrest force-pushed the jono/worker-minor-heap branch from c82ab83 to 292e8f9 Compare November 4, 2026 05:46
@JonoPrest
JonoPrest enabled stacked auto-merge (squash) November 4, 2026 05:47
@JonoPrest
JonoPrest merged commit eb7d92e into master Nov 4, 2026
26 checks passed
@JonoPrest
JonoPrest deleted the jono/worker-minor-heap branch November 4, 2026 06:03
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants