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".
This commit is contained in:
yetone
2026-11-13 18:50:25 +08:00
parent 456b5c6c74
commit baffcb65dd
+18 -2
View File
@@ -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 {