Skip to content

Write build artifacts with fewer file-system calls - #9408

Draft
cknitt wants to merge 3 commits into
masterfrom
cheaper-atomic-write
Draft

cknitt wants to merge 3 commits into
masterfrom
cheaper-atomic-write

Conversation

@cknitt

@cknitt cknitt commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Based on #9402: this branch contains #9402's two commits, so it targets master to get CI and preview packages; only the last commit belongs to this PR.

A clean build writes thousands of artifacts (.ast, .iast, .cmi, .cmt, .cmti) through File_util.write_file_atomic. On a real project a write costs about 190 µs with one worker and 450 µs with 8 workers, nearly independent of file size, so the file-system calls dominate.

Each write created the temporary file with Filename.temp_file, which closes it, then set its mode with chmod by path, opened it again to write, closed it and renamed it. Now the temporary file is created once with O_EXCL under the same .rewatch-write- prefix, given mode 0644 with fchmod, written through that descriptor, closed and renamed. That is 5 system calls instead of 8. A name that already exists gets a new random name, as in Filename.temp_file. A failed write still closes the descriptor and removes the temporary file.

Mode 0644 still holds under any umask. Unix.fchmod is not available on Windows, so the new Platform.set_file_permissions does nothing there; Windows has no umask and openfile already makes the file writable. The Windows fallback in Compiler_process.write_held is unchanged: a failed rename still raises, removes the temporary file, and the artifact is written in place.

Measured on tests/tests (690 modules, linux-arm64), with the "publish" spans from #9402, 5 alternating clean builds each (median):

before after
publish sum, 1 worker 89.5 ms 73.8 ms
publish avg, 1 worker 61.8 µs 50.9 µs
publish sum, 8 workers 384.4 ms 386.5 ms
publish avg, 8 workers 265.5 µs 266.9 µs

With 8 workers the publish time does not change, so there the time goes elsewhere. Across 10 alternating clean builds with 8 workers, wall time is 0.29 s in both (min 0.28 s before, 0.27 s after) and CPU time (user + sys) is 1.40 s before and 1.37 s after. strace -c with one worker shows 2140 fewer openat, close and lseek calls, and fchmod in place of fchmodat. All 3484 files under lib/ and the .mjs files are byte-identical, with the same modes.

New tests in file_util_tests.ml: an atomic write creates a file and its parent directories, replaces a file with empty and with 200 KB contents, sets 0644 under umask 077, and leaves no temporary file after success, after writing into a missing directory, and after writing over a directory.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH

cknitt and others added 3 commits November 23, 2026 20:12
To see where a build spends its time, RESCRIPT_TRACE=<file> now makes the
build write a Chrome trace event file when it exits, which Perfetto
(ui.perfetto.dev) shows as a timeline:

- The main track has a span per dependency graph (parse, compile,
  namespaces, format) and per scheduler step, and counters for the tasks
  that are ready and running.
- Each worker slot has a track with a span per compiler request, named
  after its input file, with its allocated words and the garbage
  collections during it as arguments. Implementations get child spans for
  type checking and code generation, from a domain-local hook that
  Js_implementation calls once typing is done.

Times are whole microseconds, so spans that share an end point nest
exactly. A command that records nothing, such as a clean, leaves an
earlier trace alone.

Without the variable, the scheduler only checks a flag per step and the
compiler calls an empty hook per implementation. Clean builds of
tests/tests take the same time as before (0.28 s); with the trace on they
take 0.305 s, mostly for writing its 1 MB file. A test in
rewatch-ocaml/tests/run.sh checks the trace's spans and nesting and that
traced builds write the same JavaScript.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH
Signed-off-by: Christoph Knittel <ck@cca.io>
A trace of a real project showed workers spending 20-25% of their time in
tasks but outside compiler requests. That time is the task's result handler,
which publishes the files the request wrote; it now gets a "publish" span
after the request on the worker's track. On tests/tests it takes 398 ms
against 1349 ms of compiler requests. The trace test checks that the span
follows each implementation's request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH
Signed-off-by: Christoph Knittel <ck@cca.io>
A clean build writes thousands of artifacts through write_file_atomic.
Each write created the temporary file, closed it, set its mode by path,
opened it again to write and then renamed it. Now the temporary file is
created once with O_EXCL, given mode 0644 with fchmod, written through
that descriptor, closed and renamed. Windows has no fchmod, so the new
Platform.set_file_permissions does nothing there.

