perf(stringbuilder): reuse unshared reset buffer - #3843
Open
mizchi wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the native/wasm builtin StringBuilder implementation by allowing reset() to reuse the existing UTF-16 backing buffer when it’s safe (i.e., when the buffer hasn’t been exposed via the zero-copy to_string() fast path), reducing allocations and zero-fills while preserving correctness.
Changes:
- Add an internal
buffer_is_sharedflag to track whento_string()returns a zero-copy string backed by the builder’s buffer. - Update
reset()to reuse the existing buffer unless it has been shared, in which case it detaches by allocating a fresh buffer. - Add a non-JS whitebox regression test validating that
reset()reuses an unshared buffer.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| builtin/stringbuilder_buffer.mbt | Track zero-copy sharing in to_string() and reuse backing storage in reset() when safe. |
| builtin/stringbuilder_wbtest.mbt | Add a non-JS whitebox test that asserts buffer reuse behavior across reset(). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mizchi
force-pushed
the
perf/stringbuilder-reset
branch
from
August 24, 2026 15:41
b512ce6 to
9304407
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
StringBuilderstorage onreset()unless a zero-copyto_string()result shares it.UninitializedArray[UInt16], initializing only0..<lenand avoiding redundant zero-fills before writes.Benchmark
Apple M5, MoonBit
0.1.20260824, native release. Three sequential runs per revision; lower is better.93044073)48486332)Command:
moon bench --release --target native builtin/stringbuilder_large_bench_test.mbtValidation
moon test builtin --target all: wasm 2960, wasm-gc 2960, JS 2935, native 2919; all passedmoon checkmoon info; no public interface changes