Repository navigation
Unwrap option arguments of inlined functions directly when they can't be nested - #9504
Conversation
Changelog💅 Polish
Semantic Diff1 commit · Full page Unwrap option arguments of inlined functions directly when they can't be nestedd02e911 · cknitt + Claude · Dec 02, 2026, 09:44 UTC
Lambda added (Pnot_nested_option) let intOr = (opt: option<int>) => opt->Option.getOr(0)
(function opt/1002
- (apply (field:getOr/6 global Stdlib_Option!) opt/1002 0))
+ (apply (field:getOr/6 global Stdlib_Option!)
+ (#not_nested_option opt/1002) 0))JS modified (inlined Option helpers) let intOr = (opt: option<int>) => opt->Option.getOr(0)
let paramOr = (opt: option<int>, default) => opt->Option.getOr(default)
let nestedOr = (opt: option<option<int>>) => opt->Option.getOr(Some(0))
function intOr(opt) {
if (opt !== undefined) {
- return Primitive_option.valFromOption(opt);
+ return opt;
} else {
return 0;
}
}
function paramOr(opt, $$default) {
if (opt !== undefined) {
- return Primitive_option.valFromOption(opt);
+ return opt;
} else {
return $$default;
}
}
function nestedOr(opt) {
if (opt !== undefined) {
return Primitive_option.valFromOption(opt);
} else {
return 0;
}
}Review
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #9504 +/- ##
==========================================
+ Coverage 82.56% 82.59% +0.03%
==========================================
Files 468 468
Lines 66228 66358 +130
==========================================
+ Hits 54678 54809 +131
+ Misses 11550 11549 -1
🚀 New features to boost your workflow:
|
|
| Benchmark | Base | Head | Change |
|---|---|---|---|
packages/@rescript/belt/src/Belt_SetInt.res |
1,264,507 | 1,296,081 | +2.50% |
packages/@rescript/belt/src/Belt_SetString.res |
1,265,235 | 1,296,809 | +2.50% |
packages/@rescript/belt/src/Belt_SetDict.res |
1,416,364 | 1,446,419 | +2.12% |
packages/@rescript/belt/src/Belt_internalSetInt.res |
713,443 | 728,489 | +2.11% |
packages/@rescript/belt/src/Belt_internalSetString.res |
714,632 | 729,678 | +2.11% |
packages/@rescript/belt/src/Belt_internalMapInt.res |
1,370,477 | 1,397,438 | +1.97% |
packages/@rescript/belt/src/Belt_internalMapString.res |
1,372,321 | 1,399,282 | +1.96% |
packages/@rescript/belt/src/Belt_MapDict.res |
1,594,363 | 1,619,371 | +1.57% |
packages/@rescript/belt/src/Belt_MutableMapInt.res |
1,123,885 | 1,138,718 | +1.32% |
packages/@rescript/belt/src/Belt_MutableMapString.res |
1,124,164 | 1,138,997 | +1.32% |
29 more changes
| Benchmark | Base | Head | Change |
|---|---|---|---|
packages/@rescript/belt/src/Belt_MutableMap.res |
1,265,012 | 1,281,032 | +1.27% |
packages/@rescript/belt/src/Belt_internalAVLset.res |
2,758,246 | 2,791,875 | +1.22% |
packages/@rescript/belt/src/Belt_MapInt.res |
1,161,108 | 1,174,537 | +1.16% |
packages/@rescript/belt/src/Belt_MapString.res |
1,161,621 | 1,175,050 | +1.16% |
packages/@rescript/belt/src/Belt_MutableSetInt.res |
1,603,479 | 1,619,909 | +1.02% |
packages/@rescript/belt/src/Belt_MutableSetString.res |
1,604,712 | 1,621,142 | +1.02% |
packages/@rescript/belt/src/Belt_internalAVLtree.res |
3,361,982 | 3,395,255 | +0.99% |
packages/@rescript/belt/src/Belt_MutableSet.res |
1,830,988 | 1,847,921 | +0.92% |
stress/InlineChain.res |
3,385,347 | 3,401,524 | +0.48% |
packages/@rescript/runtime/Primitive_object.res |
1,107,748 | 1,111,666 | +0.35% |
packages/@rescript/belt/src/Belt_SortArray.res |
1,368,169 | 1,372,909 | +0.35% |
packages/@rescript/belt/src/Belt_MutableQueue.res |
764,305 | 766,916 | +0.34% |
packages/@rescript/belt/src/Belt_SortArrayInt.res |
1,270,599 | 1,274,369 | +0.30% |
packages/@rescript/belt/src/Belt_SortArrayString.res |
1,272,390 | 1,276,160 | +0.30% |
build/shared-edit |
84,829,641 | 85,007,844 | +0.21% |
packages/@rescript/runtime/Stdlib_List.res |
2,580,934 | 2,586,108 | +0.20% |
packages/@rescript/belt/src/Belt_List.res |
3,060,863 | 3,066,746 | +0.19% |
build/leaf-edit |
11,642,470 | 11,663,128 | +0.18% |
build/no-op |
9,011,628 | 9,027,504 | +0.18% |
packages/@rescript/belt/src/Belt_Range.res |
280,051 | 280,509 | +0.16% |
packages/@rescript/belt/src/Belt_Map.res |
954,248 | 955,747 | +0.16% |
packages/@rescript/belt/src/Belt_Set.res |
834,117 | 835,386 | +0.15% |
packages/@rescript/belt/src/Belt_internalBuckets.res |
935,505 | 936,840 | +0.14% |
stress/LetRec.res |
26,331,341 | 26,367,372 | +0.14% |
packages/@rescript/belt/src/Belt_HashSet.res |
947,041 | 948,336 | +0.14% |
packages/@rescript/belt/src/Belt_MutableStack.res |
378,915 | 379,423 | +0.13% |
packages/@rescript/belt/src/Belt_HashMap.res |
1,161,604 | 1,163,146 | +0.13% |
packages/@rescript/belt/src/Belt_internalSetBuckets.res |
680,562 | 681,287 | +0.11% |
build/clean |
255,056,554 | 255,319,860 | +0.10% |
Instructions
In total: 8,967,192,566 → 8,969,653,833 (+0.03%).
| Benchmark | Base | Head | Change |
|---|---|---|---|
packages/@rescript/belt/src/Belt_internalAVLtree.res |
134,677,036 | 136,065,362 | +1.03% |
stress/InlineChain.res |
136,870,188 | 137,221,484 | +0.26% |
packages/@rescript/runtime/Stdlib_Math.resi |
21,354,580 | 21,307,249 | -0.22% |
packages/@rescript/runtime/Stdlib_List.resi |
24,638,423 | 24,590,580 | -0.19% |
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res |
24,222,988 | 24,178,216 | -0.18% |
parse/RedBlackTreeNoComments.res |
14,569,526 | 14,543,041 | -0.18% |
packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res |
25,671,195 | 25,625,068 | -0.18% |
packages/@rescript/runtime/Stdlib_String.resi |
29,503,735 | 29,456,507 | -0.16% |
parse/RedBlackTree.res |
14,974,912 | 14,951,370 | -0.16% |
packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res |
31,338,081 | 31,293,120 | -0.14% |
7 more changes
| Benchmark | Base | Head | Change |
|---|---|---|---|
packages/@rescript/belt/src/Belt_List.res |
116,852,889 | 117,019,518 | +0.14% |
packages/@rescript/runtime/Stdlib_Date.resi |
34,298,520 | 34,250,973 | -0.14% |
packages/@rescript/belt/src/Belt_Array.resi |
33,821,927 | 33,775,941 | -0.14% |
packages/@rescript/belt/src/Belt_List.resi |
39,045,718 | 38,996,304 | -0.13% |
stress/LetRec.res |
1,222,964,826 | 1,224,506,899 | +0.13% |
packages/@rescript/runtime/Stdlib_Array.resi |
40,064,783 | 40,017,671 | -0.12% |
packages/@rescript/runtime/Stdlib_TypedArray.res |
41,418,462 | 41,370,532 | -0.12% |
Output size
In total: 27,460,200 → 27,462,362 (+0.01%).
| Benchmark | Base | Head | Change |
|---|---|---|---|
output/playground/compiler.js |
1,904,574 | 1,906,736 | +0.11% |
Pull request d02e911 compared with base e489471 in this run.
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
cfe78d2 to
06215dc
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. Bravo. 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". |
06215dc to
bf01388
Compare
|
@codex review |
bf01388 to
2bdce98
Compare
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
… be nested
Why:
An inlined function such as `Option.getOr` is compiled for any `'a`, so
its body unwraps its option with `Primitive_option.valFromOption`, in
case the payload is itself an option. The caller knows the payload type.
A `switch` written at the call site reads the value directly when the
payload can't be `undefined`; the inlined copy now does the same.
The translation of a call marks each option argument whose payload type
can't be `undefined` with an identity primitive, `Pnot_nested_option`.
When the call is inlined, the body is rewritten for that call: unwrapping
a marked parameter becomes `Pval_from_option_not_nest`, a plain read, and
a marked parameter the body passes to another call is marked there too.
A mark compiles to nothing, so calls that aren't inlined don't change.
Lambda added (Pnot_nested_option):
Example:
let intOr = (opt: option<int>) => opt->Option.getOr(0)
Before (lambda):
(function opt/1002
(apply (field:getOr/6 global Stdlib_Option!) opt/1002 0))
After (lambda):
(function opt/1002
(apply (field:getOr/6 global Stdlib_Option!)
(#not_nested_option opt/1002) 0))
JS modified (inlined Option helpers):
Example:
let intOr = (opt: option<int>) => opt->Option.getOr(0)
let paramOr = (opt: option<int>, default) => opt->Option.getOr(default)
let nestedOr = (opt: option<option<int>>) => opt->Option.getOr(Some(0))
Before (js):
function intOr(opt) {
if (opt !== undefined) {
return Primitive_option.valFromOption(opt);
} else {
return 0;
}
}
function paramOr(opt, $$default) {
if (opt !== undefined) {
return Primitive_option.valFromOption(opt);
} else {
return $$default;
}
}
function nestedOr(opt) {
if (opt !== undefined) {
return Primitive_option.valFromOption(opt);
} else {
return 0;
}
}
After (js):
function intOr(opt) {
if (opt !== undefined) {
return opt;
} else {
return 0;
}
}
function paramOr(opt, $$default) {
if (opt !== undefined) {
return opt;
} else {
return $$default;
}
}
function nestedOr(opt) {
if (opt !== undefined) {
return Primitive_option.valFromOption(opt);
} else {
return 0;
}
}
Review: Why is the fact kept per call and not per variable? A variable
can reach a call with a type of its own: `f((Obj.magic(y): option<int>))`
or a coercion translates to plain `y`. Recording `y` as not nested would
also turn a correct `switch y` elsewhere into a plain read. So only the
inlined body is rewritten, and `y` keeps `valFromOption` everywhere else.
Review: Do the marks get in the way of other optimizations? Beta
reduction substitutes through a mark, the inlining size ignores it, and
the guard on inlining other modules' functions treats a marked parameter
like a plain one. Apart from the dropped `valFromOption` calls, the
generated JavaScript doesn't change.
Review: What does it cost to compile? Each call argument of an option
type gets a mark and the payload check a `switch` on it does. Type
abbreviations are read from their declarations first, and expanded only
when that check fails or the declaration ends in a bare parameter, so
arguments of other types cost almost nothing. Belt's tree modules, which
pass an option to nearly every call, allocate 1–3% more words;
`Belt_List` 0.1%.
Review: What stays? `Option.map` still wraps its result with
`Primitive_option.some`, since the body doesn't know the result type
either. That needs a second mark, on the result, and is left for later.
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>
2bdce98 to
d02e911
Compare
…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 call whose own result type is `option<t>`, where
`t` can't be `undefined`, wraps the call in the mark that #9504 put on
arguments, `Pnot_nested_option`. When such a call is inlined, the body
is rewritten for it: a `Some` in return position is built without
wrapping, and a call in return position gets the mark, so that a
wrapper of `Option.map` is inlined the same way. A mark compiles to
nothing, and a marked call is compiled in its place, so tail calls stay
loops.
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
(#not_nested_option
(apply (field:map/4 global Stdlib_Option!)
(#not_nested_option opt/1002) f/1003)))
Lambda modified (body inlined at a marked call):
Example:
let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
Before (lambda): Lam_beta_reduce.mark_result, Option.map inlined
(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;
}
let x = opt;
return Primitive_option.some(x > 0 ? x : undefined);
}
After (js):
function mapped(opt, f) {
if (opt !== undefined) {
return f(opt);
}
}
function nested(opt) {
if (opt === undefined) {
return;
}
let x = opt;
return Primitive_option.some(x > 0 ? x : 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) {
let opt = Primitive_option.some(f(x));
if (opt !== undefined) {
return opt;
} else {
return 0;
}
}
After (js):
function knownSome(x, f) {
let opt = f(x);
if (opt !== undefined) {
return opt;
} else {
return 0;
}
}
Review: Why is it sound to trust the caller's type? The rewrite trusts
only the type of the call itself, never a variable's. A `switch` at the
call site on a value of that type already reads the payload directly, so a
body that returned a wrapped or `undefined` payload under that type would
already break the caller; an `Obj.magic` inside the body opens no new
hole. A cast at the call site is not trusted: `magic(g(x))` passed where
`option<int>` is expected is no longer marked as an argument, since the
call's own type has a nested payload, so `g`'s body keeps its wrapping.
Review: Do the marks get in the way of other optimizations? The code
generator compiles a marked call as the call, with the same continuation,
so a self tail call is still a loop. Beta reduction of a body that is one
marked call, the `let x = e; f(x)` rewrite and the flattening of beta
residues look through the mark, and the inlining size ignores it. Without
each of these look-throughs, a test case gets a temporary: a forwarding
function's arguments, `let x = e; f(x)`, and a tuple bound before a call.
Apart from the dropped `Primitive_option.some` calls, the generated
JavaScript of the tests, the runtime and Belt doesn't change.
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, so a result that isn't an option costs a lookup.
Compiling the runtime and Belt allocates 0.22% more words in all; Belt's
mutable set and map modules the most, 0.9%; `Stdlib_Option` 0.06%.
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>
…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`. When such a call is inlined, the callee's body
is rewritten for it before the arguments are substituted: 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.
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): Lam_beta_reduce.mark_result, Option.map's body
(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;
}
let x = opt;
return Primitive_option.some(x > 0 ? x : undefined);
}
After (js):
function mapped(opt, f) {
if (opt !== undefined) {
return f(opt);
}
}
function nested(opt) {
if (opt === undefined) {
return;
}
let x = opt;
return Primitive_option.some(x > 0 ? x : 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) {
let opt = Primitive_option.some(f(x));
if (opt !== undefined) {
return opt;
} else {
return 0;
}
}
After (js):
function knownSome(x, f) {
let opt = f(x);
if (opt !== undefined) {
return opt;
} else {
return 0;
}
}
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.
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: Is a flagged call a marked argument? Yes: an argument whose own
call is flagged is an option whose payload can't be `undefined`, by that
call's type, so the inlined body unwraps the parameter it is bound to
directly, as for an argument marked by its type.
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.8% fewer words in all; Belt's list and sorted array modules 2.5% fewer;
no module more than 0.15% more.
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>
…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;
}
let x = opt;
return Primitive_option.some(x > 0 ? x : undefined);
}
After (js):
function mapped(opt, f) {
if (opt !== undefined) {
return f(opt);
}
}
function nested(opt) {
if (opt === undefined) {
return;
}
let x = opt;
return Primitive_option.some(x > 0 ? x : 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) {
let opt = Primitive_option.some(f(x));
if (opt !== undefined) {
return opt;
} else {
return 0;
}
}
After (js):
function knownSome(x, f) {
let opt = f(x);
if (opt !== undefined) {
return opt;
} else {
return 0;
}
}
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: 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. Without it, `let v = (o, f) => Inline_export.mapPair(o, f)`
in the tests returned `Primitive_option.some(f(o))`: the call to
`Option.map` in `mapPair` reads parameters, so it is inlined only after
the body is exported.
Review: Is a flagged call a marked argument? Yes: an argument whose own
call is flagged is an option whose payload can't be `undefined`, by that
call's type, so the inlined body unwraps the parameter it is bound to
directly, as for an argument marked by its type.
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.8% fewer words in all; Belt's list and sorted array modules 2.5% fewer;
no module more than 0.15% more.
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>
Follow-up from #8733. An inlined function such as
Option.getOris compiled for any'a, so its body unwraps its option withPrimitive_option.valFromOption, in case the payload is itself an option. The call site knows the payload type, so the inlined copy can now read the value directly, like aswitchwritten there.How it works:
undefinedin an identity primitive,Pnot_nested_option.Lam_beta_reducerewritesvalFromOption(param)in the inlined body toPval_from_option_not_nest(param)(a plain read) for marked parameters, marks the parameter again where the body passes it to another call, and substitutes the argument without the mark.The fact is kept per call, not per variable.
f((Obj.magic(y): option<int>))or a coercion translates to plainy. Recordingyitself as not nested would break a correctswitch yelsewhere; the new test has a case for it.Nested options,
Nullable.tpayloads and generic'akeepvalFromOption.Tests: new
inline_option_unwrap_test(direct reads, other modules' functions with parameter arguments, a generic wrapper, type abbreviations of options, a variable bound toNullable.toOption, payloads that can be undefined, the cast case). Seven existing outputs losevalFromOptioncalls.Compile cost: each option argument gets a mark and the same payload check a
switchon it does; type abbreviations are read from their declarations without instantiating them. Total allocated words +0.15%. The benchmarks check is red because Belt's set and map modules, which pass an option tree to nearly every call, allocate 1–2.5% more.Left for later:
Option.mapstill wraps its result withPrimitive_option.some, because the body doesn't know the result type either. That needs a second mark, on the result.Some(e)arguments and partial applications (f(a, ...)) aren't marked, so they keepvalFromOptionas on master.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq