perf(utf8): reduce lossy decode output allocation - #4143
Merged
peter-jerry-ye merged 2 commits intoAug 26, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the non-JS UTF-8 decode_lossy implementation by reducing UTF-16 scratch buffer over-allocation and avoiding an extra allocation/copy when the scratch buffer is filled exactly, improving throughput and peak memory usage.
Changes:
- Size the non-JS
decode_lossyUTF-16 scratchFixedArray[UInt16]tosrc_length(instead ofsrc_length * 2). - Reuse the scratch buffer as the final result when
written == src_lengthto avoid allocating/copying. - Add benchmark tests for
decode_lossyand include thebenchtest dependency.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| encoding/utf8/moon.pkg | Adds moonbitlang/core/bench as a test-only dependency to support new benchmarks. |
| encoding/utf8/decode_nonjs.mbt | Reduces scratch allocation for decode_lossy and reuses scratch when fully utilized to avoid extra allocation/copy. |
| encoding/utf8/decode_lossy_bench_test.mbt | Adds benchmark coverage for ASCII, invalid, and mixed UTF-8 inputs at 1,000,000 bytes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
peter-jerry-ye
force-pushed
the
perf/utf8-lossy-output-allocation
branch
from
August 26, 2026 08:38
f70acc0 to
7eb876d
Compare
peter-jerry-ye
enabled auto-merge (rebase)
August 26, 2026 09:26
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
Size the non-JS
decode_lossyUTF-16 scratch array to the input byte length instead of twice that length: one UTF-8 byte can produce at most one UTF-16 code unit, including replacement characters for malformed input. When decoding fills the scratch array exactly, reuse it as the result instead of allocating and copying again.This also halves peak scratch storage.
Benchmark
Native release, 1,000,000 input bytes:
Validation
moon test encoding/utf8 --target allmoon checkmoon info(no interface changes)