perf: skip the decoder flush window when only one sequence is live#11
Merged
Conversation
The decoder thread waited a fixed 200us batching window after every sample request, even with a single live sequence where no sibling can ever arrive; that is pure added latency per sampled token for the autocomplete app, which holds exactly one sequence. The flush decision reads a lock-free mirror of the sequence count (an atomic updated under sequences_mutex at every map mutation) because taking sequences_mutex while holding decode_mutex would invert the order destroySequence uses. Multi-sequence drivers keep the batching window unchanged.
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.
Summary
The decoder thread's batching design waits
BATCH_WINDOW_MICROS(200us) after the first sample request so sibling sequences can pile into onellama_decode. With a single live sequence there is no sibling to wait for, so the window was pure added latency on every sampled token; at a 20-26 token suggestion that is ~4-5ms per suggestion for nothing. The autocomplete app holds exactly one sequence by design (it destroys the old sequence before building a fresh one), so it paid this on every generation.The flush decision now reads
live_sequence_count, a lock-free atomic mirror ofsequences.size()updated undersequences_mutexat every map mutation (create, destroy, destroy-all). An atomic mirror rather than takingsequences_mutexin the decoder because the decoder holdsdecode_mutexat the decision point, anddestroySequencenestsdecode_mutexinside its sequence-scoped section, so lockingsequences_mutexthere would invert the order. The mirror can lag a concurrent create/destroy by at most one flush decision, which only means one window waited (or skipped) conservatively.Multi-sequence drivers (evals, tests running two sequences side by side) keep the batching window unchanged.
Validation
Behavioral note: single-sequence end-to-end timing is best confirmed via the bench against a local GGUF; the change is a strict removal of a fixed wait on that path.
🤖 Generated with Claude Code