Mark small Belt functions @inline
Why
Since #9479, other modules inline @inline functions, and #9484 marked the stdlib's small helpers, but Belt marked none, so every call to a one-line Belt wrapper such as Belt.Option.getWithDefault stayed a call. 33 functions are now @inline, chosen from the exported functions whose bodies other modules can store: - Belt.Option (9): keep, forEach, mapWithDefault, map, flatMap, getWithDefault, orElse, isSome, isNone; - Belt.Result (8): mapWithDefault, map, flatMap, getWithDefault, isOk, isError, eq, cmp; - lookups (5): Belt.Array.get and set, Belt.List.head, tail and add; - accessors (11): size of the six hash maps and sets, isEmpty, size and peek of MutableQueue, isEmpty and top of MutableStack.
modifiedJS a caller of Belt
let computeArea = ((x, y, z)) => x * y * Belt.Option.getWithDefault(z, 1)let size = (q: Belt.MutableQueue.t<int>) => Belt.MutableQueue.size(q)let hashSize = (h: Belt.HashMap.Int.t<int>) => Belt.HashMap.Int.size(h)
js
Before
After
function computeArea(param) {function computeArea(param) { return (param[0] * param[1] | 0) * Belt_Option.getWithDefault(param[2], 1) | 0; let z = param[2]; return (param[0] * param[1] | 0) * ( z !== undefined ? Primitive_option.valFromOption(z) : 1 ) | 0;}} let size = Belt_MutableQueue.size; function size(q) { return q.length;} let hashSize = Belt_HashMapInt.size; function hashSize(h) { return h.size;}
modifiedJS a Belt.Result caller
let isOk = (a: result<int, string>) => Belt.Result.isOk(a)let rcmp = (a: result<int, string>, b) => Belt.Result.cmp(a, b, (x, y) => x - y)
js
Before
After
let isOk = Belt_Result.isOk; function isOk(a) { return a.TAG === "Ok";} function rcmp(a, b) {function rcmp(a, b) { return Belt_Result.cmp(a, b, (x, y) => x - y | 0); if (a.TAG === "Ok") { if (b.TAG === "Ok") { return a._0 - b._0 | 0; } else { return 1; } } else if (b.TAG === "Ok") { return -1; } else { return 0; }}}
Review
- What was left out? The wrappers of the map and set modules (
Belt.Map,Set, theMutable…ones and theirIntandStringvariants): inlined, each call named Belt's internal modules (Belt_internalAVLset,Belt_internalBucketsType), read thedataandcmpfields and moved its arguments into temporaries, so callers got longer and harder to read. For the same reason the constructors (make,fromArray,clear,copy,MutableStack.push): each call became a record literal or an internal call.IntandFloatfromStringadded a temporary at every call.Option.eqandcmpcame out larger, asOption.equalandcomparedid in #9484. Every…OrThrowand…Exnhelper stays a call, and so do loops and functions that call module-local helpers, such asList.length,Array.everyandMutableStack.size. - Why are the
…Ualiases not marked? An alias such aslet mapU = mapstores no body, so other modules still call it, and giving each its own body would change Belt's JavaScript. They are all deprecated. - How is a wrong
@inlinecaught? Warning 113: Belt builds with warnings as errors.MutableSet.mergeManyfailed it (it calls the module-localaddArrayMutate) and is not marked. Belt's own JavaScript is unchanged, apart from theassertpositions inBelt_Array, which move down by the added@inlinelines.