Skip to content

Return unchanged JS IR nodes from the mapper and the JS passes - #9394

Closed
cknitt wants to merge 1 commit into
masterfrom
js-ir-sharing
Closed

cknitt wants to merge 1 commit into
masterfrom
js-ir-sharing

Conversation

@cknitt

@cknitt cknitt commented Nov 23, 2026

Copy link
Copy Markdown
Member

The JS IR mapper Js_record_map rebuilt every node, list and option even when nothing in it changed. Five JS passes run on it, so each pass copied a module's whole JS program although it rewrites little.

Now the mapper returns a node as it is when all its parts come back physically unchanged, like Lambda_traverse.shallow_map_sharing already does for Lambda. The passes' own overrides (statement lists in flatten, tailcall_inline and record_rest, switches and try in external_shadow) and a string switch in lam_pass_remove_alias share the same way. Every mapper function is still called in the same order on the same values.

The JS IR has no mutable fields of its own, and the mutable state reachable from it (identifier usage stats, function environments) was already shared by reference before.

Measured on 77 Belt and tests/tests files: JS output and diagnostics are byte-identical, and compiling allocates 3.2% less.

First of three allocation PRs; the stack together saves 4.6%.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH

The JS IR mapper Js_record_map rebuilt every node, list and option even when
nothing in it changed, and five JS passes run on it, so each pass copied a
module's whole JS program although it rewrites little. The mapper now
returns a node as it is when all its parts come back physically unchanged,
like Lambda_traverse.shallow_map_sharing on the Lambda side; the passes'
own overrides (statement lists in flatten, tailcall_inline and record_rest,
switches and try in external_shadow) and a string switch in remove_alias do
the same. Every function is still called in the same order on the same
values.

The JS IR has no mutable fields of its own, and the mutable state reachable
from it (identifier usage stats, function environments) was already shared
by reference before, so no pass sees a difference. JS output and diagnostics
are byte-identical on 77 benchmark files, and total allocation drops by
3.2%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt added this pull request to stack #9397 November 23, 2026 06:35
@github-actions

Copy link
Copy Markdown

Changelog

🏠 Internal

  • Return unchanged JS IR nodes from the JS passes instead of copying them, so compiling allocates about 3% less.

Semantic Diff

1 commit · Full page

Return unchanged JS IR nodes from the mapper and the JS passes

3d21237 · cknitt + Claude · Nov 23, 2026, 06:25 UTC

The JS IR mapper Js_record_map rebuilt every node, list and option even when nothing in it changed, and five JS passes run on it, so each pass copied a module's whole JS program although it rewrites little. The mapper now returns a node as it is when all its parts come back physically unchanged, like Lambda_traverse.shallow_map_sharing on the Lambda side; the passes' own overrides (statement lists in flatten, tailcall_inline and record_rest, switches and try in external_shadow) and a string switch in remove_alias do the same. Every function is still called in the same order on the same values.

The JS IR has no mutable fields of its own, and the mutable state reachable from it (identifier usage stats, function environments) was already shared by reference before, so no pass sees a difference. JS output and diagnostics are byte-identical on 77 benchmark files, and total allocation drops by 3.2%.

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.80583% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.18%. Comparing base (643e9c0) to head (3d21237).

Files with missing lines Patch % Lines
compiler/backend/js_record_map.ml 90.22% 17 Missing ⚠️
compiler/backend/js_pass_external_shadow.ml 82.35% 3 Missing ⚠️
compiler/backend/js_pass_tailcall_inline.ml 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9394      +/-   ##
==========================================
- Coverage   82.21%   82.18%   -0.03%     
==========================================
  Files         460      460              
  Lines       65551    65577      +26     
==========================================
+ Hits        53891    53896       +5     
- Misses      11660    11681      +21     
Files with missing lines Coverage Δ
compiler/backend/js_pass_flatten.ml 76.74% <100.00%> (ø)
compiler/backend/js_pass_record_rest.ml 93.87% <100.00%> (+0.39%) ⬆️
compiler/backend/lam_pass_remove_alias.ml 90.09% <100.00%> (ø)
compiler/backend/js_pass_tailcall_inline.ml 85.71% <88.88%> (-0.54%) ⬇️
compiler/backend/js_pass_external_shadow.ml 93.22% <82.35%> (-5.00%) ⬇️
compiler/backend/js_record_map.ml 90.76% <90.22%> (-9.24%) ⬇️
🚀 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.

