Repository navigation
Conversation
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>
Changelog🚀 New Feature
Semantic Diff1 commit · Full page Mark small Belt functions
|
✅ Compiler benchmarksAllocated wordsIn total: 655,165,547 → 655,182,497 (±0.00%). Generated projects: 627,288,140 → 627,288,089 (±0.00%).
(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. InstructionsIn total: 9,128,430,195 → 9,128,469,385 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn total: 27,368,788 → 27,372,332 (+0.01%).
(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 Report✅ All modified and coverable lines are covered by tests. 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 🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9505 |
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,isNoneBelt.Result(8):mapWithDefault,map,flatMap,getWithDefault,isOk,isError,eq,cmpBelt.Array.getandset,Belt.List.head,tail,addsizeof the six hash maps and sets;isEmpty,size,peekofMutableQueue;isEmpty,topofMutableStackLeft out:
Int/FloatfromString, andOption.eq/cmp.…OrThrow/…Exnhelper, loops, and functions using module-local helpers.…Ualiases (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
assertline positions inBelt_Array. #9504 removes most of thePrimitive_option.valFromOptioncalls these inlined helpers leave in callers.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq