Skip to content

perf: add Token::head_append_many for batch encoding - #1090

Merged
DaniPopes merged 7 commits into
mainfrom
dani/encode-many
Mar 25, 2026
Merged

DaniPopes merged 7 commits into
mainfrom
dani/encode-many

Conversation

@DaniPopes

Copy link
Copy Markdown
Member

Adds a head_append_many method to the Token trait for bulk encoding a slice of tokens. The default implementation loops over head_append.

WordToken overrides this to reinterpret &[WordToken] as &[Word] (both #[repr(transparent)]) and calls Encoder::append_words (extend_from_slice), replacing N individual Vec::push calls with a single memcpy.

FixedSeqToken and DynSeqToken use head_append_many in their head_append and encode_sequence for non-dynamic inner tokens, also skipping the unnecessary offset tracking in that case.

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 25, 2026
Comment thread crates/sol-types/src/abi/token.rs Outdated
/// Shared implementation for [`TokenSeq::encode_sequence`] used by both
/// [`FixedSeqToken`] and [`DynSeqToken`].
fn encode_sequence_impl<'de, T: Token<'de>>(tokens: &[T], enc: &mut Encoder) {
if T::DYNAMIC {

@DaniPopes DaniPopes Mar 25, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if !T::DYNAMIC, push_offset pushes 0, bump_offset increases it by inner.tail_words which is 0, tail_append is a noop, and pop_offset pops the 0 off the stack. so this whole thing reduces to for x in tokens { x.head_append(enc) } -- then this optimizes to head_append_many.

@DaniPopes
DaniPopes enabled auto-merge (squash) March 25, 2026 15:24
@DaniPopes
DaniPopes merged commit f3651f2 into main Mar 25, 2026
31 checks passed
@DaniPopes
DaniPopes deleted the dani/encode-many branch March 25, 2026 15:35
@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