Skip to content

Add opt-in cross-module inlining for Option helpers - #8689

Open
cknitt wants to merge 9 commits into
masterfrom
codex/cross-module-inline
Open

cknitt wants to merge 9 commits into
masterfrom
codex/cross-module-inline

Conversation

@cknitt

@cknitt cknitt commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Add @inline(crossModule) to opt individual functions into cross-module inlining, and use it for selected Option helpers.

Inlining copies a function’s body into its callers. If that body changes but its type does not, the caller must still be rebuilt; checking only the .cmi type signature would leave the old body in the generated JavaScript.

The compiler stores exported inline bodies in the existing .cmj file, alongside other JavaScript compilation metadata. Rewatch now compares the digest already stored in that file’s header before and after compiling a module. If it changes, Rewatch rebuilds dependents. This keeps implementation details out of .cmi and requires no new artifact. A .cmj change unrelated to inlining may also cause a dependent rebuild.

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 56.25000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.74%. Comparing base (25e3dfb) to head (1dbd977).

Files with missing lines Patch % Lines
compiler/core/lam_stats_export.ml 66.66% 2 Missing ⚠️
compiler/ml/translattribute.ml 33.33% 2 Missing ⚠️
compiler/core/lam_analysis.ml 0.00% 1 Missing ⚠️
compiler/core/lam_closure.ml 75.00% 1 Missing ⚠️
compiler/ml/printlambda.ml 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8689      +/-   ##
==========================================
+ Coverage   81.72%   81.74%   +0.02%     
==========================================
  Files         460      460              
  Lines       64817    64827      +10     
==========================================
+ Hits        52969    52995      +26     
+ Misses      11848    11832      -16     
Files with missing lines Coverage Δ
compiler/core/js_cmj_format.ml 92.98% <100.00%> (+0.12%) ⬆️
compiler/ml/lambda.ml 71.13% <ø> (ø)
compiler/core/lam_analysis.ml 78.23% <0.00%> (-0.69%) ⬇️
compiler/core/lam_closure.ml 74.39% <75.00%> (+0.97%) ⬆️
compiler/ml/printlambda.ml 8.74% <0.00%> (-0.04%) ⬇️
compiler/core/lam_stats_export.ml 88.23% <66.66%> (-2.88%) ⬇️
compiler/ml/translattribute.ml 75.55% <33.33%> (+1.64%) ⬆️

... and 7 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 Sep 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 1dbd977

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt marked this pull request as ready for review September 28, 2026 10:04

@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: 24646c5cb8

ℹ️ 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 compiler/core/lam_stats_export.ml Resolved
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Changelog

🚀 New Feature

  • Allow selected functions to opt into cross-module inlining with @inline(crossModule) and inline common Option helpers in application code.

Semantic Diff

9 commits · Full page

✏️ Enable selective cross-module inlining for Option helpers

bb8a422 · cknitt · Sep 28, 2026, 06:04 UTC · edited

@inline(crossModule) marks a closed function whose body the .cmj exports, so that other modules inline its calls as they would a local function. Option's forEach, map and flatMap use it.

✏️ Inline additional Option helpers across modules

e33d56d · cknitt · Sep 28, 2026, 07:46 UTC · edited

Option's filter, mapOr, getOr, orElse, isSome and isNone use @inline(crossModule) too.

✏️ Document cross-module inlining feature

cd458ae · cknitt · Sep 28, 2026, 07:49 UTC · edited

Add the changelog fragment for @inline(crossModule) and the Option helpers.

✏️ Refresh Option inlining snapshots after clean build

d44363e · cknitt · Sep 28, 2026, 07:55 UTC · edited

A clean build of the tests inlines the Option helpers in more places than the incremental build the snapshots were made with.

✏️ Track CMJ changes for cross-module inlining

008acbd · cknitt · Sep 28, 2026, 09:19 UTC · edited

Rewatch now compares the .cmj itself instead of a separate .cmj.inline digest: exported JavaScript metadata, inline bodies included, can change without changing the .cmi, and callers compiled against the old .cmj are then stale.

