Skip to content

perf(encode): add MarshalAppend for zero-alloc buffer reuse#48

Merged
xe-nvdk merged 1 commit intov6from
perf/marshal-append
Mar 3, 2026
Merged

perf(encode): add MarshalAppend for zero-alloc buffer reuse#48
xe-nvdk merged 1 commit intov6from
perf/marshal-append

Conversation

@xe-nvdk
Copy link
Member

@xe-nvdk xe-nvdk commented Mar 3, 2026

Summary

  • Adds MarshalAppend(dst, v) that appends encoded bytes to a caller-provided buffer, eliminating the final make+copy allocation in Marshal()
  • Refactors Marshal() to delegate to MarshalAppend(nil, v) — behavior is identical
  • Follows the standard Go Append* pattern (like strconv.AppendInt)

Benchmark Results

BenchmarkStructMarshal-14         3139424    378.4 ns/op   1224 B/op   4 allocs/op
BenchmarkStructMarshalAppend-14   4091446    293.8 ns/op     72 B/op   3 allocs/op

~26% faster, 94% less memory when callers reuse a buffer.

Test plan

  • All existing tests pass
  • New benchmark validates improvement

Adds MarshalAppend(dst, v) that appends encoded bytes to a caller-provided
buffer, avoiding the final make+copy in Marshal(). Refactors Marshal()
to delegate to MarshalAppend(nil, v).

Benchmark: StructMarshalAppend is ~26% faster and uses 94% less memory
than Marshal when the caller reuses a buffer.
@xe-nvdk xe-nvdk merged commit 2382197 into v6 Mar 3, 2026
3 checks passed
@xe-nvdk xe-nvdk deleted the perf/marshal-append branch March 3, 2026 01:43
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.

1 participant