Perf/byte array fields - #4
Open
Simon-Calbert-Aerospacelab wants to merge 2 commits into
Open
Conversation
Simon-Calbert-Aerospacelab
force-pushed
the
perf/batch-enum-bit-fields
branch
from
August 11, 2026 11:53
949f598 to
c0305a8
Compare
Simon-Calbert-Aerospacelab
force-pushed
the
perf/byte-array-fields
branch
2 times, most recently
from
August 11, 2026 13:06
af2540a to
7b11aef
Compare
Simon-Calbert-Aerospacelab
force-pushed
the
perf/batch-enum-bit-fields
branch
from
August 11, 2026 13:06
c0305a8 to
4f20248
Compare
Simon-Calbert-Aerospacelab
force-pushed
the
perf/batch-enum-bit-fields
branch
from
September 3, 2026 14:19
4f20248 to
16088c6
Compare
Simon-Calbert-Aerospacelab
force-pushed
the
perf/byte-array-fields
branch
from
September 3, 2026 14:20
7b11aef to
7b4b35b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
A
[u8; N]field is read one byte at a time.[T; N]has a single generic impl, and it has to be general: it loops overMaybeUninit, callsT::from_reader_with_ctxper element, and carries adrop-on-error unwind path in case a later element fails after earlier ones were
constructed.
For
T = u8none of that is needed. The bytes are the value. Measured on areal pipeline, an IPv4 header's two 4-byte address fields cost 168 instructions
each. A hand-written parser does one 4-byte load.
This PR has the derive emit one call when the field is syntactically
[u8; N].Stacked on #3.
What the derive emits
Read:
Write:
Both methods already exist on
ReaderandWriterand are already public, andboth already handle the unaligned case. This PR adds no runtime code at all; it
only changes which existing call the derive emits.
When a field does not qualify
The field must be
[u8; N]with a literalN, and carry no other dekuattribute: not
bits,bytes,count,bytes_read,until,read_all,map,ctx,update,reader,writer,skip,cond,assert,assert_eq,temp,temp_value,magic, anypad_*, or anyseek_*. Each ofthose either moves the cursor, makes the read conditional, or changes what a
single field means.
Two notes on the gates:
endian = "little"container qualifies too.Msb0only. On an unaligned cursorread_bytes_const_intoreverses thebuffer for
Lsb0, which is not what a sequence of byte reads does. Rather thanreason about that,
Lsb0keeps the existing path.Anything that does not qualify is untouched, so this is purely additive.
Concretely, per file
deku-derive/src/macros/deku_read.rs:byte_array_len, the eligibilitycheck, plus one arm in the field-read emitter.
deku-derive/src/macros/deku_write.rs: one arm in the field-write emitter,reusing the same check.
That is the whole change.
Numbers
Per pair of 4-byte address fields, from a bench that pushes 96 items through a
single reader or writer:
On a production 9-protocol decode pipeline, throughput went from 798.0 to
832.5 MiB/s, +4.3%.
Callgrind on that pipeline confirms the mechanism rather than just the outcome:
169.11M to 165.90M instructions. The generic
[T; N]impl disappears fromthe profile entirely, and
read_bytes_const_intodrops from 2.73M instructionsto 1.01M.
Reproduce with
cargo bench --bench bebits --all-features.Testing
The risk here is the unaligned path, so that is where the tests concentrate.
tests/test_byte_arrays.rshas 13 tests:[u8; 4]that starts mid-byteand comparing against the generic
[T; N]impl invoked directly as anoracle, over 199 seeds each, plus a byte-exact write round-trip.
Incompleterather than a panic or a short read.[u16; 2], abitsattribute,pad_bytes_before, and amap.Full CI feature matrix. Separately, the output was checked against independent
hand-written parsers for seven real protocol headers: 8.2M cases, zero
mismatches.