Repository navigation
Conversation
Changelog💅 Polish
Semantic Diff1 commit · Full page Return Some of inlined functions without wrapping when the payload can't be undefined6883faf · cknitt + Claude · Dec 05, 2026, 10:05 UTC
Lambda modified (result of a call) let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
(function opt/1002 f/1003
- (apply (field:map/4 global Stdlib_Option!)
+ (apply #result_not_nested (field:map/4 global Stdlib_Option!)
(#not_nested_option opt/1002) f/1003))Lambda modified (body inlined at a flagged call) let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
(if (#is_not_none opt/1002)
- (some (apply f/1003 (#val_from_option_not_nest opt/1002)))
+ (some_not_nest (apply f/1003 (#val_from_option_not_nest opt/1002)))
undefined)JS modified (inlined Option.map) let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
let nested = (opt: option<int>) => opt->Option.map(x => x > 0 ? Some(x) : None)
function mapped(opt, f) {
if (opt !== undefined) {
- return Primitive_option.some(f(opt));
+ return f(opt);
}
}
function nested(opt) {
if (opt !== undefined) {
return Primitive_option.some(opt > 0 ? opt : undefined);
}
}JS modified (a chain of Option helpers) let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
function knownSome(x, f) {
- return Primitive_option.some(f(x));
+ return f(x);
}Review
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## no-late-inline #9528 +/- ##
==================================================
- Coverage 82.65% 82.63% -0.02%
==================================================
Files 469 469
Lines 66577 66638 +61
==================================================
+ Hits 55028 55069 +41
- Misses 11549 11569 +20
🚀 New features to boost your workflow:
|
✅ Compiler benchmarksAllocated wordsIn total: 658,250,716 → 658,663,785 (+0.06%). Generated projects: 628,525,972 → 628,539,777 (±0.00%).
11 more changes
InstructionsIn total: 8,976,466,510 → 8,976,822,585 (±0.00%).
Output sizeIn total: 27,660,834 → 27,662,405 (+0.01%).
Pull request 6883faf compared with base 12d3ffc in this run. |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
08cc41a to
d99531b
Compare
|
@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. You're on a roll. 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". |
d99531b to
93beda8
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
93beda8 to
1c5cac6
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
1c5cac6 to
5b18c3e
Compare
5b18c3e to
848634f
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
…n't be undefined
Why:
An inlined function such as `Option.map` is compiled for any `'b`, so
the `Some(v)` it returns is `Primitive_option.some(v)`, in case `v` is
itself `None`. The caller knows the result type. A `Some(v)` written at
the call site is just `v` when the payload can't be `undefined`; the
inlined copy now returns it the same way.
The translation of a full application whose own type is `option<t>`,
where `t` can't be `undefined`, sets a new flag on the call,
`ap_result_not_nested`. The beta reduction that inlines a call takes
the call's flag and rewrites the callee's body for it before it
substitutes the arguments: a `Some` in return position is built without
wrapping, and a call in return position gets the flag, so that a
wrapper of `Option.map` is inlined the same way. The flag is part of
the call, so a flagged call is still a call to every other pass, and a
self tail call stays a loop. An `@inline` body in a `.cmj` keeps its
calls' flags, so another module inlines it the same way.
Lambda modified (result of a call):
Example:
let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
Before (lambda):
(function opt/1002 f/1003
(apply (field:map/4 global Stdlib_Option!)
(#not_nested_option opt/1002) f/1003))
After (lambda):
(function opt/1002 f/1003
(apply #result_not_nested (field:map/4 global Stdlib_Option!)
(#not_nested_option opt/1002) f/1003))
Lambda modified (body inlined at a flagged call):
Example:
let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
Before (lambda): Option.map's body, before its arguments are substituted
(if (#is_not_none opt/1002)
(some (apply f/1003 (#val_from_option_not_nest opt/1002)))
undefined)
After (lambda):
(if (#is_not_none opt/1002)
(some_not_nest (apply f/1003 (#val_from_option_not_nest opt/1002)))
undefined)
JS modified (inlined Option.map):
Example:
let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
let nested = (opt: option<int>) => opt->Option.map(x => x > 0 ? Some(x) : None)
Before (js):
function mapped(opt, f) {
if (opt !== undefined) {
return Primitive_option.some(f(opt));
}
}
function nested(opt) {
if (opt !== undefined) {
return Primitive_option.some(opt > 0 ? opt : undefined);
}
}
After (js):
function mapped(opt, f) {
if (opt !== undefined) {
return f(opt);
}
}
function nested(opt) {
if (opt !== undefined) {
return Primitive_option.some(opt > 0 ? opt : undefined);
}
}
JS modified (a chain of Option helpers):
Example:
let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
Before (js):
function knownSome(x, f) {
return Primitive_option.some(f(x));
}
After (js):
function knownSome(x, f) {
return f(x);
}
Review: Why is it sound to trust the caller's type? The rewrite trusts the
type of the call itself, never the type an argument is used at, which a
cast can change. The flag has its own place on the call, so the argument
mark of #9504 is never read as one: `isSome(magic(opt->Option.map(f)))`
leaves the cast call's wrapping alone. The body is rewritten before the
arguments are substituted, so only its own `Some`s and calls are: a cast
argument that the body returns, as in `keep(magic(g(a)), true)` with
`keep = (o, _) => o`, keeps its wrapping. A `switch` at the call site on a
value of the call's type already reads the payload directly, so a body
that returned a wrapped or `undefined` payload under that type, through a
cast or an external, would already break the caller; such a body inlined
here now returns the payload as a `Some` written here would. A cast of
the called function counts the same way, since the call's type comes from
the function's type: with `callOpt = (g: int => option<int>, a) => g(a)`,
`callOpt(magic(nestedPositive), a)` inlines `nestedPositive`, of type
`int => option<option<int>>`, at the call `g(a)`, and returns its payload
unwrapped.
Review: Does the flag get in the way of other optimizations? No: a
flagged call is still a call, so no pass needs to look through a mark. The
tests' forwarding function, `let x = e; f(x)` and a tuple bound before a
call compile without a temporary, and a self tail call is still a loop.
Apart from the dropped `Primitive_option.some` calls, the generated
JavaScript of the tests, the runtime and Belt doesn't change.
Review: What about a flagged call passed as an argument? It gets the
argument mark of #9504 from its type like any other argument, so the body
it is passed to unwraps it directly. The two marks stay separate: the
flag says what an inlined body may return, the argument mark what a
parameter holds.
Review: Why keep the flag in a `.cmj`? It is a fact about the call's
value at the types of the module that wrote it, so it holds wherever the
body is inlined. Most calls in a stored body are inlined before it is
stored, as `Option.map` in `mapPair` of the tests is, so the flag is rarely
needed there, but keeping it costs nothing.
Review: What does it cost to compile? Each full application checks its
result type the way #9504 checks arguments: abbreviations are read from
their declarations first, and a built-in type such as `int` or `unit` is
known to be no option without a lookup, which saves the lookups #9504
made for such arguments too. Compiling the runtime and Belt allocates
0.2% more words in all, at most 0.8% more for one module (Belt's mutable
sets).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq
Signed-off-by: Christoph Knittel <ck@cca.io>
848634f to
6883faf
Compare
When an inlined function returns
Some(v)and the caller's type says the payload can't beundefined, the inlined copy now returnsvwithoutPrimitive_option.some.Option.mapis written once for every payload type, so itsSome(f(x))must wrap a payload that might beundefined. #9504 let inlined bodies read option arguments directly when the caller's type rules out nesting. This does the same for the result:option<t>, wheretcan't beundefined, gets a flag on the call (ap_result_not_nested). Partial applications and externals aren't flagged.Somein a return position of the body returns the value itself, and a call in a return position is flagged in turn, so wrappers aroundOption.mapbenefit too. Return positions are followed throughif,switch,letbodies, sequences andtry. This happens in both places that inline: the alias pass and the reduction of directly applied functions.Before:
After:
Soundness. The rewrite trusts only the call's own type. The caller's own
switchon that call's result already reads the payload directly whentcan't beundefined, so a body returning a wrapped orundefinedpayload under that type would already break the caller. Two things keep casts at the call site out:keep(magic(g(a))), keeps its wrapping.A cast or an external hiding
undefinedinside the inlined function itself, as inSome(a->Array.getUnsafe(5)), now behaves like the sameSomewritten at the call site. So does a cast of the function itself at the call site, as incallOpt(magic(f), a): the call's type comes from the function's type, and the cast is gone once compiled.A flagged call is still a call, so self tail calls stay loops and no other pass needs to look through a mark.
Compile cost. Each full application checks its result type the way #9504 checks arguments, with a shortcut for built-in types such as
intandunitthat also saves #9504 lookups. Compiling the runtime and Belt allocates 0.2% more words in total, at most 0.8% for one module (Belt's mutable sets). The JavaScript of the runtime and Belt doesn't change.Changed test outputs drop
Primitive_option.some, inOption.mapcalls in the stdlib tests and inint_overflow_test,flat_make_export_testandinline_export_test. The newinline_option_result_testcovers:Option.mapwith a named function and a lambda;@inlinefunction returningSomethroughif,switchandtry;Option.map;Nullableand generic payloads;let x = e; f(x), and a tuple bound before a call;Stacked on #9537. Follow-up of #9504 (#8733).
🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq