Skip to content

perf: add Token::decode_many_from for batch decoding - #1089

Merged
DaniPopes merged 10 commits into
mainfrom
dani/decode-many-from
Mar 25, 2026
Merged

DaniPopes merged 10 commits into
mainfrom
dani/decode-many-from

Conversation

@DaniPopes

Copy link
Copy Markdown
Member

Adds an unsafe decode_many_from method to the Token trait that writes decoded tokens directly into an uninitialized buffer (&mut [MaybeUninit<Self>]). The default implementation loops over decode_from with a drop guard for panic safety.

WordToken overrides this to perform a single copy_nonoverlapping instead of N individual take_word + copy calls, reducing the operation from O(N) decode_from calls (each with a bounds check and 32-byte copy) to one bounds check + one memcpy.

Both DynSeqToken and FixedSeqToken now use decode_many_from for their sequence decoding.

Benchmarks

Decoding an authorizeKey calldata payload with offset aliasing (N shared CallScope entries, each containing N bytes4 selectors):

N Before After Speedup
100 146µs 132µs 1.1x
200 509µs 300µs 1.7x
500 3.4ms 2.3ms 1.5x
1000 14.5ms 10.4ms 1.4x
2000 54.2ms 39.1ms 1.4x
5000 332ms 225ms 1.5x

End-to-end (hyperfine, all sizes combined): 1.30x faster.

Add an unsafe decode_many_from method to the Token trait that writes
decoded tokens directly into an uninitialized buffer. The default
implementation loops over decode_from with a drop guard for safety.

WordToken overrides this to perform a single memcpy via
copy_nonoverlapping instead of N individual word copies, reducing
the operation from O(N) decode_from calls (each with a bounds check)
to a single bounds check + memcpy.

Both DynSeqToken and FixedSeqToken now use decode_many_from.
WordToken overrides this to reinterpret &[WordToken] as &[Word] and
call extend_from_slice, replacing N individual Vec::push calls with
a single memcpy.

FixedSeqToken and DynSeqToken use head_append_many in their
encode_sequence and head_append for non-dynamic inner tokens.
@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Mar 24, 2026
@DaniPopes
DaniPopes enabled auto-merge (squash) March 25, 2026 04:47
@DaniPopes
DaniPopes merged commit 9a19cde into main Mar 25, 2026
31 checks passed
@DaniPopes
DaniPopes deleted the dani/decode-many-from branch March 25, 2026 04:52
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants