perf(stringbuilder): reuse unshared reset buffer - #3843
Open
mizchi wants to merge 5 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.
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
StringBuilderbacking buffer onreset()when it has not been exposed by the zero-copyto_string()path.buffer_is_sharedflag and detach only then.Why
The native/wasm
StringBuilder::resetimplementation always allocated and zero-filled a newFixedArray[UInt16], even whento_string()had already copied the contents. This defeated the intended spare-capacity reuse.A full buffer may be turned into a
Stringwithout copying, so simply settinglen = 0would mutate a string previously returned byto_string(). The new flag preserves that case by allocating a fresh buffer only when necessary.Benchmark
Existing benchmark:
StringBuilder::reset reuse spare capacity n=4096. Lower is better.Validation
moon test builtin --target all --quietmoon check builtin --target allmoon info builtinmoon fmt builtin/stringbuilder_buffer.mbt builtin/stringbuilder_wbtest.mbtNo public API changes.