Skip to content

Skip copying every props label per JSX element - #9187

Merged
JonoPrest merged 2 commits into
masterfrom
jono/jsx-props-skip-label-copy
Nov 4, 2026
Merged

JonoPrest merged 2 commits into
masterfrom
jono/jsx-props-skip-label-copy

Conversation

@JonoPrest

Copy link
Copy Markdown
Contributor

JsxDOM.domProps has 591 optional fields, and every lowercase JSX element paid for all of them:

  • jsx_props_of copied every label into a new array just to check whether the record compiles to an array.
  • Typing the record literal built a 591 entry array and list to look for missing required labels.

The label array is now checked in place, and the missing label search is skipped when every label is optional. Records with a required label take the old path. Output is unchanged.

The copy mattered more than its own cost. In OCaml 5, Array.map over more than 256 elements forces a minor collection, which stops every domain. On a 552 module React app that happened about 2,800 times per clean build (87% of all minor collections), and it was most of the extra CPU a build spends on 7 domains compared to 1.

Clean build of that app, 8 vCPU Linux VM, median of 5 interleaved runs:

domains master this PR
7 (default) 5.08 s 4.08 s
1 16.27 s 15.85 s

Projects without JSX don't change. 300 <div>s in one module now translate as fast as 300 plain function calls (23 ms to 7 ms).

Testing:

  • New large_optional_record_test.res (large records with optional, @as and mutable fields, spreads, all-optional records, host JSX with spreads and nesting) and a super_errors fixture for missing labels on a partly optional record. Both match master's output.
  • JS, .cmi, .cmj, .ast and .cmt are byte identical to master across three production codebases and all their dependencies.
  • make test and make test-rewatch pass.

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Build: compile JSX elements without copying every label of their props record, which removed most garbage collection pauses in builds of JSX-heavy projects on several domains.

Semantic Diff

2 commits · Full page

Don't copy every label of a large record per JSX element

3b95c9e · JonoPrest · Nov 03, 2026, 07:59 UTC

Why

JsxDOM.domProps has 591 optional fields. For every lowercase JSX element, jsx_props_of mapped all of them into a fresh array just to check whether the record compiles to an array, and typing a record literal built a 591 entry array and list to look for missing mandatory labels.

In OCaml 5, Array.map on more than 256 elements forces a minor collection, which stops every domain. On a 552 module React app this happened about 2,800 times per build, 87% of all minor collections, and was most of the extra CPU time a build spends on 7 domains compared to 1.

The label array is now checked in place, and the missing label search is skipped when every label is optional. Records with a required label, like most component props, take the old path. The output is unchanged.

Review

  • Why does a 1 domain build barely change? Without other domains waiting at each forced collection, the copy costs about 55 us per element. The gain comes from the collections no longer stopping the other domains.
Don't copy every label of a large record literal either

5578f1e · JonoPrest · Nov 03, 2026, 14:37 UTC

Why

Translating a record literal still built an array of (label, written value) pairs over every label, and a second array of (runtime name, optional) pairs for its shape. For a record with more than 256 labels, such as a JsxDOM.domProps literal, each of these forces a minor collection in OCaml 5, which stops every domain.

fields_by_position now returns a list built in position order, and the shape array is filled in place. Field expressions are still translated left to right, so the output is unchanged.

JonoPrest added a commit that referenced this pull request Nov 3, 2026
The entry names #9187, so the changelog points at this PR.
@JonoPrest
JonoPrest added this pull request to stack #9189 November 3, 2026 07:32
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T14:38:48.383637Z b78ba42 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.

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.73%. Comparing base (6741bab) to head (5578f1e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9187   +/-   ##
=======================================
  Coverage   81.73%   81.73%           
=======================================
  Files         458      458           
  Lines       65292    65293    +1     
=======================================
+ Hits        53367    53368    +1     
  Misses      11925    11925           
Files with missing lines Coverage Δ
compiler/ml/lambda.ml 71.13% <100.00%> (ø)
compiler/ml/translcore.ml 88.40% <100.00%> (ø)
compiler/ml/typecore.ml 89.11% <100.00%> (+<0.01%) ⬆️
🚀 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 1adf455bf9

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

@JonoPrest
JonoPrest marked this pull request as ready for review November 3, 2026 07:40
@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 5578f1e

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

@JonoPrest
JonoPrest requested a review from cknitt November 3, 2026 07:56
JonoPrest added a commit that referenced this pull request Nov 3, 2026
The entry names #9187, so the changelog points at this PR.

Signed-off-by: Jono Prest <jjprest@gmail.com>
@JonoPrest
JonoPrest force-pushed the jono/jsx-props-skip-label-copy branch from 1adf455 to d005e48 Compare November 3, 2026 07:59
@cknitt

cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member

This is great! Thanks a lot!

The second commit is not required. PR links will be added by a script at release time.

When I discussed the PR with Claude, it noted:

  • One more copy of the same kind remains. fields_by_position (translcore.ml:1035) runs Array.mapi (fun i lbl -> (lbl, written.(i))) over all labels and builds a new tuple each time, so it forces the same collection.
    • When it runs: transl_record calls it for any record literal without a spread. The JSX path never reaches it: host elements without a spread use Array.to_list fields, and a domProps spread has copy_fields = false because it has 20 or more labels.
    • What still pays for it: a plain literal of a large record type, such as let p: JsxDOM.domProps = {className: "x"}. When such a literal is a constant, field_shape () (translcore.ml:1772) builds another set of tuples. Looking up written.(lbl.lbl_pos) while iterating lbl_all would avoid that copy.

@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: b78ba429d6

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

@JonoPrest

Copy link
Copy Markdown
Contributor Author

This is great! Thanks a lot!

The second commit is not required. PR links will be added by a script at release time.

When I discussed the PR with Claude, it noted:

  • One more copy of the same kind remains. fields_by_position (translcore.ml:1035) runs Array.mapi (fun i lbl -> (lbl, written.(i))) over all labels and builds a new tuple each time, so it forces the same collection.

    • When it runs: transl_record calls it for any record literal without a spread. The JSX path never reaches it: host elements without a spread use Array.to_list fields, and a domProps spread has copy_fields = false because it has 20 or more labels.
    • What still pays for it: a plain literal of a large record type, such as let p: JsxDOM.domProps = {className: "x"}. When such a literal is a constant, field_shape () (translcore.ml:1772) builds another set of tuples. Looking up written.(lbl.lbl_pos) while iterating lbl_all would avoid that copy.

@cknitt thanks! Feedback folded into last commit.

JonoPrest added a commit that referenced this pull request Nov 3, 2026
The entry names #9187, so the changelog points at this PR.

Signed-off-by: Jono Prest <jjprest@gmail.com>
@JonoPrest
JonoPrest force-pushed the jono/jsx-props-skip-label-copy branch from b78ba42 to d4b317a Compare November 3, 2026 15:35
@cknitt

cknitt commented Nov 3, 2026

Copy link
Copy Markdown
Member

The second commit is not required. PR links will be added by a script at release time.

Can you drop the "Link the changelog entry to its pull request" commit?

Why:
    JsxDOM.domProps has 591 optional fields. For every lowercase JSX
    element, jsx_props_of mapped all of them into a fresh array just to
    check whether the record compiles to an array, and typing a record
    literal built a 591 entry array and list to look for missing mandatory
    labels.

    In OCaml 5, Array.map on more than 256 elements forces a minor
    collection, which stops every domain. On a 552 module React app this
    happened about 2,800 times per build, 87% of all minor collections,
    and was most of the extra CPU time a build spends on 7 domains
    compared to 1.

The label array is now checked in place, and the missing label search
is skipped when every label is optional. Records with a required label,
like most component props, take the old path. The output is unchanged.

Review: Why does a 1 domain build barely change? Without other domains
waiting at each forced collection, the copy costs about 55 us per
element. The gain comes from the collections no longer stopping the
other domains.

Signed-off-by: Jono Prest <jjprest@gmail.com>
Why:
    Translating a record literal still built an array of (label, written
    value) pairs over every label, and a second array of (runtime name,
    optional) pairs for its shape. For a record with more than 256 labels,
    such as a JsxDOM.domProps literal, each of these forces a minor
    collection in OCaml 5, which stops every domain.

fields_by_position now returns a list built in position order, and the
shape array is filled in place. Field expressions are still translated
left to right, so the output is unchanged.

Signed-off-by: Jono Prest <jjprest@gmail.com>
@JonoPrest
JonoPrest force-pushed the jono/jsx-props-skip-label-copy branch from d4b317a to 5578f1e Compare November 3, 2026 19:51

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! ❤️

@JonoPrest

Copy link
Copy Markdown
Contributor Author

Can you drop the "Link the changelog entry to its pull request" commit?

Sure, done. It seemed like the script would just skip if it was manually added. I won't add in future 🙏🏼

@JonoPrest
JonoPrest merged commit f9f54a6 into master Nov 4, 2026
26 checks passed
@JonoPrest
JonoPrest deleted the jono/jsx-props-skip-label-copy branch November 4, 2026 05:46
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