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
modifiedLambda body inlined at a flagged call
let mapped = (opt: option<int>, f: int => int) => opt->Option.map(f)lambda
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
modifiedJS a chain of Option helpers
let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)js
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 ownSomes and calls are: a cast argument that the body returns, as inkeep(magic(g(a)), true)withkeep = (o, _) => o, keeps its wrapping. Aswitchat the call site on a value of the call's type already reads the payload directly, so a body that returned a wrapped orundefinedpayload under that type, through a cast or an external, would already break the caller; such a body inlined here now returns the payload as aSomewritten here would. A cast of the called function counts the same way, since the call's type comes from the function's type: withcallOpt = (g: int => option<int>, a) => g(a),callOpt(magic(nestedPositive), a)inlinesnestedPositive, of typeint => option<option<int>>, at the callg(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 droppedPrimitive_option.somecalls, 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, asOption.mapinmapPairof 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
intorunitis 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).