perf(sidecar): skip usage rewrite parsing for frames without usage - #2481
Open
sudoalok wants to merge 1 commit into
Open
perf(sidecar): skip usage rewrite parsing for frames without usage#2481sudoalok wants to merge 1 commit into
sudoalok wants to merge 1 commit into
Conversation
Signed-off-by: Alok Behera <alokbeherak061@gmail.com>
roytman
reviewed
Aug 20, 2026
| @@ -215,3 +216,24 @@ var _ = Describe("Cached token usage rewriter", func() { | |||
| Expect(recorder.Body.String()).To(ContainSubstring(`"cached_tokens":7`)) | |||
| }) | |||
| }) | |||
Contributor
There was a problem hiding this comment.
The two new benchmarks exercise the no-usage guard's performance but don't assert correctness. Every existing It case in this file that reaches replaceCachedTokensJSON has usage in its body, and the one case that doesn't ("should preserve non-JSON streamed data lines") uses non-JSON, which isn't valid JSON either.
So there's no test covering the actual hot path this PR optimizes: a valid-JSON streaming delta with no usage field. If the guard's key or quoting were ever wrong, no spec would catch it, only a benchmark number silently changing.
Could you add something like:
It("should preserve streamed content chunks without usage", func() {
body := []byte(`data: {"choices":[{"delta":{"content":" the"}}]}` + "\n\ndata: [DONE]\n")
updated := replaceCachedTokens(body, 7)
Expect(updated).To(Equal(body))
})
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.
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
replaceCachedTokensSSELine unmarshals every sse frame looking for usage, but only the last frame in a stream has usage in it. so a 500 token response does ~500 full json.Unmarshal calls to end up rewriting one field once.
put a bytes.Contains check for "usage" in front of the parse. frames without it already came out unchanged, they just paid for the unmarshal first, so the output is the same. if the model writes the word usage in its own text the check passes and the parse runs and finds nothing, same as now.
numbers from the benchmarks in this PR:
content frame: 3689 ns/op 1816 B/op 40 allocs/op -> 224 ns/op 0 B/op 0 allocs
usage frame: unchanged
added benchmarks for both frame shapes to the existing test file. existing tests pass, race clean, vet and gofmt clean.
the openai streaming parser already does the same strings.Contains(content, "usage") check before unmarshalling, and its the same idea as the guarded log sites in #2470. came across this while looking at #2447 but its unrelated to that discussion, this path already runs today on the nixl connector.
Which issue(s) this PR fixes:
Fixes #
Release note (write
NONEif no user-facing change):