Skip to content

Release each iteration's handles in the bench-jsstring NonFlat loops - #8552

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-17-flake-bench-jsstring
Nov 18, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-17-flake-bench-jsstring

Conversation

@littledivy

@littledivy littledivy commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

The nine JsString_Utf8Length_*_NonFlat benchmarks build a cons string with 7 to 31 concat calls on every iteration. The only HandleScope, though, is the one runInIoContext opens around the whole loop. So every cons string, and the flat copy utf8Length makes, stays reachable until the benchmark returns. For the 8192-character cases that's about 16.6 KB per iteration, and after 20000 iterations 325 MB of the V8 heap is still live. On a fast runner Google Benchmark picks around 80000 iterations. V8 then hits its heap limit, with 1368 MB live and nothing freed, and the test aborts. That is the flaky windows bench-jsstring failure. Forcing 200000 iterations of NonFlat_8192 reproduces it on Linux too.

In this patch each NonFlat loop body runs in js.withinHandleScope(), so an iteration's strings become garbage when it ends. The timed work is the same. The old times were mostly GC marking the growing heap, so the numbers drop:

NonFlat before after
live heap, 8192 chars, 20000 iterations 325 MB 1.0 MB
Latin1_8192 median 28.6 us 2.24 us
Utf16_8192 median 26.4 us 2.24 us
Utf16_Invalid_8192 median 33.5 us 11.2 us
Latin1_256 median 2.24 us 0.34 us

Measured with bazel test -c opt on Linux. The heap row comes from GetHeapStatistics() after the loop, and the times are medians of 3 repetitions.

@littledivy
littledivy requested review from a team as code owners November 17, 2026 17:46

@clanky-the-clanker clanky-the-clanker 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.

Superseded by the review of d23efd98.

This review, of 264ef900

✅ Approved — Small, targeted change to a benchmark file. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code reviewer: ✅ LGTM

No issues found.


Reviewed commit: 264ef900

Comment thread src/workerd/tests/bench-jsstring.c++ Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-17-flake-bench-jsstring branch from 264ef90 to d23efd9 Compare November 17, 2026 21:27

@clanky-the-clanker clanky-the-clanker 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.

Superseded by the review of 073e6b01.

This review, of d23efd98

✅ Approved — This is a small, targeted fix to a benchmark: each iteration's handles are released, which removes the unbounded heap growth. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found. Each NonFlat loop body is wrapped in js.withinHandleScope. The lambdas return void, so a plain v8::HandleScope is used, and piece is created outside the scope, so it stays valid across iterations.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: d23efd98

@codecov-commenter

codecov-commenter commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.70%. Comparing base (8e223fe) to head (d889269).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8552      +/-   ##
==========================================
- Coverage   66.70%   66.70%   -0.01%     
==========================================
  Files         797      797              
  Lines      204157   204157              
  Branches    31724    31724              
==========================================
- Hits       136192   136189       -3     
- Misses      49384    49385       +1     
- Partials    18581    18583       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

The nine JsString_Utf8Length_*_NonFlat benchmarks build a cons string
with 7 to 31 concat calls on every iteration, but the only HandleScope
is the one runInIoContext opens around the whole loop. Every cons
string, and the flat copy utf8Length makes, stayed reachable until the
benchmark returned. For the 8192-char cases that is about 16.6 KB per
iteration: after 20000 iterations 325 MB of the V8 heap is still live.
On a fast runner Google Benchmark picks around 80000 iterations, V8
reaches its heap limit (1368 MB live, nothing freed) and the test
aborts, which is the windows bench-jsstring flake. Forcing 200000
iterations of NonFlat_8192 reproduces it on Linux.

In this patch each NonFlat loop body runs in js.withinHandleScope(), so
an iteration's strings become garbage when it ends. The timed work is
the same. The old times were mostly GC marking the growing heap, so
the numbers drop:

  NonFlat, Linux -c opt        before    after
  heap live, 8192, 20000 it    325 MB    1.0 MB
  Latin1_8192 median           28.6 us   2.59 us
  Utf16_8192 median            26.4 us   2.88 us
  Utf16_Invalid_8192 median    33.5 us   12.6 us
  Latin1_256 median            2.24 us   0.40 us
@littledivy
littledivy force-pushed the littledivy/2026-11-17-flake-bench-jsstring branch from d23efd9 to 073e6b0 Compare November 17, 2026 21:54

@clanky-the-clanker clanky-the-clanker 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.

Superseded by the review of d889269f.

This review, of 073e6b01

✅ Approved — This is a small, targeted fix to a benchmark: each iteration's handles are released, which removes the unbounded heap growth. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found. Each NonFlat loop body is wrapped in js.withinHandleScope. The lambdas return void, so a plain v8::HandleScope is used, and piece is created outside the scope, so it stays valid across iterations.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 073e6b01 — same changes as d23efd98, so this is that commit's review (no new review was run; line numbers are as of d23efd98)

@clanky-the-clanker clanky-the-clanker 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.

✅ Approved — This is a small, targeted fix to a benchmark: each iteration's handles are released, which removes the unbounded heap growth. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found. Each NonFlat loop body is wrapped in js.withinHandleScope. The lambdas return void, so a plain v8::HandleScope is used, and piece is created outside the scope, so it stays valid across iterations.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: d889269f — same changes as d23efd98, so this is that commit's review (no new review was run; line numbers are as of d23efd98)

@littledivy
littledivy merged commit b0b176c into main Nov 18, 2026
36 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-17-flake-bench-jsstring branch November 18, 2026 05:20
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.

3 participants