From 381e7e831d97ae78983291479bf2a353f1d4b232 Mon Sep 17 00:00:00 2001 From: Minjie Shi Date: Mon, 23 Nov 2026 04:18:07 +0800 Subject: [PATCH 1/2] perf(openai): inspect OAuth input arrays without copying the history --- .../service/openai_gateway_request_body.go | 3 +- .../openai_passthrough_normalization_test.go | 30 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/backend/internal/service/openai_gateway_request_body.go b/backend/internal/service/openai_gateway_request_body.go index 0e3454935946..a439b688ae3c 100644 --- a/backend/internal/service/openai_gateway_request_body.go +++ b/backend/internal/service/openai_gateway_request_body.go @@ -1723,7 +1723,8 @@ func normalizeOpenAIPassthroughOAuthBody(body []byte, compact bool) ([]byte, boo changed = true } - if inputResult := gjson.GetBytes(normalized, "input"); inputResult.Exists() { + // Inspect the input in place so an already-normalized history array is never copied here. + if inputResult := gjson.Get(openAIWSPayloadStringView(normalized), "input"); inputResult.Exists() { switch { case inputResult.Type == gjson.String: text := inputResult.String() diff --git a/backend/internal/service/openai_passthrough_normalization_test.go b/backend/internal/service/openai_passthrough_normalization_test.go index f2071443ad48..eb653e4edcb4 100644 --- a/backend/internal/service/openai_passthrough_normalization_test.go +++ b/backend/internal/service/openai_passthrough_normalization_test.go @@ -1,6 +1,7 @@ package service import ( + "strings" "testing" "github.com/stretchr/testify/require" @@ -306,11 +307,14 @@ func TestNormalizeOpenAIPassthroughOAuthBody_CompactRemovesUnsupportedUser(t *te } func TestNormalizeOpenAIPassthroughOAuthBody_StringInputWrappedAsArray(t *testing.T) { - body := []byte(`{"model":"gpt-5.4","input":"hello world"}`) + const original = `{"model":"gpt-5.4","input":"hello world"}` + body := []byte(original) normalized, changed, err := normalizeOpenAIPassthroughOAuthBody(body, false) require.NoError(t, err) require.True(t, changed) + require.Equal(t, original, string(body)) + clear(body) // The rewritten request must survive reuse of the original buffer. input := gjson.GetBytes(normalized, "input") require.True(t, input.IsArray(), "string input should be converted to array") @@ -334,11 +338,14 @@ func TestNormalizeOpenAIPassthroughOAuthBody_EmptyStringInputWrappedAsEmptyArray } func TestNormalizeOpenAIPassthroughOAuthBody_ObjectInputWrappedAsArray(t *testing.T) { - body := []byte(`{"model":"gpt-5.4","input":{"type":"message","role":"user","content":"hi"}}`) + const original = `{"model":"gpt-5.4","input":{"type":"message","role":"user","content":"hi"}}` + body := []byte(original) normalized, changed, err := normalizeOpenAIPassthroughOAuthBody(body, false) require.NoError(t, err) require.True(t, changed) + require.Equal(t, original, string(body)) + clear(body) input := gjson.GetBytes(normalized, "input") require.True(t, input.IsArray(), "object input should be wrapped in array") @@ -359,6 +366,25 @@ func TestNormalizeOpenAIPassthroughOAuthBody_ArrayInputUnchanged(t *testing.T) { require.Equal(t, "message", input.Array()[0].Get("type").String()) } +func BenchmarkNormalizeOpenAIPassthroughOAuthArrayInput(b *testing.B) { + for _, tc := range []struct { + name string + size int + }{{"4KiB", 4 << 10}, {"1MiB", 1 << 20}} { + b.Run(tc.name, func(b *testing.B) { + body := []byte(`{"model":"gpt-5.5","store":false,"stream":true,"input":[{"role":"user","content":"` + + strings.Repeat("x", tc.size) + `"}]}`) + b.SetBytes(int64(len(body))) + b.ReportAllocs() + for b.Loop() { + if _, changed, err := normalizeOpenAIPassthroughOAuthBody(body, false); changed || err != nil { + b.Fatalf("changed=%v err=%v", changed, err) + } + } + }) + } +} + func TestDetectOpenAIPassthroughInstructionsRejectReason(t *testing.T) { for _, tt := range []struct { name string From db6c4b929de459025852a05c78f9f10e04796e61 Mon Sep 17 00:00:00 2001 From: Minjie Shi Date: Mon, 23 Nov 2026 04:30:23 +0800 Subject: [PATCH 2/2] perf(openai): avoid copying OAuth input before orphan cleanup --- backend/internal/service/openai_gateway_request_body.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/internal/service/openai_gateway_request_body.go b/backend/internal/service/openai_gateway_request_body.go index a439b688ae3c..7409b3313c91 100644 --- a/backend/internal/service/openai_gateway_request_body.go +++ b/backend/internal/service/openai_gateway_request_body.go @@ -1619,7 +1619,7 @@ func normalizeOpenAIResponsesCompatibilityBodyWithOptions(body []byte, account * } } needsOrphanCleanup := account != nil && account.IsOpenAIOAuthLike() && - gjson.GetBytes(normalized, "input").IsArray() && openAIResponsesMayNeedOrphanCleanup(normalized) + gjson.Get(openAIWSPayloadStringView(normalized), "input").IsArray() && openAIResponsesMayNeedOrphanCleanup(normalized) if needsOrphanCleanup || openAIResponsesInputMayNeedTruncation(normalized) { var reqBody map[string]any if err := decodeOpenAIJSONUseNumber(normalized, &reqBody); err != nil {