Repository navigation
Fix inlining copying a record literal to each use - #9493
Conversation
Changelog🐛 Bug fix
Semantic Diff4 commits · Full page Fix inlining copying a record literal to each use5482bf5 · cknitt + Claude · Dec 01, 2026, 19:41 UTC
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}))
-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 functionbf8b52d · cknitt + Claude · Dec 01, 2026, 19:41 UTC
Beta reduction now binds only constants that compile to an object literal: a block, or Add Lambda unit tests for constant arguments and switchesd4edf3f · 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 safe0682c69 · cknitt + Claude · Dec 02, 2026, 06:13 UTC · new The comment on Lambda.const_has_identity said that every evaluation of |
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
✅ Compiler benchmarksAllocated wordsIn 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. InstructionsIn total: 8,963,894,374 → 8,963,853,272 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn 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. |
450c879 to
2bd65f8
Compare
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9493 |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
2bd65f8 to
4944ae4
Compare
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>
3fc5d29 to
0abd863
Compare
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>
0abd863 to
bf8b52d
Compare
|
@codex review |
There was a problem hiding this comment.
💡 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>
cristianoc
left a comment
There was a problem hiding this comment.
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>
|
Thanks, you are right: |
Fixes #9490.
Inlining copied a record literal argument to each use of the parameter:
Each copy builds a new object, so
===on the parameter was false and each use allocated. Tuple literals andSomearound a record had the same problem.The fix.
Lam_beta_reducenow substitutes a constant argument at each use only if it doesn't compile to an object literal. The newLambda.const_has_identitysays which constants do: a block, such as a record or tuple, orSomearound one. Such an argument is bound to a variable, like any other argument, so the example compiles tolet b = {…}; console.log(b === b). A parameter used once still gets the literal in place, and field reads still fold. A nested none such asSome(None)is still repeated:Primitive_option.somereturns 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)inStdlib_ResultTests. To keep that,Lam_pass_remove_aliasnow 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_testchecks===on record, tuple and option arguments, and a field read. The unit tests inounit_constant_argument_testscheck 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