From 4c7c003177c2306feeae7e3edf05ccbe83d0121f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 1 Oct 2026 17:54:03 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Avoid=20strings.ToLower=20a?= =?UTF-8?q?llocation=20in=20header=20match?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: callmeradical <822382+callmeradical@users.noreply.github.com> --- .jules/bolt.md | 3 +++ cmd/sergeant-mcp-client/proxy.go | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 29665ec..092524c 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -21,3 +21,6 @@ ## 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-10-25 - Avoid strings.ToLower allocation in hot paths +**Learning:** Using `strings.ToLower` for case-insensitive prefix checking forces a memory allocation, which is detrimental in hot paths like proxying HTTP responses. +**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..a0b6c9b 100644 --- a/cmd/sergeant-mcp-client/proxy.go +++ b/cmd/sergeant-mcp-client/proxy.go @@ -174,8 +174,8 @@ 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 strings.ToLower allocation for case-insensitive prefix matching + isEventStream := len(resp.contentType) >= len("text/event-stream") && strings.EqualFold(resp.contentType[:len("text/event-stream")], "text/event-stream") p.outMu.Lock() defer p.outMu.Unlock() if isEventStream {