Skip to content

Rename a for loop's start expression once when inlining - #9396

Merged
cknitt merged 2 commits into
masterfrom
lam-bounded-vars-for
Nov 23, 2026
Merged

cknitt merged 2 commits into
masterfrom
lam-bounded-vars-for

Conversation

@cknitt

@cknitt cknitt commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Lam_bounded_vars renames the variables bound inside a function body before the function is inlined. Its for loop case rewrote the loop's start expression twice:

let l1 = aux l1 in
...
Lambda.for_ ident (aux l1) l2 dir l3

This has been there since at least 2019. The second pass only renamed the already fresh binders again, consistently, so the result was the same program. Now the start expression is rewritten once.

JS output and diagnostics are byte-identical on 77 benchmark files.

New tests:

  • tests/ounit_tests/ounit_lam_bounded_vars_tests.ml rewrites a for loop whose start expression binds a variable, and checks that the binder, its use and the renaming map agree. It fails with the old double rename.
  • tests/tests/src/inline_for_start_test.res applies two function literals right away, which inlines them through Lam_bounded_vars, with a let and a callback in the loop's start expression. Functions with loops are otherwise too big to inline, so this is the path that reaches the for-loop case.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH

@cknitt
cknitt added this pull request to stack #9397 November 23, 2026 06:35
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Changelog

🏠 Internal

  • Rename the variables in a for loop's start expression once instead of twice when inlining a function.

Semantic Diff

2 commits · Full page

Rename a for loop's start expression once when inlining

ec4cc89 · cknitt + Claude · Nov 22, 2026, 20:52 UTC

Lam_bounded_vars renames the variables bound inside a function body before it is inlined. Its for-loop case rewrote the start expression twice (let l1 = aux l1 in ... Lambda.for_ ident (aux l1) ...), a slip that has been there since at least 2019. The second pass only renamed the fresh binders again, consistently, so the result was the same program; JS output and diagnostics are byte-identical on 77 benchmark files, and allocation drops by 56 words in total.

Test the renaming of a for loop's start expression when inlining

4c7a4c3 · cknitt + Claude · Nov 23, 2026, 08:37 UTC

The previous commit had no lasting test, as Codex pointed out. A unit test now rewrites a for loop whose start expression binds a variable and checks that the binder, its use and the renaming map agree, with one map entry per binder; with the old double rename the map records a name the result doesn't use. An end-to-end test applies two function literals right away, which inlines them through Lam_bounded_vars, with a let and a callback in the loop's start expression, and checks their results. Functions with loops are otherwise too big to inline, so this is the path that reaches the for-loop case.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.21%. Comparing base (449d6e7) to head (4c7a4c3).

Files with missing lines Patch % Lines
tests/ounit_tests/ounit_lam_bounded_vars_tests.ml 90.47% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9396   +/-   ##
=======================================
  Coverage   82.21%   82.21%           
=======================================
  Files         460      461    +1     
  Lines       65552    65573   +21     
=======================================
+ Hits        53892    53914   +22     
+ Misses      11660    11659    -1     
Files with missing lines Coverage Δ
compiler/backend/lam_bounded_vars.ml 69.33% <100.00%> (+6.66%) ⬆️
tests/ounit_tests/ounit_tests_main.ml 100.00% <ø> (ø)
tests/ounit_tests/ounit_lam_bounded_vars_tests.ml 90.47% <90.47%> (ø)

... and 1 file 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.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 636,986,837 → 636,986,804 (±0.00%).

Generated projects: 629,044,295 → 629,044,801 (±0.00%).

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

Instructions

In total: 9,221,398,536 → 9,221,420,665 (±0.00%).

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

Output size

In total: 25,976,823 → 25,976,816 (±0.00%).

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

Pull request 4c7a4c3 compared with base 449d6e7 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@9396

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 4c7a4c3

@cknitt

cknitt commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8ed82e324

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

Comment thread compiler/backend/lam_bounded_vars.ml Resolved
@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-23T08:41:58.156529Z a07c272 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.

@cknitt

cknitt commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: a07c272b6b

ℹ️ 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:51
cknitt and others added 2 commits November 23, 2026 19:43
Lam_bounded_vars renames the variables bound inside a function body before
it is inlined. Its for-loop case rewrote the start expression twice
(`let l1 = aux l1 in ... Lambda.for_ ident (aux l1) ...`), a slip that has
been there since at least 2019. The second pass only renamed the fresh
binders again, consistently, so the result was the same program; JS output
and diagnostics are byte-identical on 77 benchmark files, and allocation
drops by 56 words in total.

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>
The previous commit had no lasting test, as Codex pointed out. A unit test
now rewrites a for loop whose start expression binds a variable and checks
that the binder, its use and the renaming map agree, with one map entry
per binder; with the old double rename the map records a name the result
doesn't use. An end-to-end test applies two function literals right away,
which inlines them through Lam_bounded_vars, with a let and a callback in
the loop's start expression, and checks their results. Functions with
loops are otherwise too big to inline, so this is the path that reaches
the for-loop case.

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 removed this pull request from stack #9397 November 23, 2026 19:44
@cknitt
cknitt changed the base branch from parsetree-mapper-sharing to master November 23, 2026 19:44
@cknitt
cknitt force-pushed the lam-bounded-vars-for branch from a07c272 to 4c7a4c3 Compare November 23, 2026 19:44
@cknitt
cknitt enabled auto-merge (squash) November 23, 2026 19:55
@cknitt
cknitt merged commit 45dba2c into master Nov 23, 2026
23 checks passed
@cknitt
cknitt deleted the lam-bounded-vars-for branch November 23, 2026 20:17
@github-actions

Copy link
Copy Markdown

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