Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the non-JS StringBuilder (UTF-16 buffer-backed implementation) against Int overflow during capacity doubling, preventing potential non-terminating growth loops for very large required sizes.
Changes:
- Introduces a private
stringbuilder_grow_capacity(current, required)helper that detects overflow during doubling and falls back to allocating exactlyrequired. - Updates
StringBuilder::grow_if_necessaryto use the new helper instead of an unguarded doubling loop. - Adds allocation-free whitebox tests covering normal doubling behavior and overflow boundary cases, plus a focused benchmark for growth from the minimum capacity.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| builtin/stringbuilder_buffer.mbt | Adds overflow-safe capacity growth helper and wires it into grow_if_necessary. |
| builtin/stringbuilder_grow_wbtest.mbt | Adds whitebox tests validating normal doubling and overflow survival without allocating. |
| builtin/stringbuilder_bench_test.mbt | Adds a benchmark targeting growth behavior starting from minimal initial capacity. |
| builtin/moon.pkg | Excludes the new whitebox test from JS targets to match the non-JS implementation selection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
545c6ce to
05f4de3
Compare
05f4de3 to
2cab5a0
Compare
|
Thanks for this, and sorry it sat so long. The overflow guard itself has since landed in b903d49 ("fix: make buffer growth overflow-safe"), which introduced After your 2026-09-01 rebase this PR is down to The benchmark is worth having on its own, though: every current StringBuilder bench passes a Heads-up that #3822 is in the same position — it still edits |
Summary
StringBuildercapacity growth againstIntoverflow.Root cause
StringBuilder::grow_if_necessaryrepeatedly doubled the backing capacity. Once the capacity exceeded2^30,Intoverflow could make the loop cycle through non-positive values and never reach the requested length. The new private helper falls back to the exact requested capacity after an overflowing double.This is the
StringBuildercounterpart to #3822, but it changes the separate UTF-16 builder implementation.Benchmark
StringBuilder growth from minimum capacity n=4096, lower is better.0.1.20260824moon bench --release --target native -p moonbitlang/core/builtin -f stringbuilder_bench_test.mbt -i 5Each run contains 10 benchmark samples.
Validation
moon info && moon fmtmoon test: 7546 passed, 0 failedmoon check