Skip to content

Commit 919d111

Browse files
authored
Merge pull request #1021 from seanshigit/perf/plain-ascii-escape-probe
perf(openai): 加快连续 JSON 转义字符的扫描
2 parents 7f8eec5 + 51d59cc commit 919d111

2 files changed

Lines changed: 73 additions & 17 deletions

File tree

‎backend/internal/service/json_escape_probe.go‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,41 @@ import "bytes"
1818
// Escapes are consumed pairwise, so an escaped backslash followed by a literal
1919
// u (Windows paths, regexes, code samples) is plain text, not an escape.
2020
func JSONMayEscapePlainASCII(body []byte) bool {
21-
for i := 0; ; {
22-
j := bytes.IndexByte(body[i:], '\\')
23-
if j < 0 {
24-
return false
25-
}
26-
next := i + j + 1
27-
if next >= len(body) {
21+
gap := bytes.IndexByte(body, '\\')
22+
for gap >= 0 {
23+
body = body[gap+1:]
24+
if len(body) == 0 {
2825
return true
2926
}
30-
if body[next] != 'u' {
31-
i = next + 1
32-
continue
33-
}
34-
if next+5 > len(body) {
35-
return true
27+
width := 1
28+
if body[0] == 'u' {
29+
if len(body) < 5 {
30+
return true
31+
}
32+
r, ok := parseJSONHex4(body[1:5])
33+
if !ok || jsonEscapeHidesPlainASCII(r) {
34+
return true
35+
}
36+
width = 5
3637
}
37-
r, ok := parseJSONHex4(body[next+1 : next+5])
38-
if !ok || jsonEscapeHidesPlainASCII(r) {
39-
return true
38+
body = body[width:]
39+
gap = nextJSONBackslash(body, gap)
40+
}
41+
return false
42+
}
43+
44+
// Escapes in quoted code and tool output often cluster. After a nearby hit,
45+
// check the next few bytes inline before searching the rest of the body.
46+
func nextJSONBackslash(body []byte, previousGap int) int {
47+
const nearby = 16
48+
if previousGap < nearby && len(body) >= nearby {
49+
for i, c := range body[:nearby] {
50+
if c == '\\' {
51+
return i
52+
}
4053
}
41-
i = next + 5
4254
}
55+
return bytes.IndexByte(body, '\\')
4356
}
4457

4558
func parseJSONHex4(b []byte) (rune, bool) {

‎backend/internal/service/json_escape_probe_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package service
44

55
import (
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/require"
@@ -36,3 +37,45 @@ func TestJSONMayEscapePlainASCII(t *testing.T) {
3637
})
3738
}
3839
}
40+
41+
func TestJSONMayEscapePlainASCIIBackslashRuns(t *testing.T) {
42+
for count := 0; count <= 9; count++ {
43+
run := strings.Repeat("\\", count)
44+
odd := count%2 != 0
45+
for _, prefix := range []string{"", "\\u003c", "\\n" + strings.Repeat("x", 15), "\\n" + strings.Repeat("x", 16), strings.Repeat("code\\n", 100)} {
46+
require.Equal(t, odd, JSONMayEscapePlainASCII([]byte(prefix+run)), "trailing run %d", count)
47+
require.Equal(t, odd, JSONMayEscapePlainASCII([]byte(prefix+run+"u0061")), "keyword run %d", count)
48+
require.Equal(t, odd, JSONMayEscapePlainASCII([]byte(prefix+run+"u0x")), "malformed run %d", count)
49+
}
50+
}
51+
}
52+
53+
func BenchmarkJSONMayEscapePlainASCII(b *testing.B) {
54+
code := strings.Repeat("func f() { return \\\"sample\\\" }\\n", 1<<15)
55+
for _, tc := range []struct {
56+
name string
57+
body string
58+
want bool
59+
}{
60+
{"short", "{\"input\":\"ok\"}", false},
61+
{"short-newline", "{\"input\":\"a\\nb\"}", false},
62+
{"plain", strings.Repeat("function sample ", 1<<16), false},
63+
{"quoted-code", code, false},
64+
{"source-file", strings.Repeat("package app\\nfunc sample() string { return \\\"Example 测试\\\" }\\n", 1<<14), false},
65+
{"sparse-newlines", strings.Repeat(strings.Repeat("x", 126)+"\\n", 1<<13), false},
66+
{"late-keyword-escape", code + "\\u0061", true},
67+
{"html-escapes", strings.Repeat("\\u003c\\u003e", 1<<16), false},
68+
{"escaped-backslashes", strings.Repeat("\\\\", 1<<19), false},
69+
} {
70+
b.Run(tc.name, func(b *testing.B) {
71+
body := []byte(tc.body)
72+
b.ReportAllocs()
73+
b.SetBytes(int64(len(body)))
74+
for b.Loop() {
75+
if JSONMayEscapePlainASCII(body) != tc.want {
76+
b.Fatal("unexpected escape classification")
77+
}
78+
}
79+
})
80+
}
81+
}

0 commit comments

Comments
 (0)