diff --git a/.jules/bolt.md b/.jules/bolt.md index 52f429b..a734fa5 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -16,4 +16,7 @@ **Action:** Use `strings.NewReader(str)` directly instead of `bytes.NewReader([]byte(str))` to eliminate the allocation overhead. ## 2026-09-11 - Memoize Dynamic Library Load (`ctypes.CDLL`) **Learning:** Found that `ctypes.CDLL(None, use_errno=True)` within `pidfd_open` and `pidfd_send_signal` fallback paths in `_sgt-process-token.py` re-loaded the C library dynamically on every call, causing measurable per-call overhead, analogous to the learning about `libc_pidfd_function` from 2024-08-27. -**Action:** Substituted the redundant inline `ctypes.CDLL` loads with the memoized module-level `_LIBC_WITH_ERRNO` global to eliminate repeated linking overhead. \ No newline at end of file +**Action:** Substituted the redundant inline `ctypes.CDLL` loads with the memoized module-level `_LIBC_WITH_ERRNO` global to eliminate repeated linking overhead. +## 2026-09-20 - Avoid mime.ParseMediaType for header matching in hot paths +**Learning:** Using `mime.ParseMediaType` forces memory allocations due to string splitting and map creation, slowing down hot paths like response stream parsing. +**Action:** Use `strings.HasPrefix(strings.ToLower(strings.TrimSpace(header)), target)` instead of `mime.ParseMediaType` when checking content types to prevent allocation overhead, ensuring standard compliance through case-insensitivity. diff --git a/cmd/sergeant-mcp-client/proxy.go b/cmd/sergeant-mcp-client/proxy.go index 2fe4e65..05f9514 100644 --- a/cmd/sergeant-mcp-client/proxy.go +++ b/cmd/sergeant-mcp-client/proxy.go @@ -5,7 +5,7 @@ import ( "bytes" "fmt" "io" - "mime" + "net/http" "strings" "sync" @@ -175,10 +175,10 @@ func (p *proxy) writeResponse(resp *response) { return } - mediaType, _, _ := mime.ParseMediaType(resp.contentType) p.outMu.Lock() defer p.outMu.Unlock() - if mediaType == "text/event-stream" { + // Use strings.HasPrefix on lowercased header instead of mime.ParseMediaType to prevent allocation overhead + if strings.HasPrefix(strings.ToLower(strings.TrimSpace(resp.contentType)), "text/event-stream") { for _, data := range parseSSEData(resp.body) { fmt.Fprintf(p.stdout, "%s\n", data) }