Skip to content

Fix inlining copying a record literal to each use - #9493

Merged
cknitt merged 4 commits into
masterfrom
fix-inline-record-literal-arg
Dec 2, 2026
Merged

cknitt merged 4 commits into
masterfrom
fix-inline-record-literal-arg

Conversation

@cknitt

@cknitt cknitt commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #9490.

Inlining copied a record literal argument to each use of the parameter:

type xy = {x: int, y: int}

@inline
let same = (b: xy) => b === b

Console.log(same({x: 1, y: 2})) // false
console.log(({ x: 1, y: 2 }) === ({ x: 1, y: 2 }));

Each copy builds a new object, so === on the parameter was false and each use allocated. Tuple literals and Some around a record had the same problem.

The fix. Lam_beta_reduce now substitutes a constant argument at each use only if it doesn't compile to an object literal. The new Lambda.const_has_identity says which constants do: a block, such as a record or tuple, or Some around one. Such an argument is bound to a variable, like any other argument, so the example compiles to let b = {…}; console.log(b === b). A parameter used once still gets the literal in place, and field reads still fold. A nested none such as Some(None) is still repeated: Primitive_option.some returns one frozen marker object for each nesting depth, so every copy is the same object and === still holds.

Switches. A switch on the copied literal used to fold, for example on Ok(5) in Stdlib_ResultTests. To keep that, Lam_pass_remove_alias now folds a switch on a variable bound to a constant, as it already did for string switches. This also folds switches on top-level variant constants in five existing tests.

Tests. inline_literal_arg_identity_test checks === on record, tuple and option arguments, and a field read. The unit tests in ounit_constant_argument_tests check both rules on Lambda directly: a block argument is bound once and a nested none is repeated, and a switch on a variable bound to a constant takes its case or keeps the variable.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix inlining that built a record, tuple or option literal argument again at each use of the parameter.

Semantic Diff

4 commits · Full page

Fix inlining copying a record literal to each use

5482bf5 · cknitt + Claude · Dec 01, 2026, 19:41 UTC

Why

When Lam_beta_reduce inlines a call, it substituted a constant argument for each use of its parameter. A record, tuple or option literal compiles to a new object at each copy, so b === b on the parameter was false and each use allocated.

Beta reduction now substitutes only constants that allocate nothing. Other constants are bound to a variable, like any other argument.

A switch on the copied literal used to fold. To keep that, Lam_pass_remove_alias now folds a switch on a variable bound to a constant, as it already did for a string switch. This also folds switches on top-level variant constants in several existing tests.

JS modified (record literal argument)

type xy = {x: int, y: int}

@inline
let same = (b: xy) => b === b

Console.log(same({x: 1, y: 2}))

js

-console.log(({
-  x: 1,
-  y: 2
-}) === ({
+let b = {
   x: 1,
   y: 2
-}));
+};
+
+console.log(b === b);

Fixes #9490

Repeat a nested none argument of an inlined function

bf8b52d · cknitt + Claude · Dec 01, 2026, 19:41 UTC

Why

The previous commit bound every allocating constant argument to a variable. That includes a nested none, such as Some(None), which compiles to a Primitive_option.some(undefined) call. But every evaluation of Some(None) already gives a different object, so copying it can't change what === returns, and the binding protected nothing. Its lets only made the output longer: in option_repr_test, a let inside a list element forced the elements before it into temporaries.

Beta reduction now binds only constants that compile to an object literal: a block, or Some around one. The new Lambda.const_has_identity says which those are. option_repr_test's output is the same as on master again.

Add Lambda unit tests for constant arguments and switches

d4edf3f · cknitt + Claude · Dec 01, 2026, 20:22 UTC

The earlier commits were tested only through generated JavaScript. These unit tests check the two Lambda invariants directly. Beta reduction binds a block argument once and repeats a nested none. The alias pass takes the case of a switch on a variable bound to a constant, and keeps the variable when no case is taken.

✨ Give the right reason why repeating a nested none is safe

0682c69 · cknitt + Claude · Dec 02, 2026, 06:13 UTC · new

The comment on Lambda.const_has_identity said that every evaluation of Some(None) gives a different object. It is the other way round: Primitive_option.some returns one frozen marker object for each nesting depth, so every copy is the same object and === still holds. That is why repeating the constant is safe.

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.16393% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.53%. Comparing base (69e11bf) to head (0682c69).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
tests/ounit_tests/ounit_constant_argument_tests.ml 90.69% 4 Missing ⚠️
compiler/typing/lambda.ml 71.42% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9493      +/-   ##
==========================================
+ Coverage   82.49%   82.53%   +0.03%     
==========================================
  Files         467      469       +2     
  Lines       66125    66223      +98     