✏️ Read CMJ digest header for incremental rebuilds

b08d5b8 · cknitt · Sep 28, 2026, 09:53 UTC · edited

The compiler writes the digest of a .cmj's contents in its first 16 bytes, so Rewatch can read that instead of hashing the whole file.

Recompile dependents of a changed CMJ in the OCaml build system

b2066b0 · cknitt + Claude · Oct 27, 2026, 07:15 UTC

Why

The OCaml build system, now the default rescript, recompiled dependents only when a module's CMI changed. An @inline(crossModule) body lives in the CMJ and can change while the CMI stays the same, so callers kept the old inlined code, during a build and on the next build after the dependency was compiled separately. Rust rewatch already compares CMJ digests since the previous commits.

Publishing a byte-different CMJ now marks dependents dirty like a changed CMI, and the startup freshness check compares a dependent's CMT with the later of its dependency's CMI and CMJ.

Review

  • Does every implementation change now recompile dependents? Only those whose CMJ bytes change, matching the Rust implementation.
Treat catch binders as bound in Lam_closure.free_variables

01e3b54 · cknitt + Claude · Oct 29, 2026, 09:51 UTC

Answers the Codex finding that an @inline(crossModule) function whose catch handler inspects the exception was rejected as capturing a local value: the Ltrywith branch never added its exception binder to the local set, so the handler's uses of it counted as free variables.

Refresh Option inlining snapshots after rebasing onto master

1dbd977 · cknitt + Claude · Oct 30, 2026, 08:55 UTC

The Temporal tests that master added call Option.isNone and Option.map, which are now inlined.

@cknitt

cknitt commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 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-10-29T09:54:08.674988Z ae74e35 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. Another round soon, please!

Reviewed commit: ae74e35e1a

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

`@inline(crossModule)` marks a closed function whose body the .cmj exports, so
that other modules inline its calls as they would a local function. Option's
forEach, map and flatMap use it.

Signed-off-by: Christoph Knittel <ck@cca.io>
Option's filter, mapOr, getOr, orElse, isSome and isNone use
`@inline(crossModule)` too.

Signed-off-by: Christoph Knittel <ck@cca.io>
Add the changelog fragment for `@inline(crossModule)` and the Option helpers.

Signed-off-by: Christoph Knittel <ck@cca.io>
A clean build of the tests inlines the Option helpers in more places than the
incremental build the snapshots were made with.

Signed-off-by: Christoph Knittel <ck@cca.io>
Rewatch now compares the .cmj itself instead of a separate .cmj.inline digest:
exported JavaScript metadata, inline bodies included, can change without
changing the .cmi, and callers compiled against the old .cmj are then stale.

Signed-off-by: Christoph Knittel <ck@cca.io>
The compiler writes the digest of a .cmj's contents in its first 16 bytes, so
Rewatch can read that instead of hashing the whole file.

Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
    The OCaml build system, now the default `rescript`, recompiled
    dependents only when a module's CMI changed. An `@inline(crossModule)`
    body lives in the CMJ and can change while the CMI stays the same, so
    callers kept the old inlined code, during a build and on the next build
    after the dependency was compiled separately. Rust rewatch already
    compares CMJ digests since the previous commits.

Publishing a byte-different CMJ now marks dependents dirty like a changed
CMI, and the startup freshness check compares a dependent's CMT with the
later of its dependency's CMI and CMJ.

Review: Does every implementation change now recompile dependents? Only
those whose CMJ bytes change, matching the Rust implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKsWykY6KH4QSJ9Wrs
Signed-off-by: Christoph Knittel <ck@cca.io>
Answers the Codex finding that an `@inline(crossModule)` function whose
`catch` handler inspects the exception was rejected as capturing a local
value: the `Ltrywith` branch never added its exception binder to the local
set, so the handler's uses of it counted as free variables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <ck@cca.io>
The Temporal tests that master added call Option.isNone and Option.map,
which are now inlined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the codex/cross-module-inline branch 2 times, most recently from 5899ffe to 1dbd977 Compare October 30, 2026 09:23

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