Repository navigation
perf: bound the streaming stop scan, real usage counts, --no-token-echo - #203
Merged
Merged
Conversation
Three fixes on the per-token request path: - Stop sequences: the streaming chat and text handlers re-scanned the whole accumulated response on every chunk (O(n^2) in response length; chat always carries five built-in stops). checkStopSequences takes an optional lookback and the streaming paths search only the text since their last scan plus the longest stop string. The chat path tracks characters since the last scan rather than the chunk length, because JSON-mode buffering skips the scan for its first chunks. - usage.completion_tokens counted .chunk events, so tokens the decoder buffers (tool-call bodies, partial text) were never counted. All four paths now take GenerateCompletionInfo.generationTokenCount when .info arrives. An early stop-sequence finish still reports the chunk count, since .info never arrives there. - --no-token-echo turns off the per-token print/fflush to stdout. Default is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014J4GsqznbKNX8tcKzNRxEr
Member
|
Reviewed (with AI assistance, Claude Code) and checked against current main (#202 is merged; no conflicts). I found no blocking issues: the bounded lookback matches a full rescan for grapheme merges, multi-byte text, split stops and JSON-mode buffering. Two notes, neither blocking:
Merging. |
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.
Three small fixes to the per-token request path in
Server.swift.Changes
1. Bounded stop-sequence scan. Both streaming handlers (chat and text completions) ran
checkStopSequenceson the whole accumulated response for every chunk, which is O(n²) in response length. The chat path always carries five built-in stop strings, so this cost applies to every chat request.checkStopSequencesnow takes an optionallookback, and the streaming paths search only the text appended since their last scan plus the longest stop string, with one extra character for grapheme merges.2.
usage.completion_tokensreports real tokens. All four handlers counted.chunkevents. Tokens the decoder buffers, such as tool-call bodies and partial text, emit no chunk, so they were never counted. Each path now takesGenerateCompletionInfo.generationTokenCountwhen.infoarrives. This also correctstimings.predicted_n, tok/s and thegen_tokenslog line. An early stop-sequence finish still reports the chunk count, because.infonever arrives on that path; there's a comment saying so.3.
--no-token-echo. This flag turns off the per-tokenprint+fflush(stdout)in the chat handlers. The default is unchanged. It's documented in the README flags table.Testing
swift test --filter SwiftLMTests: 210 tests, 0 failures. NewStopSequenceTestscases:mlx-community/Ministral-8B-Instruct-2410-4bit:stopcuts cleanly with no leak.stophits correctly.completion_tokensis now 20; the old code would have reported 1.--no-token-echoleaves thesrvlog lines intact.Built and tested with Xcode 27.0 (27A266a), using the submodule as pinned.
There is no CHANGELOG or version file in this repo, so nothing was bumped.
🤖 Generated with Claude Code