Skip to content

Stop measuring lambda size once the inlining limit is reached - #8771

Merged
cknitt merged 4 commits into
quick-perf-fixesfrom
perf-lam-size
Oct 10, 2026
Merged

cknitt merged 4 commits into
quick-perf-fixesfrom
perf-lam-size

Conversation

@cknitt

@cknitt cknitt commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Stacked on #8769.

Lam_analysis.size estimates a lambda's size for inlining decisions. Its callers only compare the result with small thresholds (5, 7 or 10), but it walked the whole lambda every time, including at every call site of a known function (Lam_pass_remove_alias, which runs several times per module). It also installed an exception handler at every level of the recursion.

size is replaced by size_upto ~limit, which stops counting once the running total reaches limit and returns the size if it is below limit, limit otherwise. Each caller passes the largest threshold it compares with, so every comparison has the same result as before. Constructs that made size return 1000 (loops, switches, try, ...) still count as 1000, which is above any limit. Constants count their leaves against the same limit, so a large constant list or record in a function body is not walked completely either.

Measurements

Same method as #8769: allocated words (OCAMLRUNPARAM=v=0x400, deterministic) and CPU time as the minimum of 7 interleaved runs, compared with the previous PR in the stack. Every benchmark file compiles to byte-identical JS and diagnostics.

CPU time Allocated words
Stress: a 400-statement function called from 600 sites 71.1 → 41.0 ms (−42%) +0.4%
Stress: a function with a 2000-element constant list called from 300 sites (vs. the first version of this PR) 278.9 → 236.6 ms (−15%) +0.1%
55 real files (Belt, larger integration tests) −1.9% (≈ noise) +0.08%

The small allocation increase is the counter's closure.

🤖 Generated with Claude Code

@cknitt
cknitt added this pull request to stack #8775 October 10, 2026 10:17
@cknitt

cknitt commented Oct 10, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 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-10-10T10:55:45.664405Z ea0d484 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 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: 43dcace26a

ℹ️ 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/core/lam_analysis.ml
@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 5 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (quick-perf-fixes@df0fcbb). Learn more about missing BASE report.

Files with missing lines Patch % Lines
compiler/core/lam_analysis.ml 88.37% 5 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                 @@
##             quick-perf-fixes    #8771   +/-   ##
===================================================
  Coverage                    ?   80.66%           
===================================================
  Files                       ?      463           
  Lines                       ?    62758           
  Branches                    ?        0           
===================================================
  Hits                        ?    50623           
  Misses                      ?    12135           
  Partials                    ?        0           
Files with missing lines Coverage Δ
compiler/core/lam_pass_exits.ml 90.54% <100.00%> (ø)
compiler/core/lam_stats_export.ml 91.11% <ø> (ø)
tests/ounit_tests/ounit_lam_size_tests.ml 100.00% <100.00%> (ø)
tests/ounit_tests/ounit_tests_main.ml 100.00% <ø> (ø)
compiler/core/lam_analysis.ml 78.76% <88.37%> (ø)
🚀 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 commented Oct 10, 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: a620591952

ℹ️ 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/core/lam_analysis.ml Outdated
@cknitt
cknitt marked this pull request as ready for review October 10, 2026 10:52
@cknitt

cknitt commented Oct 10, 2026

Copy link
Copy Markdown
Member Author

@codex review

@cknitt
cknitt requested a review from cristianoc October 10, 2026 10:55
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ea0d484bde

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

@pkg-pr-new

pkg-pr-new Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@8771

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: a08dc5c

cknitt and others added 4 commits October 10, 2026 15:22
Callers only compare the size with small thresholds, but it was computed
for the whole body at every call site of a known function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
Unit tests for size_upto at the inlining limits, and a fixture with calls
on both sides of the thresholds (its JS is the same as before the change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
Large constant lists or records were still walked completely before the
limit was checked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt merged commit 54e6219 into master Oct 10, 2026
35 of 36 checks passed
@cknitt
cknitt deleted the perf-lam-size branch October 10, 2026 15:42
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