Semantic Diff

#9505 Mark small Belt functions @inline

1 commit

69e11bfe7acda5984c1bf6bb2e7b10c320cd7df6..pr-9505

dbe89b767 · cknitt + Claude · Dec 01, 2026, 21:25 UTC

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, the Mutable… ones and their Int and String variants): inlined, each call named Belt's internal modules (Belt_internalAVLset, Belt_internalBucketsType), read the data and cmp fields 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. Int and Float fromString added a temporary at every call. Option.eq and cmp came out larger, as Option.equal and compare did in #9484. Every …OrThrow and …Exn helper stays a call, and so do loops and functions that call module-local helpers, such as List.length, Array.every and MutableStack.size.
  • Why are the …U aliases not marked? An alias such as let mapU = map stores 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 @inline caught? Warning 113: Belt builds with warnings as errors. MutableSet.mergeMany failed it (it calls the module-local addArrayMutate) and is not marked. Belt's own JavaScript is unchanged, apart from the assert positions in Belt_Array, which move down by the added @inline lines.