mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
perf(gateway): skip the cache_control TTL walk when no 1h breakpoint exists
The byte-level precheck added for #8668 still runs both cache_control walks for any body containing a "thinking" literal, because the thinking-block cleanup cannot be skipped by that check alone. Claude Code enables extended thinking by default, so that is the dominant shape: the precheck pays its own scan and then falls through to the unconditional normalizeCacheControlTTLOrder(enforceCacheControlBlockLimit(body)). On the 32KB Claude Code benchmark body with thinking, this made the optimized path slightly slower than the pre-fix code (112us vs 104us). Skip the second collectCacheControlPaths pass when the body has no "1h" literal. The walk is provably a no-op there: last1h stays -1 and nothing is rewritten. Escaped bodies returned earlier, so the byte check is exact, and the block-limit deletions only remove cache_control keys, which cannot introduce a 1h breakpoint. Benchmark (Apple M5 Pro, -benchtime 2s -count 3): thinking, no 1h: 112us / 21 allocs -> 52us / 8 allocs common, no 1h: 21.6us / 0 allocs -> 21.3us / 0 allocs 1h bodies: unchanged (within noise) The benchmark gains two thinking cases so future work cannot regress this shape unnoticed. Equivalence tests compare against the verbatim legacy implementation. Fixes #8861 Refs #8668
This commit is contained in:
1 parent
d70676756f
commit
833cfcc2fb
2 files changed
+41
-3
No files matched your search
@@ -1091,7 +1091,15 @@ func enforceCacheControlLimit(body []byte) []byte {
|
||||
}
|
||||
return normalizeCacheControlTTLOrder(body)
|
||||
}
|
||||
return normalizeCacheControlTTLOrder(enforceCacheControlBlockLimit(body))
|
||||
out := enforceCacheControlBlockLimit(body)
|
||||
// Escaped bodies returned earlier, so a missing "1h" literal here is exact:
|
||||
// the TTL walk would find no 1h breakpoint and rewrite nothing, yet it still
|
||||
// costs a full collectCacheControlPaths pass over the body. Deletions above
|
||||
// only remove cache_control keys, so they cannot introduce a 1h breakpoint.
|
||||
if !bytes.Contains(body, cacheTTL1hJSONBytes) {
|
||||
return out
|
||||
}
|
||||
return normalizeCacheControlTTLOrder(out)
|
||||
}
|
||||
|
||||
// enforceCacheControlBlockLimit 强制执行 cache_control 块数量限制(最多 4 个)
|
||||
|
||||
@@ -10,8 +10,32 @@ import (
|
||||
// claudeCodeLikeBody builds a ~32KB request shaped like a Claude Code turn:
|
||||
// 20 tools, a system prompt, and 40 messages with 4 content blocks each.
|
||||
func claudeCodeLikeBody(cacheControls int, with1h bool) []byte {
|
||||
return claudeCodeLikeBodyWith(claudeCodeLikeBodyOptions{cacheControls: cacheControls, with1h: with1h})
|
||||
}
|
||||
|
||||
// claudeCodeLikeBodyWithThinking builds the same shape with extended thinking
|
||||
// enabled: the top-level thinking config plus a thinking block in the last
|
||||
// assistant turn. Either occurrence makes the byte-level precheck in
|
||||
// enforceCacheControlLimit inapplicable, so this is the shape that decides
|
||||
// whether the cache_control walks can be skipped for real Claude Code traffic.
|
||||
func claudeCodeLikeBodyWithThinking(with1h bool) []byte {
|
||||
return claudeCodeLikeBodyWith(claudeCodeLikeBodyOptions{cacheControls: 4, with1h: with1h, thinking: true})
|
||||
}
|
||||
|
||||
type claudeCodeLikeBodyOptions struct {
|
||||
cacheControls int
|
||||
with1h bool
|
||||
thinking bool
|
||||
}
|
||||
|
||||
func claudeCodeLikeBodyWith(opts claudeCodeLikeBodyOptions) []byte {
|
||||
cacheControls := opts.cacheControls
|
||||
var b strings.Builder
|
||||
_, _ = b.WriteString(`{"model":"claude-sonnet-5-5","max_tokens":32000,"stream":true,"tools":[`)
|
||||
_, _ = b.WriteString(`{"model":"claude-sonnet-5-5","max_tokens":32000,"stream":true`)
|
||||
if opts.thinking {
|
||||
_, _ = b.WriteString(`,"thinking":{"type":"enabled","budget_tokens":31999}`)
|
||||
}
|
||||
_, _ = b.WriteString(`,"tools":[`)
|
||||
for i := 0; i < 20; i++ {
|
||||
if i > 0 {
|
||||
_ = b.WriteByte(',')
|
||||
@@ -42,10 +66,14 @@ func claudeCodeLikeBody(cacheControls int, with1h bool) []byte {
|
||||
if c > 0 {
|
||||
_ = b.WriteByte(',')
|
||||
}
|
||||
if opts.thinking && m == 39 && c == 0 {
|
||||
_, _ = b.WriteString(`{"type":"thinking","thinking":"` + strings.Repeat("t", 80) + `","signature":"` + strings.Repeat("s", 64) + `"}`)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&b, `{"type":"text","text":"%s"`, strings.Repeat("x", 120))
|
||||
if m >= 38 && c == 3 && cacheControls > 0 {
|
||||
ttl := ""
|
||||
if with1h && m == 39 {
|
||||
if opts.with1h && m == 39 {
|
||||
ttl = `,"ttl":"1h"`
|
||||
}
|
||||
fmt.Fprintf(&b, `,"cache_control":{"type":"ephemeral"%s}`, ttl)
|
||||
@@ -66,6 +94,8 @@ func BenchmarkEnforceCacheControlLimit(b *testing.B) {
|
||||
}{
|
||||
{"common_4cc_no1h", claudeCodeLikeBody(4, false)},
|
||||
{"with_1h", claudeCodeLikeBody(4, true)},
|
||||
{"thinking_4cc_no1h", claudeCodeLikeBodyWithThinking(false)},
|
||||
{"thinking_4cc_1h", claudeCodeLikeBodyWithThinking(true)},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
b.Run(tc.name+"/legacy", func(b *testing.B) {
|
||||
|
||||
Reference in new issue
Block a user