Skip to content

perf(bytes): write escaped hex digits directly - #4141

Merged
bobzhang merged 1 commit into
moonbitlang:mainfrom
mizchi:perf/bytes-output-hex
Aug 27, 2026
Merged

perf(bytes): write escaped hex digits directly#4141
bobzhang merged 1 commit into
moonbitlang:mainfrom
mizchi:perf/bytes-output-hex

Conversation

@mizchi

@mizchi mizchi commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Write escaped byte hex digits directly to the output logger instead of allocating a two-character String via Byte::to_hex() for every escaped byte. The private helper is shared by Show and ToJson.

Benchmark

Native release, 100,000 binary bytes:

Operation Before After
Bytes Show 1.65 ms 544.43 us
Bytes ToJson 1.46 ms 508.42 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:27

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

Improves Bytes/BytesView escaping performance by writing hex digits directly to a Logger instead of allocating a 2-character String per escaped byte, and validates behavior with a targeted regression test and a benchmark.

Changes:

  • Added a private write_byte_hex(&Logger, Byte) helper in builtin/bytesview.mbt to emit two lowercase hex digits without allocating.
  • Updated Show and ToJson implementations for BytesView to use the new helper for \xHH escapes.
  • Added a new behavior test ensuring lowercase hex output, plus a new benchmark test file for Bytes Show and ToJson on binary data.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
builtin/bytesview.mbt Introduces write_byte_hex and switches BytesView Show/ToJson escaping to direct hex digit writes.
builtin/bytes_test.mbt Adds a regression test asserting exact lowercase \xhh escaping for both Show and ToJson.
builtin/bytes_output_bench_test.mbt Adds benchmarks for Bytes Show/ToJson on 100k binary bytes to quantify the perf impact.

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

@bobzhang
bobzhang force-pushed the perf/bytes-output-hex branch from a8429c2 to 5781efe Compare August 27, 2026 03:37
@bobzhang
bobzhang enabled auto-merge (rebase) August 27, 2026 03:40
@bobzhang
bobzhang merged commit 39055be into moonbitlang:main Aug 27, 2026
13 of 20 checks passed
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.

3 participants