From baffcb65dd6ef4d498d72049becd397194a27064 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 18:29:48 +0800 Subject: [PATCH] provider: Host asks a saved Gemini base without building a string again 16b8c300f made Base(Gemini) GeminiAsked(p.Gemini), which runs the base through GeminiBase and url.Parse twice on every call. Provider.Host asks Base for each protocol on every look, so a Gemini provider's Host allocated 5 times a look and TestHostBuildsNoList was red on main (e4ea31321: "d: Host allocated 5 times a look"). GeminiAsked now gives back a base that is already what geminiVersioned would make (a scheme, a path of its own, no trailing slash, query, fragment, /models or surrounding space) as it is, without allocating. A bare host, a pasted endpoint and anything untrimmed still go through geminiVersioned, so TestGeminiAsked's cases are unchanged. Run (mica, -tags nogui): go test -run 'TestHostBuildsNoList|Gemini|Vertex' ./internal/provider ./internal/gateway ./internal/catalog ok; gofmt clean. Red on the old code: the same test on a clean origin/main e4ea31321 worktree, "d: Host allocated 5 times a look". --- internal/provider/gemini_api.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/provider/gemini_api.go b/internal/provider/gemini_api.go index 62aba04b8..ac82ccc8e 100644 --- a/internal/provider/gemini_api.go +++ b/internal/provider/gemini_api.go @@ -54,12 +54,28 @@ func geminiVersioned(u string) string { // on X). A base with a path of its own (/v1beta, a relay's /gemini) is // asked as typed. func GeminiAsked(u string) string { - if u == "" { - return "" + if u == "" || geminiKept(u) { + return u } return geminiVersioned(u) } +// geminiKept reports, without allocating, that geminiVersioned would give u +// back as it is: a scheme, a path of its own with no trailing slash, query, +// fragment or /models, and no space around it. Host asks Base(Gemini) on +// every look (TestHostBuildsNoList), and a saved base is already this. +func geminiKept(u string) bool { + if strings.TrimSpace(u) != u || strings.HasSuffix(u, "/") || strings.ContainsAny(u, "?#") || strings.Contains(u, "/models") { + return false + } + i := strings.Index(u, "://") + if i <= 0 { + return false + } + j := strings.IndexByte(u[i+3:], '/') + return j > 0 +} + // GeminiPath is the path under a Gemini base model is asked at: streamed as // server-sent events (alt=sse) when stream, else one JSON reply. func GeminiPath(model string, stream bool) string {