Skip to content

Mark small stdlib functions @inline - #9484

Merged
cknitt merged 1 commit into
inline-const-diagnosticsfrom
stdlib-inline
Nov 30, 2026
Merged

cknitt merged 1 commit into
inline-const-diagnosticsfrom
stdlib-inline

Conversation

@cknitt

@cknitt cknitt commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Marks 72 small stdlib functions @inline, so calls to them compile to the code they run. They were chosen from the exported functions whose bodies other modules can store:

  • argument-reordering wrappers around externals: Array.reduce and its variants, Array.concatAll, Promise.catch, the iterator and generator next functions, Dom_storage, Dict.delete, BigInt.toInt, Bool.toString;
  • combinators that take a callback: Null, Nullable and Result map, flatMap, mapOr, getOr, forEach and the like, and Result, Pair and Date equal and compare;
  • the …Opt lookups of Array and String, Array.last, isEmpty of Array, String, Map and Set, List.add, head and tail, and the Ordering helpers.

For example, [1, 2, 3]->Array.reduce(list{}, List.add) now compiles to [1, 2, 3].reduce(Stdlib_List.add, /* [] */0). A Promise.catch handler is passed straight to promise.catch.

Left out:

  • loops (zip, partition, filterMap, Option.all*), parsers wrapped in try, async functions, and larger functions such as clamp;
  • functions whose inlined code came out worse in the tests:
    • every …OrThrow (a throw block at each call, without a source position);
    • Lazy.make and isEvaluated (the runtime's lazy representation in every caller);
    • JsError.throwWithMessage (its throw site lost its source position);
    • Option, Null and Nullable equal and compare, and JsExn.fromException (larger, with nothing simplified);
    • Iterator.next and nextValue, which use a module-local helper.

Belt is left for its own pass.

Int.Constants.minValue and maxValue become @inline constants in Stdlib_Int.resi. JavaScript that reads them from the runtime's module gets undefined, so that has a breaking changelog entry.

A wrong @inline fails the build: the runtime builds with warnings as errors, and warning 113 (#9482) reports functions other modules can't inline. The runtime's and Belt's own JavaScript is unchanged, apart from Stdlib_Int. In tests/tests, 20 generated files change, by about as many lines added as removed.

This takes over the stdlib part of #8689. Part of stack #9486, at the top.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@cknitt
cknitt changed the base branch from master to inline-const-diagnostics November 29, 2026 09:27
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Changelog

💥 Breaking Change

  • Int.Constants.minValue and maxValue are now @inline constants, so ReScript code using them gets the number itself. JavaScript code that reads them from the runtime's Stdlib_Int module gets undefined.

🚀 New Feature

  • Mark small stdlib functions @inline, so calls to them compile to the code they run: argument-reordering wrappers such as Array.reduce (which now compiles to array.reduce(f, init)), Promise.catch and the iterator and generator next functions; the Null, Nullable and Result combinators such as map and getOr; the …Opt lookups of Array and String; and predicates such as isEmpty.

Semantic Diff

1 commit · Full page

Mark small stdlib functions @inline

0bb9ef6 · cknitt + Claude · Nov 29, 2026, 08:37 UTC

Why

Since #9479, other modules inline @inline functions, but apart from the nine Option helpers, the stdlib marked none, so every call to a one-line wrapper such as Array.reduce stayed a call through the wrapper. 72 functions are now @inline, chosen from the exported functions whose bodies other modules can store: - argument-reordering wrappers around externals: Array.reduce and its variants, Array.concatAll, Promise.catch, the iterator and generator next functions, Dom_storage, Dict.delete, BigInt.toInt, Bool.toString; - combinators that take a callback: Null, Nullable and Result map, flatMap, mapOr, getOr, forEach and the like, Result, Pair and Date equal and compare; - the …Opt lookups of Array and String, Array.last, isEmpty of Array, String, Map and Set, List.add, head, tail, and the Ordering helpers. Int.Constants.minValue and maxValue become @inline constants in Stdlib_Int.resi.

JS modified (a caller of the stdlib)

let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
let last = [1, 2, 3]->Array.last

js

-let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);
+let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);
 
-let last = Stdlib_Array.last([1, 2, 3]);
+let a = [1, 2, 3];
+
+let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler)

js

-Stdlib_Promise.$$catch(promise, e => { … });
+promise.catch(err => {
+  let e = Primitive_exceptions.internalToException(err);
+  …
+});

JS modified (the Int constants)