==========================================
+ Hits        54553    54657     +104     
+ Misses      11572    11566       -6     
Files with missing lines Coverage Δ
compiler/backend/lam_beta_reduce.ml 78.26% <100.00%> (+0.48%) ⬆️
compiler/backend/lam_pass_remove_alias.ml 93.07% <100.00%> (+0.39%) ⬆️
tests/ounit_tests/ounit_tests_main.ml 100.00% <ø> (ø)
compiler/typing/lambda.ml 69.76% <71.42%> (+0.08%) ⬆️
tests/ounit_tests/ounit_constant_argument_tests.ml 90.69% <90.69%> (ø)

... and 19 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.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 651,857,432 → 651,844,790 (±0.00%).

Generated projects: 627,040,981 → 627,041,353 (±0.00%).

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

Instructions

In total: 8,963,894,374 → 8,963,853,272 (±0.00%).

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

Output size

In total: 27,394,735 → 27,394,967 (±0.00%).

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

Pull request 0682c69 compared with base 0fe7cd9 in this run.

@cknitt
cknitt force-pushed the fix-inline-record-literal-arg branch from 450c879 to 2bd65f8 Compare December 1, 2026 18:26
@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 0682c69

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

@cknitt

cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 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-12-01T20:10:42.167760Z bf8b52d 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. Hooray!

Reviewed commit: 2bd65f8359

ℹ️ 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 force-pushed the fix-inline-record-literal-arg branch from 2bd65f8 to 4944ae4 Compare December 1, 2026 19:27
@cknitt
cknitt marked this pull request as ready for review December 1, 2026 19:27
@cknitt cknitt changed the title Build a literal argument of an inlined function once Fix inlining copying a record literal to each use Dec 1, 2026
Why:
    When Lam_beta_reduce inlines a call, it substituted a constant
    argument for each use of its parameter. A record, tuple or option
    literal compiles to a new object at each copy, so `b === b` on the
    parameter was false and each use allocated.

Beta reduction now substitutes only constants that allocate nothing.
Other constants are bound to a variable, like any other argument.

A switch on the copied literal used to fold. To keep that,
Lam_pass_remove_alias now folds a switch on a variable bound to a
constant, as it already did for a string switch. This also folds
switches on top-level variant constants in several existing tests.

JS modified (record literal argument):
Example:
    type xy = {x: int, y: int}

    @inline
    let same = (b: xy) => b === b

    Console.log(same({x: 1, y: 2}))
Before (js):
    console.log(({
      x: 1,
      y: 2
    }) === ({
      x: 1,
      y: 2
    }));
After (js):
    let b = {
      x: 1,
      y: 2
    };

    console.log(b === b);

Fixes #9490

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the fix-inline-record-literal-arg branch 2 times, most recently from 3fc5d29 to 0abd863 Compare December 1, 2026 19:48
Why:
    The previous commit bound every allocating constant argument to a
    variable. That includes a nested none, such as `Some(None)`, which
    compiles to a `Primitive_option.some(undefined)` call. But every
    evaluation of `Some(None)` already gives a different object, so
    copying it can't change what `===` returns, and the binding
    protected nothing. Its `let`s only made the output longer: in
    option_repr_test, a `let` inside a list element forced the elements
    before it into temporaries.

Beta reduction now binds only constants that compile to an object
literal: a block, or `Some` around one. The new Lambda.const_has_identity
says which those are. option_repr_test's output is the same as on master
again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the fix-inline-record-literal-arg branch from 0abd863 to bf8b52d Compare December 1, 2026 19:48
@cknitt

cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@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: bf8b52d6f0

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

The earlier commits were tested only through generated JavaScript. These
unit tests check the two Lambda invariants directly. Beta reduction binds
a block argument once and repeats a nested none. The alias pass takes the
case of a switch on a variable bound to a constant, and keeps the
variable when no case is taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt requested a review from cristianoc December 1, 2026 20:49

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Non-blocking comment: Primitive_option.some reuses a frozen marker per nesting depth, so repeated Some(None) calls preserve === because they return the same marker, not because each call allocates a fresh object. Please update the rationale in const_has_identity.

The comment on Lambda.const_has_identity said that every evaluation of
`Some(None)` gives a different object. It is the other way round:
Primitive_option.some returns one frozen marker object for each nesting
depth, so every copy is the same object and `===` still holds. That is
why repeating the constant is safe.

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

cknitt commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Thanks, you are right: Primitive_option.some returns the cached frozen marker for each depth, so copies of Some(None) are the same object. Fixed the rationale in const_has_identity in 0682c69 and in the PR description; the code is unchanged. (Reply by Claude.)

@cknitt
cknitt enabled auto-merge (squash) December 2, 2026 06:18
@cknitt
cknitt merged commit f570bfb into master Dec 2, 2026
23 checks passed
@cknitt
cknitt deleted the fix-inline-record-literal-arg branch December 2, 2026 06:24
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.

Inlining copies a record literal argument to each use, which changes ===

2 participants