Adjust StringBuilder size hint semantics#3627
Open
bobzhang wants to merge 1 commit into
Open
Conversation
Collaborator
Coverage Report for CI Build 4484Coverage remained the same at 94.089%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Contributor
There was a problem hiding this comment.
Pull request overview
Updates StringBuilder constructor semantics to treat size_hint consistently as a UTF-16 code-unit capacity hint, with a small eager default to reduce early reallocations, and aligns behavior across the buffer-backed (non-JS) and concat-backed (JS) implementations.
Changes:
- Changed
StringBuilder()defaultsize_hintfrom0to8(UTF-16 code units) and updated docstrings accordingly. - Made
size_hint=0a supported “empty initial buffer” path by allowing zero-capacity buffers and updating growth logic to handle it. - Rejected negative
size_hintvalues in both implementations and added tests for zero-capacity growth and negative-hint rejection.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| builtin/stringbuilder_buffer.mbt | Switches to UTF-16 code-unit capacity hints, allows 0-capacity buffers, and updates growth logic to handle zero capacity. |
| builtin/stringbuilder_concat.mbt | Aligns constructor defaults/docs and rejects negative size_hint even though the hint is ignored on JS. |
| builtin/stringbuilder_test.mbt | Adds coverage for size_hint=0 growth and negative size_hint constructor panics. |
💡 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
StringBuilder()to use an eager default capacity hint of 8 UTF-16 code unitssize_hintconsistently as UTF-16 code-unit capacity rather than bytesStringBuilder(size_hint=0)as the explicit empty-buffer path and make buffer growth handle zero capacitysize_hintvalues on both buffer-backed and JS concat-backed implementationsValidation
moon fmt builtin/stringbuilder_buffer.mbt builtin/stringbuilder_concat.mbt builtin/stringbuilder_test.mbtmoon check builtinmoon test builtinmoon infomoon ide doc StringBuilder::StringBuildergit diff --checkmoon checkmoon test