On tests/tests with one compiler worker, the publish spans take 74 ms
instead of 90 ms. With 8 workers they stay at about 385 ms.

Review: Does a failed write still remove its temporary file? Yes, any
exception closes the descriptor if it is still open, removes the file and
re-raises. A test covers a missing directory and a directory as the
destination.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T16:41:40.016245Z 3eb0b7c 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.

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Changelog

🚀 New Feature

  • Write a timeline of the build, viewable in Perfetto, to the file that RESCRIPT_TRACE names.

💅 Polish

  • Write build artifacts with fewer file-system calls, so publishing a module's outputs takes about 17% less time with one compiler worker.

Semantic Diff

3 commits · Full page

Write a build trace for Perfetto with RESCRIPT_TRACE

67ea24a · cknitt + Claude · Nov 23, 2026, 20:08 UTC

To see where a build spends its time, RESCRIPT_TRACE=<file> now makes the build write a Chrome trace event file when it exits, which Perfetto (ui.perfetto.dev) shows as a timeline:

- The main track has a span per dependency graph (parse, compile, namespaces, format) and per scheduler step, and counters for the tasks that are ready and running. - Each worker slot has a track with a span per compiler request, named after its input file, with its allocated words and the garbage collections during it as arguments. Implementations get child spans for type checking and code generation, from a domain-local hook that Js_implementation calls once typing is done.

Times are whole microseconds, so spans that share an end point nest exactly. A command that records nothing, such as a clean, leaves an earlier trace alone.

Without the variable, the scheduler only checks a flag per step and the compiler calls an empty hook per implementation. Clean builds of tests/tests take the same time as before (0.28 s); with the trace on they take 0.305 s, mostly for writing its 1 MB file. A test in rewatch-ocaml/tests/run.sh checks the trace's spans and nesting and that traced builds write the same JavaScript.

Show publishing as a span in the build trace

e15e3b3 · cknitt + Claude · Nov 24, 2026, 07:56 UTC

A trace of a real project showed workers spending 20-25% of their time in tasks but outside compiler requests. That time is the task's result handler, which publishes the files the request wrote; it now gets a "publish" span after the request on the worker's track. On tests/tests it takes 398 ms against 1349 ms of compiler requests. The trace test checks that the span follows each implementation's request.

Write build artifacts with fewer file-system calls

3eb0b7c · cknitt + Claude · Nov 24, 2026, 16:36 UTC

A clean build writes thousands of artifacts through write_file_atomic. Each write created the temporary file, closed it, set its mode by path, opened it again to write and then renamed it. Now the temporary file is created once with O_EXCL, given mode 0644 with fchmod, written through that descriptor, closed and renamed. Windows has no fchmod, so the new Platform.set_file_permissions does nothing there.

On tests/tests with one compiler worker, the publish spans take 74 ms instead of 90 ms. With 8 workers they stay at about 385 ms.

Review

  • Does a failed write still remove its temporary file? Yes, any exception closes the descriptor if it is still open, removes the file and re-raises. A test covers a missing directory and a directory as the destination.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 3eb0b7ce39

ℹ️ 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".

@cknitt
cknitt changed the base branch from build-trace to master November 24, 2026 18:20
@cknitt cknitt closed this Nov 24, 2026
@cknitt cknitt reopened this Nov 24, 2026
@github-actions

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 637,404,885 → 637,313,339 (-0.01%).

Generated projects: 629,030,488 → 628,852,128 (-0.03%).

Benchmark Base Head Change
build/Namespace/clean 84,163,999 84,021,169 -0.17%

Instructions

In total: 9,221,420,665 → 9,221,449,830 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 25,987,723 → 25,987,723 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 3eb0b7c compared with base aacaf72 in this run.

@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.22%. Comparing base (449d6e7) to head (3eb0b7c).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
compiler/driver/compile_phase_hook.ml 66.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9408   +/-   ##
=======================================
  Coverage   82.21%   82.22%           
=======================================
  Files         460      462    +2     
  Lines       65552    65577   +25     
=======================================
+ Hits        53892    53919   +27     
+ Misses      11660    11658    -2     
Files with missing lines Coverage Δ
compiler/driver/js_implementation.ml 85.34% <100.00%> (+0.12%) ⬆️
compiler/driver/compile_phase_hook.ml 66.66% <66.66%> (ø)

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 3eb0b7c

@github-actions

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

1 participant