Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a decoded struct's keys are read without a string made for each (#3383)
yetone2d21034d817b
diff --git a/internal/gateway/fastjson.go b/internal/gateway/fastjson.goindex 1b0c6e5d1..370807817 100644--- a/internal/gateway/fastjson.go+++ b/internal/gateway/fastjson.go@@ -348,26 +348,35 @@ func fillJSONIn(raw []byte, v reflect.Value, members []memberSpan) error {if f.err != nil {return f.err}- var seen []bool+ // seen is a bit a field, in few or in many when a struct has more+ // than 64: a []bool made for each struct decoded was one of a+ // request's commonest allocations+ var few uint64+ var many []boolvar err erroreachMemberIn(raw, members, func(key, val []byte) bool {- name, ok := unquoteJSON(key)+ name, ok := keyName(key)if !ok {err = errSlowJSONreturn false}- i := f.field([]byte(name))+ i := f.field(name)if i < 0 {return true}- if seen == nil {- seen = make([]bool, len(f.names))+ twice := false+ if len(f.names) <= 64 {+ twice, few = few&(1<<i) != 0, few|1<<i+ } else {+ if many == nil {+ many = make([]bool, len(f.names))+ }+ twice, many[i] = many[i], true}- if seen[i] {+ if twice {err = errSlowJSON // a key twice: encoding/json merges themreturn false}- seen[i] = trueerr = fillJSON(val, v.Field(f.index[i]))return err == nil})@@ -630,6 +639,23 @@ func skipJSONValue(data []byte, i int) int {return i}+// keyName is a member's key as unquoteJSON reads it, without making a+// string of it: a plain ASCII key, the kind nearly every key is, is the+// bytes between its quotes.+func keyName(key []byte) ([]byte, bool) {+ if len(key) < 2 || key[0] != '"' || key[len(key)-1] != '"' {+ return nil, false+ }+ k := key[1 : len(key)-1]+ for _, c := range k {+ if c == '\\' || c == '"' || c < ' ' || c >= utf8.RuneSelf {+ name, ok := unquoteJSON(key)+ return []byte(name), ok+ }+ }+ return k, true+}+// unquoteJSON is the string a valid JSON string literal holds, as// encoding/json decodes it (unquoteBytes): invalid UTF-8 and lone// surrogates become U+FFFD.diff --git a/internal/gateway/fastjson_test.go b/internal/gateway/fastjson_test.goindex a4ae840dc..127d1bab8 100644--- a/internal/gateway/fastjson_test.go+++ b/internal/gateway/fastjson_test.go@@ -2,6 +2,7 @@ package gatewayimport ("encoding/json"+ "fmt""math/rand/v2""reflect""strings"@@ -93,6 +94,7 @@ var fjSeeds = []string{`"a long string with no escapes at all, past eight bytes"`, `"sevené then \"quoted\" \\ and \t past eight"`,"\"a control byte \x1f deep in a long string\"", "\"\x7f\x80 high bytes \xc3\xa9 \xe2\x82\xac in a long string\"",`{"strs":["a",null,"b"]}`, `{"strs":"notarray"}`, `{"metadata":null}`, `{"metadata":[]}`,+ `{"modèl":"a non-ASCII key","Model":"fullwidth","MODEL":"b","Model":"c"}`, "{\"mo\x7fdel\":\"d\",\"mo\x1fdel\":1}",}func fjCompare(t *testing.T, data []byte) {@@ -154,6 +156,34 @@ func TestDecodeJSONIsUnmarshal(t *testing.T) {}}+// A struct marks the fields it has seen in a word's bits, or in a slice+// when it has more than 64: a key twice is refused the same in either.+func TestDecodeJSONKeyTwice(t *testing.T) {+ var fields []reflect.StructField+ for i := range 70 {+ fields = append(fields, reflect.StructField{Name: fmt.Sprintf("F%d", i), Type: reflect.TypeFor[string](), Tag: reflect.StructTag(fmt.Sprintf(`json:"f%d"`, i))})+ }+ wide := reflect.StructOf(fields)+ for _, s := range []string{`{"f0":"a","f69":"b","f65":"c"}`, `{"f66":"a","f66":"b"}`, `{"f3":"a","F3":"b"}`, `{"f69":1}`} {+ got, want := reflect.New(wide).Interface(), reflect.New(wide).Interface()+ gerr, werr := decodeJSON([]byte(s), got), json.Unmarshal([]byte(s), want)+ if (gerr == nil) != (werr == nil) || !reflect.DeepEqual(got, want) {+ t.Fatalf("%s: decodeJSON %v %v, json.Unmarshal %v %v", s, got, gerr, want, werr)+ }+ }+ // the fast path's own refusal, which encoding/json then decodes+ for _, s := range []string{`{"f66":"a","f66":"b"}`, `{"f3":"a","F3":"b"}`} {+ if err := fillJSON([]byte(s), reflect.New(wide).Elem()); err != errSlowJSON {+ t.Fatalf("%s: fillJSON %v, want errSlowJSON", s, err)+ }+ }+ for _, s := range []string{`{"model":"a","model":"b"}`, `{"Model":"a","MODEL":"b"}`} {+ if err := fillJSON([]byte(s), reflect.ValueOf(new(fjBody)).Elem()); err != errSlowJSON {+ t.Fatalf("%s: fillJSON %v, want errSlowJSON", s, err)+ }+ }+}+func TestDecodeJSONDepth(t *testing.T) {for _, n := range []int{9999, 10000, 10001} {fjCompare(t, []byte(strings.Repeat("[", n)+strings.Repeat("]", n)))