Semantic Diff

#9528 Return Some of inlined functions without wrapping when the payload can't be undefined

1 commit

de87d9af6e3d65fc9c5dbb36cc6be7081402bd08..pr-9528

6883faf3c · cknitt + Claude · Dec 05, 2026, 10:05 UTC

Return Some of inlined functions without wrapping when the payload can'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.

modifiedLambda result of a call

let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
lambda
Before
After
(function opt/1002 f/1003(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)) (#not_nested_option opt/1002) f/1003))

modifiedLambda body inlined at a flagged call

let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)
lambda
Before Option.map's body, before its arguments are substituted
After
(if (#is_not_none opt/1002)(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) undefined)

modifiedJS 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)
js
Before
After
function mapped(opt, f) {function mapped(opt, f) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.some(f(opt)); return f(opt); } }}} function nested(opt) {function nested(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.some(opt > 0 ? opt : undefined); return Primitive_option.some(opt > 0 ? opt : undefined); } }}}

modifiedJS a chain of Option helpers

let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
js
Before
After
function knownSome(x, f) {function knownSome(x, f) { return Primitive_option.some(f(x)); 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 Somes 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.
  • 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.
  • 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.
  • 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.
  • 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).