@github-actions

Copy link
Copy Markdown

🎉 Compiler benchmarks

Allocated words

In total: 636,516,809 → 624,737,176 (-1.85%).

Generated projects: 629,041,809 → 626,381,835 (-0.42%).

🎉 Improvement: 74 benchmarks allocate more than 1% less.

Benchmark Base Head Change
stress/InlineChain.res 3,511,206 3,249,991 -7.44%
stress/ListLiteral.res 3,400,583 3,205,473 -5.74%
stress/LetRec.res 31,619,781 30,319,500 -4.11%
packages/@rescript/belt/src/Belt_SortArrayInt.res 1,342,429 1,288,238 -4.04%
packages/@rescript/belt/src/Belt_SortArrayString.res 1,344,225 1,290,034 -4.03%
packages/@rescript/belt/src/Belt_SortArray.res 1,447,582 1,389,544 -4.01%
packages/@rescript/runtime/Stdlib_List.res 2,686,422 2,583,889 -3.82%
packages/@rescript/belt/src/Belt_List.res 3,175,359 3,059,356 -3.65%
packages/@rescript/belt/src/Belt_Array.res 2,154,235 2,079,003 -3.49%
build/leaf-edit 13,027,802 12,585,963 -3.39%
109 more changes
Benchmark Base Head Change
packages/@rescript/belt/src/Belt_internalAVLset.res 2,876,034 2,781,376 -3.29%
packages/@rescript/belt/src/Belt_internalAVLtree.res 3,506,403 3,392,692 -3.24%
packages/@rescript/runtime/Primitive_object.res 1,154,866 1,118,626 -3.14%
build/no-op 9,213,898 8,929,500 -3.09%
packages/@rescript/belt/src/Belt_MutableQueue.res 791,334 767,304 -3.04%
build/shared-edit 72,591,528 70,415,790 -3.00%
packages/@rescript/belt/src/Belt_MutableSetInt.res 1,664,555 1,617,782 -2.81%
packages/@rescript/belt/src/Belt_MutableSetString.res 1,665,801 1,619,028 -2.81%
packages/@rescript/runtime/Primitive_module.res 337,540 328,211 -2.76%
packages/@rescript/belt/src/Belt_MutableSet.res 1,902,475 1,849,986 -2.76%
packages/@rescript/belt/src/Belt_internalBuckets.res 966,624 941,256 -2.62%
packages/@rescript/belt/src/Belt_internalMapInt.res 1,408,499 1,372,468 -2.56%
packages/@rescript/belt/src/Belt_internalMapString.res 1,410,343 1,374,312 -2.55%
packages/@rescript/runtime/Stdlib_Option.res 753,633 734,852 -2.49%
packages/@rescript/belt/src/Belt_HashMapInt.res 1,060,707 1,034,742 -2.45%
packages/@rescript/belt/src/Belt_HashMapString.res 1,061,419 1,035,454 -2.45%
packages/@rescript/belt/src/Belt_MutableMap.res 1,302,290 1,270,625 -2.43%
packages/@rescript/belt/src/Belt_internalSetInt.res 730,723 713,019 -2.42%
packages/@rescript/belt/src/Belt_HashSetInt.res 850,520 829,941 -2.42%
packages/@rescript/belt/src/Belt_internalSetString.res 731,914 714,210 -2.42%
packages/@rescript/belt/src/Belt_HashSetString.res 850,837 830,258 -2.42%
packages/@rescript/runtime/Primitive_hash.res 964,614 941,376 -2.41%
packages/@rescript/belt/src/Belt_SetInt.res 1,289,624 1,259,171 -2.36%
packages/@rescript/belt/src/Belt_SetString.res 1,290,353 1,259,900 -2.36%
packages/@rescript/belt/src/Belt_HashMap.res 1,200,049 1,171,954 -2.34%
packages/@rescript/belt/src/Belt_MutableMapInt.res 1,154,742 1,127,820 -2.33%
packages/@rescript/belt/src/Belt_MutableMapString.res 1,155,019 1,128,097 -2.33%
packages/@rescript/belt/src/Belt_internalSetBuckets.res 699,079 682,861 -2.32%
packages/@rescript/belt/src/Belt_MapDict.res 1,630,823 1,593,012 -2.32%
packages/@rescript/belt/src/Belt_Set.res 857,946 838,154 -2.31%
packages/@rescript/belt/src/Belt_HashSet.res 978,208 955,800 -2.29%
packages/@rescript/belt/src/Belt_SetDict.res 1,445,985 1,413,193 -2.27%
packages/@rescript/belt/src/Belt_Map.res 981,236 959,136 -2.25%
packages/@rescript/runtime/Stdlib_Array.res 1,281,101 1,252,849 -2.21%
packages/@rescript/belt/src/Belt_MutableStack.res 387,514 379,019 -2.19%
packages/@rescript/belt/src/Belt_Range.res 288,346 282,044 -2.19%
packages/@rescript/runtime/Primitive_list.res 203,745 199,295 -2.18%
packages/@rescript/belt/src/Belt_MapInt.res 1,181,363 1,157,536 -2.02%
packages/@rescript/belt/src/Belt_MapString.res 1,181,899 1,158,072 -2.02%
stress/NestedClosures.res 10,273,646 10,073,681 -1.95%
packages/@rescript/belt/src/Belt_Option.res 389,121 381,610 -1.93%
packages/@rescript/runtime/Stdlib_Result.res 1,268,255 1,244,123 -1.90%
build/clean 236,615,174 232,125,394 -1.90%
build/Namespace/edit 7,875,796 7,735,413 -1.78%
packages/@rescript/runtime/Primitive_array.res 191,984 188,579 -1.77%
packages/@rescript/runtime/Stdlib_Int.res 455,336 447,755 -1.66%
packages/@rescript/runtime/Primitive_option.res 297,411 292,684 -1.59%
packages/@rescript/belt/src/Belt_Result.res 409,437 403,540 -1.44%
packages/@rescript/runtime/Primitive_int.res 171,842 169,401 -1.42%
packages/@rescript/runtime/Stdlib_Null.res 332,359 327,687 -1.41%
packages/@rescript/runtime/Stdlib_Nullable.res 329,857 325,361 -1.36%
packages/@rescript/runtime/Stdlib_JsError.res 229,751 226,656 -1.35%
packages/@rescript/runtime/Primitive_bigint.res 177,314 174,933 -1.34%
packages/@rescript/runtime/Primitive_string.res 159,592 157,486 -1.32%
packages/@rescript/runtime/Stdlib_Lazy.res 221,184 218,326 -1.29%
packages/@rescript/runtime/Primitive_exceptions.res 201,232 198,712 -1.25%
packages/@rescript/runtime/Primitive_float.res 146,893 145,080 -1.23%
stress/RefsInLoop.res 16,462,408 16,263,243 -1.21%
packages/@rescript/belt/src/Belt_internalBucketsType.res 291,615 288,380 -1.11%
packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res 159,421 157,701 -1.08%
packages/@rescript/runtime/Stdlib_Pair.res 165,262 163,504 -1.06%
packages/@rescript/runtime/Stdlib_JSON.res 296,903 293,836 -1.03%
packages/@rescript/runtime/Dom_storage.res 167,042 165,322 -1.03%
packages/@rescript/runtime/Stdlib_Ordering.res 152,340 150,800 -1.01%
packages/@rescript/runtime/Stdlib_AsyncIterator.res 294,756 291,832 -0.99%
packages/@rescript/runtime/Stdlib_Type.res 201,438 199,508 -0.96%
packages/@rescript/runtime/Stdlib_Bool.res 146,012 144,639 -0.94%
packages/@rescript/runtime/Stdlib_JsExn.res 173,080 171,547 -0.89%
packages/@rescript/runtime/Stdlib_AsyncIterableIterator.res 228,325 226,477 -0.81%
packages/@rescript/runtime/Primitive_bool.res 147,218 146,035 -0.80%
packages/@rescript/runtime/Stdlib_Iterator.res 234,922 233,092 -0.78%
packages/@rescript/runtime/Stdlib_Generator.res 200,286 198,756 -0.76%
packages/@rescript/runtime/Stdlib_AsyncGenerator.res 211,156 209,566 -0.75%
packages/@rescript/runtime/Stdlib_String.res 503,934 500,391 -0.70%
stress/ObjectFields.res 20,649,523 20,517,428 -0.64%
packages/@rescript/belt/src/Belt.res 187,948 186,748 -0.64%
packages/@rescript/runtime/Stdlib_BigInt.res 207,074 205,859 -0.59%
packages/@rescript/runtime/Stdlib_Temporal_PlainMonthDay.res 268,246 266,731 -0.56%
stress/Record.res 26,115,923 25,968,818 -0.56%
stress/IntSwitch.res 29,766,198 29,600,928 -0.56%
packages/@rescript/runtime/Stdlib_Math.res 327,556 325,744 -0.55%
packages/@rescript/runtime/Stdlib_Float.res 218,863 217,693 -0.53%
packages/@rescript/runtime/Stdlib.res 980,476 975,286 -0.53%
packages/@rescript/belt/src/Belt_Id.res 289,887 288,447 -0.50%
build/Namespace/clean 84,166,842 83,752,037 -0.49%
packages/@rescript/runtime/Stdlib_Temporal_Instant.res 307,599 306,084 -0.49%
packages/@rescript/runtime/Stdlib_Temporal_PlainTime.res 320,257 318,742 -0.47%
stress/PolyVariantSwitch.res 14,274,344 14,207,950 -0.47%
build/WideDeps/clean 371,076,502 369,419,137 -0.45%
packages/@rescript/runtime/Stdlib_IterableIterator.res 178,852 178,072 -0.44%
packages/@rescript/runtime/Stdlib_Intl.res 173,862 173,152 -0.41%
packages/@rescript/runtime/Stdlib_Temporal_Duration.res 373,600 372,085 -0.41%
packages/@rescript/runtime/Stdlib_Temporal_PlainYearMonth.res 382,285 380,770 -0.40%
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res 403,907 402,392 -0.38%
packages/@rescript/belt/src/Belt_Int.res 165,790 165,179 -0.37%
packages/@rescript/belt/src/Belt_Float.res 165,048 164,472 -0.35%
packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res 438,194 436,679 -0.35%
packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res 574,181 572,341 -0.32%
packages/@rescript/runtime/Stdlib_Temporal.res 223,963 223,253 -0.32%
packages/@rescript/runtime/Stdlib_Dict.res 320,111 319,171 -0.29%
packages/@rescript/runtime/Stdlib_IteratorObject.res 250,722 250,042 -0.27%
build/WideDeps/edit 165,922,669 165,475,248 -0.27%
packages/@rescript/runtime/Stdlib_Date.res 551,778 550,783 -0.18%
packages/@rescript/runtime/Stdlib_Promise.res 352,778 352,188 -0.17%
packages/@rescript/runtime/JsxEvent.res 1,090,874 1,089,069 -0.17%
packages/@rescript/runtime/Stdlib_Map.res 205,648 205,333 -0.15%
packages/@rescript/runtime/Stdlib_Set.res 210,990 210,675 -0.15%
packages/@rescript/runtime/RescriptTools.res 104,517 104,367 -0.14%
packages/@rescript/runtime/Stdlib_Global.res 141,743 141,543 -0.14%

Instructions

In total: 9,221,447,548 → 9,172,468,895 (-0.53%).

🎉 Improvement: 2 benchmarks execute more than 1% fewer instructions.

Benchmark Base Head Change
stress/InlineChain.res 138,211,031 131,031,341 -5.19%
stress/LetRec.res 1,377,895,712 1,337,823,932 -2.91%
stress/ListLiteral.res 131,161,599 130,871,293 -0.22%
packages/@rescript/belt/src/Belt_List.res 118,135,924 118,010,975 -0.11%

Output size

In total: 25,962,507 → 25,963,868 (+0.01%).

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

Pull request 3d21237 compared with base 643e9c0 in this run.

@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 3d21237

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-23T07:31:44.038380Z 3d21237 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: 3d212378cf

ℹ️ 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".

@cknitt
cknitt marked this pull request as ready for review November 23, 2026 08:50
@cknitt

cknitt commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Closing: the saved allocations don't make builds faster. 20 alternating clean builds of tests/tests (690 modules) with and without this stack show the same wall and CPU time (±0%, −0.5% CPU) and about 1% less peak memory, and a real-world project shows no difference either. The avoided copies died young, so they cost the GC little. That doesn't pay for the extra mapper code.

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