Skip to content

Mark small Belt functions @inline - #9505

Draft
cknitt wants to merge 1 commit into
masterfrom
belt-inline
Draft

cknitt wants to merge 1 commit into
masterfrom
belt-inline

Conversation

@cknitt

@cknitt cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member

Follow-up from #8733: the Belt version of #9484. 33 small exported Belt functions are now @inline, so other modules compile calls to them as the code they run instead of a call through the wrapper.

  • 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, add
  • accessors (11): size of the six hash maps and sets; isEmpty, size, peek of MutableQueue; isEmpty, top of MutableStack
// Belt.HashMap.Int.size(h), before → after
Belt_HashMapInt.size(h)  →  h.size

Left out:

  • The map and set wrappers: inlined, they name Belt's internal modules and make callers longer.
  • Constructors, Int/Float fromString, and Option.eq/cmp.
  • Every …OrThrow/…Exn helper, loops, and functions using module-local helpers.
  • The deprecated …U aliases (an alias stores no body).

The commit message has the details. Warning 113 (Belt builds with warnings as errors) catches a wrong mark.

Belt's own JS is unchanged except the assert line positions in Belt_Array. #9504 removes most of the Primitive_option.valFromOption calls these inlined helpers leave in callers.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

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`.

JS modified (a caller of Belt):
Example:
    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)
Before (js):
    function computeArea(param) {
      return (param[0] * param[1] | 0) * Belt_Option.getWithDefault(param[2], 1) | 0;
    }

    let size = Belt_MutableQueue.size;

    let hashSize = Belt_HashMapInt.size;
After (js):
    function computeArea(param) {
      let z = param[2];
      return (param[0] * param[1] | 0) * (
        z !== undefined ? Primitive_option.valFromOption(z) : 1
      ) | 0;
    }

    function size(q) {
      return q.length;
    }

    function hashSize(h) {
      return h.size;
    }

JS modified (a Belt.Result caller):
Example:
    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)
Before (js):
    let isOk = Belt_Result.isOk;

    function rcmp(a, b) {
      return Belt_Result.cmp(a, b, (x, y) => x - y | 0);
    }
After (js):
    function isOk(a) {
      return a.TAG === "Ok";
    }

    function rcmp(a, b) {
      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`.

Review: 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.

Review: 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.

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>
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Changelog

🚀 New Feature

  • Mark small Belt functions @inline, so calls to them compile to the code they run: the Belt.Option and Belt.Result combinators such as map and getWithDefault, Belt.Result.eq and cmp, Belt.Array.get and set, Belt.List.head, tail and add, and the size, isEmpty, peek and top accessors of the hash maps and sets, MutableQueue and MutableStack.

Semantic Diff

1 commit · Full page

Mark small Belt functions @inline

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

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.

JS modified (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

 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;
+}

JS modified (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

-let isOk = Belt_Result.isOk;
+function isOk(a) {
+  return a.TAG === "Ok";
+}
 
 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 Mark small stdlib functions @inline #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.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 655,165,547 → 655,182,497 (±0.00%).

Generated projects: 627,288,140 → 627,288,089 (±0.00%).

Benchmark Base Head Change
packages/@rescript/belt/src/Belt_Option.res (inputs changed) 380,145 384,058 +1.03%
packages/@rescript/belt/src/Belt_Result.res (inputs changed) 402,881 406,894 +1.00%
packages/@rescript/belt/src/Belt_MutableStack.res (inputs changed) 378,511 379,442 +0.25%
packages/@rescript/belt/src/Belt_MutableQueue.res (inputs changed) 764,303 765,641 +0.18%

(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions.

Instructions

In total: 9,128,430,195 → 9,128,469,385 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 27,368,788 → 27,372,332 (+0.01%).

Benchmark Base Head Change
output/belt/*.cmj (inputs changed) 22,376 24,816 +10.90%

(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions.

Pull request dbe89b7 compared with base 69e11bf in this run.

@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.50%. Comparing base (69e11bf) to head (dbe89b7).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9505   +/-   ##
=======================================
  Coverage   82.49%   82.50%           
=======================================
  Files         467      467           
  Lines       66125    66125           
=======================================
+ Hits        54553    54555    +2     
+ Misses      11572    11570    -2     

see 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9505

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9505

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9505

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9505

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9505

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9505

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9505

commit: dbe89b7

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant