Skip to content

Optimize hex serialization#4667

Merged
jmg-duarte merged 1 commit into
mainfrom
optimize-hex-serialization
Jul 24, 2026
Merged

Optimize hex serialization#4667
jmg-duarte merged 1 commit into
mainfrom
optimize-hex-serialization

Conversation

@MartinquaXD

@MartinquaXD MartinquaXD commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Description

const_hex offers a way to serialize bytes into a fixed size stack allocated buffer.

The new version is twice as fast (in benchmarks):

to_string/current       time:   [23.257 ns 23.766 ns 24.292 ns]
to_string/buffer        time:   [11.992 ns 12.068 ns 12.173 ns]

Benefits comes from avoiding the heap, avoiding the .unwrap and avoiding the utf8 check as it uses the unchecked version internally; finally, it should also result in a smaller binary size (though it will not be noticeable).

Changes

Use const_hex::Buffer::<N, PREFIX>::new() for all code serializing fixed sized bytes.
Also cleans up 2 implementations for variable length bytes serialization.

How to test

all tests should still pass as this should not introduce any behavior changes.

@MartinquaXD
MartinquaXD requested a review from a team as a code owner July 24, 2026 08:13
@claude

claude Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Claude finished @MartinquaXD's task in 1m 43s —— View job


PR Review: Optimize hex serialization

  • Read changed files & diff
  • Review correctness of hex serialization changes
  • Post inline comments for any issues
  • Post summary

Verdict: LGTM ✅ — no issues found, no inline comments needed.

This is a clean, behavior-preserving optimization. Verified:

  • Buffer sizes match the byte arrays (const generics enforce this at compile time, and format(&[u8; N]) is passed a matching array in every case):
    • OrderUid(pub [u8; 56])Buffer::<56, true> in both app_data.rs and order.rs
    • AppDataHash(pub [u8; 32])Buffer::<32, true>
    • EcdsaSignature::to_bytes() -> [u8; 65]Buffer::<65, true>
    • serde-ext Hex::serialize_as::<[u8; N]>Buffer::<N, true>
  • Output unchanged: const_hex::Buffer with PREFIX = true produces lowercase 0x-prefixed hex, identical to the previous to_string() / manual encode_to_slice / encode_prefixed paths.
  • No dependency changes needed: const-hex (1.17) is already a direct dependency of model, app-data, serde-ext, and bytes-hex.
  • The bytes-hex and serde-ext variable-length/encode_prefixed cleanups are equivalent to what they replaced (and stack-allocate where the length is fixed).

The stack-allocated fixed-size buffers avoid the heap for the hot fixed-size serialization paths — a solid improvement, and at worst on-par as the description notes.

Note: I couldn't run cargo check in this environment (command not permitted), but the changes are correct by construction — a size mismatch between Buffer::<N> and the array passed to format would fail to compile.

@jmg-duarte
jmg-duarte enabled auto-merge July 24, 2026 08:45
@jmg-duarte
jmg-duarte added this pull request to the merge queue Jul 24, 2026
Merged via the queue into main with commit 398f113 Jul 24, 2026
24 checks passed
@jmg-duarte
jmg-duarte deleted the optimize-hex-serialization branch July 24, 2026 08:55
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants