Skip to content

[Go] Generate zero-copy byte accessors for flyweight var data - #1134

Merged
vyazelenko merged 2 commits into
aeron-io:masterfrom
slachiewicz:go-flyweight-vardata-view
Oct 19, 2026
Merged

vyazelenko merged 2 commits into
aeron-io:masterfrom
slachiewicz:go-flyweight-vardata-view

Conversation

@slachiewicz

Copy link
Copy Markdown
Contributor

Flyweight var data can be read only as a new string (<Name>(), Get<Name>AsString()), which allocates, or copied into a caller's buffer (Get<Name>(dst)). This adds <Name>Bytes() []byte, which returns the data as a slice of the message buffer, capacity capped at its length, and moves past it like the other getters. The slice is valid only while the buffer holds the message, as the generated comment says. A field added in a later version returns nil for an older message.

It also adds BenchmarkEncodeCar, BenchmarkDecodeCar and BenchmarkDecodeCarBytes to the flyweight baseline package; the decode benchmarks read every field of the Car, groups included, with the four var data fields read as strings or as bytes. Linux arm64 (Azure Cobalt 100), Go 1.27, alternating, 10 runs each:

ns/op allocs/op
BenchmarkDecodeCar (var data as strings) 176.53 6
BenchmarkDecodeCarBytes (var data as bytes) 89.62 0

Reading a single 52-byte var data field of an Aeron Archive ReplayRequest: <Name>() 34.88 ns and 1 allocation, Get<Name>(dst) 13.88 ns, <Name>Bytes() 11.64 ns and no allocation.

Verified: ./gradlew generateGolangCodecs && cd gocode && make → all Go tests pass, including the new TestVarDataBytes (values, aliasing of the message buffer, capped capacity, no allocations); a schema with sinceVersion="1" var data returns the value at version 1 and nil at version 0; checkstyle clean.

<Name>Bytes() returns the data as a slice of the message buffer, so a
decoder can read var data without allocating or copying.
@vyazelenko
vyazelenko merged commit 6f7a40b into aeron-io:master Oct 19, 2026
34 checks passed
@slachiewicz
slachiewicz deleted the go-flyweight-vardata-view branch October 21, 2026 09:48
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