Skip to content

PoC: replace @as(json) arguments in the stdlib with @inline functions - #9518

Draft
cknitt wants to merge 1 commit into
inline-args-read-oncefrom
stdlib-no-as-json
Draft

cknitt wants to merge 1 commit into
inline-args-read-oncefrom
stdlib-no-as-json

Conversation

@cknitt

@cknitt cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member

A proof of concept: @as(json) on external arguments isn't needed. An @inline function can pass the constant instead, the way Array.reduce already reorders its arguments:

@val external copy: (dict<'a>, dict<'a>) => dict<'a> = "Object.assign"

@inline
let copy = dictionary => copy(make(), dictionary)

This replaces all seven uses in the stdlib:

  • Dict.concat, Dict.concatMany, Dict.concatAll, Dict.copy (they pass {});
  • Object.copy (passes {});
  • Object.createWithNull, Object.createWithNullAndProperties (they pass null).

Calls compile to the same JavaScript as before; Stdlib_DictTests.mjs doesn't change:

let merged = Object.assign({}, target, {
  b: 3
});

let bare = Object.create(null);

The only difference in output: Stdlib_Dict.js and Stdlib_Object.js now export these functions, as Stdlib_Array.js exports reduce. A use as a value, such as dicts->Array.map(Dict.copy), refers to the exported function instead of a wrapper built at that spot.

Without the previous PR, literal arguments would get a variable of their own (let dict2 = {b: 3}; Object.assign({}, target, dict2)), and a literal array passed to the variadic concatMany would no longer be expanded.

Not included: a deprecation warning for @as(json) on external arguments. That would follow if we go this way.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

@cknitt
cknitt changed the base branch from master to inline-args-read-once December 3, 2026 19:26
@cknitt
cknitt added this pull request to stack #9519 December 3, 2026 19:26
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Changelog

🏠 Internal

  • Pass the constant arguments of Dict.concat, Dict.concatMany, Dict.concatAll, Dict.copy, Object.copy, Object.createWithNull and Object.createWithNullAndProperties from @inline functions instead of @as(json).

Semantic Diff

1 commit · Full page

Pass the constant arguments of Dict and Object functions without @as(json)

4d2973c · cknitt + Claude · Dec 03, 2026, 19:19 UTC

Why

A prototype for deprecating @as(json) on external arguments. The seven stdlib uses become an external that takes the constant plus an @inline function of the same name that passes {} or null, the way Array.reduce reorders its arguments. Calls compile to the same JavaScript as before.

JS unchanged (call sites):

let merged = target->Dict.concat(dict{"b": 3})
let bare = Object.createWithNull()

js · Before and After are equal

let merged = Object.assign({}, target, {
  b: 3
});

let bare = Object.create(null);

Review

  • What changes for users? Stdlib_Dict.js and Stdlib_Object.js now export these functions, as Stdlib_Array.js exports reduce. A function value such as Dict.copy passed around calls the exported function instead of a wrapper made at the use site.

…json)

Why:
    A prototype for deprecating `@as(json)` on external arguments. The seven
    stdlib uses become an external that takes the constant plus an `@inline`
    function of the same name that passes `{}` or `null`, the way
    `Array.reduce` reorders its arguments. Calls compile to the same
    JavaScript as before.

JS unchanged (call sites):
Example:
    let merged = target->Dict.concat(dict{"b": 3})
    let bare = Object.createWithNull()
Before (js):
    let merged = Object.assign({}, target, {
      b: 3
    });

    let bare = Object.create(null);
After (js):
    let merged = Object.assign({}, target, {
      b: 3
    });

    let bare = Object.create(null);

Review: What changes for users? `Stdlib_Dict.js` and `Stdlib_Object.js`
now export these functions, as `Stdlib_Array.js` exports `reduce`. A
function value such as `Dict.copy` passed around calls the exported
function instead of a wrapper made at the use site.

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>
@cknitt
cknitt force-pushed the stdlib-no-as-json branch from 9bba089 to 4d2973c Compare December 3, 2026 19:32
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 653,169,466 → 653,223,087 (+0.01%).

Generated projects: 627,295,414 → 627,295,264 (±0.00%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Object.res (inputs changed) 247,174 278,531 +12.69%
packages/@rescript/runtime/Stdlib_Dict.res (inputs changed) 318,111 347,770 +9.32%
packages/@rescript/runtime/Stdlib_Dict.resi (inputs changed) 239,124 228,925 -4.27%
packages/@rescript/runtime/Stdlib.res (inputs changed) 984,383 983,362 -0.10%

(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: 8,966,803,168 → 8,966,803,168 (±0.00%).

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

Output size

In total: 27,449,924 → 27,449,957 (±0.00%).

Benchmark Base Head Change
output/runtime/*.cmj (inputs changed) 20,606 21,342 +3.57%
output/runtime/*.cmt (inputs changed) 3,041,525 3,046,685 +0.17%
output/runtime/*.cmi (inputs changed) 1,921,294 1,918,622 -0.14%

(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 4d2973c compared with base ef50d81 in this run.

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 4d2973c

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