Skip to content

perf(bytes): skip redundant initialization in concatenation - #4142

Open
mizchi wants to merge 3 commits into
moonbitlang:mainfrom
mizchi:perf/bytes-add-uninitialized
Open

perf(bytes): skip redundant initialization in concatenation#4142
mizchi wants to merge 3 commits into
moonbitlang:mainfrom
mizchi:perf/bytes-add-uninitialized

Conversation

@mizchi

@mizchi mizchi commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Avoid zero-initializing the concatenation result on non-JS targets before both inputs overwrite the full allocation. Allocate uninitialized storage, fill the two non-overlapping ranges with blits, then reinterpret the fully initialized buffer as Bytes. JS keeps the existing implementation because the required intrinsic is unavailable there.

Benchmark

Native release, 500,000 + 500,000 bytes:

Before After
23.77 us 20.62 us

Validation

  • moon test builtin --target all
  • moon check
  • moon info (no interface changes)

Copilot AI lite review requested due to automatic review settings August 24, 2026 17:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes Bytes concatenation on non-JS targets by avoiding redundant zero-initialization, while keeping the existing JS implementation for compatibility.

Changes:

  • Refactors Bytes + Bytes to delegate to a target-specific bytes_add_impl and uses uninitialized allocation + blits on non-JS targets.
  • Adds a regression test to ensure the concatenation result is fully initialized and correctly contains both inputs.
  • Adds a benchmark covering large Bytes concatenation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
builtin/bytes.mbt Implements a non-JS fast path for Bytes::add using uninitialized allocation and two blits; keeps the JS path unchanged.
builtin/bytes_test.mbt Adds a test validating Bytes::add initializes and populates the full result correctly.
builtin/bytes_add_bench_test.mbt Introduces a benchmark for large Bytes concatenation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread builtin/bytes_add_bench_test.mbt Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants