Repository navigation
perf: add Token::head_append_many for batch encoding - #1090
Merged
Merged
Conversation
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.
mattsse
approved these changes
Mar 25, 2026
DaniPopes
commented
Mar 25, 2026
| /// 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 { |
Member
Author
There was a problem hiding this comment.
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
enabled auto-merge (squash)
March 25, 2026 15:24
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.
Adds a
head_append_manymethod to theTokentrait for bulk encoding a slice of tokens. The default implementation loops overhead_append.WordTokenoverrides this to reinterpret&[WordToken]as&[Word](both#[repr(transparent)]) and callsEncoder::append_words(extend_from_slice), replacing N individualVec::pushcalls with a single memcpy.FixedSeqTokenandDynSeqTokenusehead_append_manyin theirhead_appendandencode_sequencefor non-dynamic inner tokens, also skipping the unnecessary offset tracking in that case.