// Stdlib_Int.resi, in module Constants
@inline(-2147483648)
let minValue: int

js · Before: Stdlib_Int.mjs · After: Stdlib_Int.mjs; other modules inline the constants

-let Constants = {
-  minValue: -2147483648,
-  maxValue: 2147483647
-};
+let Constants = {};

⚠️ Breaking: JavaScript that reads Int.Constants.minValue or maxValue from the runtime's Stdlib_Int module gets undefined; ReScript code inlines them.

Review

  • What was left out? Loops (zip, partition, filterMap, Option.all*), parsers wrapped in try, async functions, and larger functions such as clamp. Also functions whose inlined code came out worse in the tests: every …OrThrow (a throw block at each call, without a source position), Lazy.make and isEvaluated (the runtime's lazy representation in every caller), JsError.throwWithMessage (its throw site lost its source position), Option.equal and compare, Null and Nullable equal and compare, and JsExn.fromException (larger, with no simplification), and Iterator.next and nextValue, which use a module-local helper. Belt is left for its own pass. JsError.panic stays @inline (Remove -bs-cross-module-opt #9480), since without it the runtime's own JavaScript changes.
  • How is a wrong @inline caught? Warning 113: the runtime builds with warnings as errors, so a function other modules cannot inline fails the build. The runtime's and Belt's own JavaScript is unchanged.

@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T09:32:43.122587Z 744d3c4 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 744d3c4f4e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 674,782,686 → 674,904,325 (+0.02%).

Generated projects: 630,567,513 → 630,567,667 (±0.00%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Ordering.res (inputs changed) 153,385 155,305 +1.25%
packages/@rescript/runtime/Dom_storage.res (inputs changed) 166,998 168,380 +0.83%
packages/@rescript/runtime/Stdlib_Generator.res (inputs changed) 200,017 201,514 +0.75%
packages/@rescript/runtime/Stdlib_Nullable.res (inputs changed) 330,502 332,940 +0.74%
packages/@rescript/runtime/Stdlib_Null.res (inputs changed) 333,003 335,436 +0.73%
packages/@rescript/runtime/Stdlib_Pair.res (inputs changed) 165,531 166,565 +0.62%
packages/@rescript/runtime/Stdlib_Int.resi (inputs changed) 204,566 205,591 +0.50%
packages/@rescript/runtime/Stdlib_IterableIterator.res (inputs changed) 178,912 179,669 +0.42%
packages/@rescript/runtime/Stdlib_String.res (inputs changed) 504,036 506,012 +0.39%
packages/@rescript/runtime/Stdlib_Result.res (inputs changed) 1,268,910 1,273,829 +0.39%
12 more changes
Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_AsyncGenerator.res (inputs changed) 211,041 211,800 +0.36%
packages/@rescript/runtime/Stdlib_AsyncIterableIterator.res (inputs changed) 228,391 229,132 +0.32%
packages/@rescript/runtime/Stdlib_Array.res (inputs changed) 1,280,687 1,284,814 +0.32%
packages/@rescript/runtime/Stdlib_IteratorObject.res (inputs changed) 250,705 251,466 +0.30%
packages/@rescript/runtime/Stdlib_Bool.res (inputs changed) 146,296 146,673 +0.26%
packages/@rescript/runtime/Stdlib_Int.res (inputs changed) 454,823 455,641 +0.18%
packages/@rescript/runtime/Stdlib_BigInt.res (inputs changed) 207,268 207,630 +0.17%
packages/@rescript/runtime/Stdlib_Map.res (inputs changed) 205,875 206,214 +0.16%
packages/@rescript/runtime/Stdlib_Set.res (inputs changed) 211,221 211,558 +0.16%
packages/@rescript/runtime/Stdlib_Date.res (inputs changed) 552,107 552,905 +0.14%
packages/@rescript/runtime/Stdlib_Promise.res (inputs changed) 353,031 353,453 +0.12%
packages/@rescript/runtime/Stdlib_Dict.res (inputs changed) 320,879 321,235 +0.11%

(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,225,173,451 → 9,225,173,451 (±0.00%).

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

Output size

In total: 27,359,384 → 27,368,415 (+0.03%).

Benchmark Base Head Change
output/runtime/*.cmj (inputs changed) 14,126 20,606 +45.87%

(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 0bb9ef6 compared with base f607fbe in this run.

@pkg-pr-new

pkg-pr-new Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 0bb9ef6

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.47%. Comparing base (2bb598f) to head (0bb9ef6).

Additional details and impacted files
@@                     Coverage Diff                      @@
##           inline-const-diagnostics    #9484      +/-   ##
============================================================
+ Coverage                     82.46%   82.47%   +0.01%     
============================================================
  Files                           466      466              
  Lines                         66070    66070              
============================================================
+ Hits                          54486    54493       +7     
+ Misses                        11584    11577       -7     

see 3 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.

@cknitt
cknitt force-pushed the stdlib-inline branch 3 times, most recently from 885f727 to 2d43271 Compare November 29, 2026 11:17
@cknitt
cknitt force-pushed the stdlib-inline branch 2 times, most recently from 308c984 to a768261 Compare November 29, 2026 11:56
@cknitt
cknitt marked this pull request as ready for review November 29, 2026 12:08
@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:13
Why:
    Since #9479, other modules inline `@inline` functions, but apart from
    the nine Option helpers, the stdlib marked none, so every call to a
    one-line wrapper such as `Array.reduce` stayed a call through the
    wrapper. 72 functions are now `@inline`, chosen from the exported
    functions whose bodies other modules can store:
    - argument-reordering wrappers around externals: `Array.reduce` and its
      variants, `Array.concatAll`, `Promise.catch`, the iterator and
      generator `next` functions, `Dom_storage`, `Dict.delete`,
      `BigInt.toInt`, `Bool.toString`;
    - combinators that take a callback: `Null`, `Nullable` and `Result`
      `map`, `flatMap`, `mapOr`, `getOr`, `forEach` and the like, `Result`,
      `Pair` and `Date` `equal` and `compare`;
    - the `…Opt` lookups of `Array` and `String`, `Array.last`, `isEmpty`
      of `Array`, `String`, `Map` and `Set`, `List.add`, `head`, `tail`,
      and the `Ordering` helpers.
    `Int.Constants.minValue` and `maxValue` become `@inline` constants in
    `Stdlib_Int.resi`.

JS modified (a caller of the stdlib):
Example:
    let reversed = [1, 2, 3]->Array.reduce(list{}, List.add)
    let last = [1, 2, 3]->Array.last
Before (js):
    let reversed = Stdlib_Array.reduce([1, 2, 3], /* [] */0, Stdlib_List.add);

    let last = Stdlib_Array.last([1, 2, 3]);
After (js):
    let reversed = [1, 2, 3].reduce(Stdlib_List.add, /* [] */0);

    let a = [1, 2, 3];

    let last = a[a.length - 1 | 0];

JS modified (a Promise.catch handler):
Before (js):
    Stdlib_Promise.$$catch(promise, e => { … });
After (js):
    promise.catch(err => {
      let e = Primitive_exceptions.internalToException(err);
      …
    });

Review: What was left out? Loops (`zip`, `partition`, `filterMap`,
`Option.all*`), parsers wrapped in `try`, async functions, and larger
functions such as `clamp`. Also functions whose inlined code came out
worse in the tests: every `…OrThrow` (a `throw` block at each call, without
a source position), `Lazy.make` and `isEvaluated` (the runtime's lazy
representation in every caller), `JsError.throwWithMessage` (its throw
site lost its source position), `Option.equal` and `compare`, `Null` and
`Nullable` `equal` and `compare`, and `JsExn.fromException` (larger, with
no simplification), and `Iterator.next` and `nextValue`, which use a
module-local helper. Belt is left for its own pass. `JsError.panic` stays
`@inline` (#9480), since without it the runtime's own JavaScript changes.

JS modified (the Int constants):
Example:
    // Stdlib_Int.resi, in module Constants
    @inline(-2147483648)
    let minValue: int
Before (js): Stdlib_Int.mjs
    let Constants = {
      minValue: -2147483648,
      maxValue: 2147483647
    };
After (js): Stdlib_Int.mjs; other modules inline the constants
    let Constants = {};

Review: How is a wrong `@inline` caught? Warning 113: the runtime builds
with warnings as errors, so a function other modules cannot inline fails
the build. The runtime's and Belt's own JavaScript is unchanged.

Breaking: JavaScript that reads `Int.Constants.minValue` or `maxValue`
from the runtime's `Stdlib_Int` module gets `undefined`; ReScript code
inlines them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt merged commit 55344bd into master Nov 30, 2026
42 of 57 checks passed
@cknitt
cknitt deleted the stdlib-inline branch November 30, 2026 07:53
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.

2 participants