Skip to content

Commit 657600e

Browse files
json: remove cycle detection from constructStructType (ignore-whitespace)
This responsibility was moved to constructCodec.
1 parent 5dc9e08 commit 657600e

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

json/codec.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -516,42 +516,35 @@ func constructStructCodec(t reflect.Type, seen codecCache, canAddr bool) codec {
516516
}
517517

518518
func constructStructType(t reflect.Type, seen codecCache, canAddr bool) *structType {
519-
// Used for preventing infinite recursion on types that have pointers to
520-
// themselves.
521-
st := seen[t]
522-
523-
if st == nil {
524-
st = &structType{
525-
fields: make([]structField, 0, t.NumField()),
526-
fieldsIndex: make(map[string]*structField),
527-
ficaseIndex: make(map[string]*structField),
528-
typ: t,
529-
}
530-
531-
seen[t] = st
532-
st.fields = appendStructFields(st.fields, t, 0, seen, canAddr)
533-
534-
for i := range st.fields {
535-
f := &st.fields[i]
536-
s := strings.ToLower(f.name)
537-
st.fieldsIndex[f.name] = f
538-
// When there is ambiguity because multiple fields have the same
539-
// case-insensitive representation, the first field must win.
540-
if _, exists := st.ficaseIndex[s]; !exists {
541-
st.ficaseIndex[s] = f
542-
}
519+
st := &structType{
520+
fields: make([]structField, 0, t.NumField()),
521+
fieldsIndex: make(map[string]*structField),
522+
ficaseIndex: make(map[string]*structField),
523+
typ: t,
524+
}
525+
526+
st.fields = appendStructFields(st.fields, t, 0, seen, canAddr)
527+
528+
for i := range st.fields {
529+
f := &st.fields[i]
530+
s := strings.ToLower(f.name)
531+
st.fieldsIndex[f.name] = f
532+
// When there is ambiguity because multiple fields have the same
533+
// case-insensitive representation, the first field must win.
534+
if _, exists := st.ficaseIndex[s]; !exists {
535+
st.ficaseIndex[s] = f
543536
}
537+
}
544538

545-
// At a certain point the linear scan provided by keyset is less
546-
// efficient than a map. The 32 was chosen based on benchmarks in the
547-
// segmentio/asm repo run with an Intel Kaby Lake processor and go1.17.
548-
if len(st.fields) <= 32 {
549-
keys := make([][]byte, len(st.fields))
550-
for i, f := range st.fields {
551-
keys[i] = []byte(f.name)
552-
}
553-
st.keyset = keyset.New(keys)
539+
// At a certain point the linear scan provided by keyset is less
540+
// efficient than a map. The 32 was chosen based on benchmarks in the
541+
// segmentio/asm repo run with an Intel Kaby Lake processor and go1.17.
542+
if len(st.fields) <= 32 {
543+
keys := make([][]byte, len(st.fields))
544+
for i, f := range st.fields {
545+
keys[i] = []byte(f.name)
554546
}
547+
st.keyset = keyset.New(keys)
555548
}
556549

557550
return st

0 commit comments

Comments
 (0)