diff --git a/internal/gateway/fastjson.go b/internal/gateway/fastjson.go index 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 []bool var err error eachMemberIn(raw, members, func(key, val []byte) bool { - name, ok := unquoteJSON(key) + name, ok := keyName(key) if !ok { err = errSlowJSON return 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<= 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.go index a4ae840dc..127d1bab8 100644 --- a/internal/gateway/fastjson_test.go +++ b/internal/gateway/fastjson_test.go @@ -2,6 +2,7 @@ package gateway import ( "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)))