From bb503e7d57b6ffbedd32307b3ed980788ca18a07 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 29 Sep 2026 18:03:06 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Eliminate=20strings.ToLower?= =?UTF-8?q?=20allocation=20in=20content-type=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced strings.ToLower combined with strings.HasPrefix with strings.EqualFold for zero-allocation content-type matching in proxy.go. Co-authored-by: callmeradical <822382+callmeradical@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ cmd/sergeant-mcp-client/proxy.go | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 29665ec..f49d7ff 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -21,3 +21,7 @@ ## 2024-10-25 - Avoid mime.ParseMediaType for exact header matching **Learning:** Using `mime.ParseMediaType` for exact or prefix header matching in hot paths introduces unnecessary memory allocations. **Action:** When substituting with `strings.HasPrefix`, explicitly handle case-insensitivity (e.g., via `strings.ToLower`) to maintain HTTP standard compliance while preventing heavy parsing allocations. + +## 2024-05-24 - Zero-Allocation Prefix Matching +**Learning:** Using strings.ToLower for case-insensitive header matching forces unnecessary memory allocations in hot paths. +**Action:** Use len(str) >= len(prefix) && strings.EqualFold(str[:len(prefix)], prefix) to achieve zero-allocation matching. diff --git a/cmd/sergeant-mcp-client/proxy.go b/cmd/sergeant-mcp-client/proxy.go index 5d69ba6..6def068 100644 --- a/cmd/sergeant-mcp-client/proxy.go +++ b/cmd/sergeant-mcp-client/proxy.go @@ -174,8 +174,10 @@ func (p *proxy) writeResponse(resp *response) { return } - // Performance optimization: Avoid mime.ParseMediaType allocation for exact header matching - isEventStream := strings.HasPrefix(strings.ToLower(resp.contentType), "text/event-stream") + // Performance optimization: Avoid mime.ParseMediaType allocation for exact header matching. + // Use EqualFold for zero-allocation case-insensitive prefix matching instead of strings.ToLower. + const prefix = "text/event-stream" + isEventStream := len(resp.contentType) >= len(prefix) && strings.EqualFold(resp.contentType[:len(prefix)], prefix) p.outMu.Lock() defer p.outMu.Unlock() if isEventStream {