Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Replace mutex protected slice with sync.Map.
  • Loading branch information
Faye Amacker committed Oct 3, 2019
1 parent 00264a3 commit 79019c2
Showing 1 changed file with 17 additions and 33 deletions.
50 changes: 17 additions & 33 deletions structfields.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,44 +233,28 @@ type structFields struct {
}

var (
structFieldsMu sync.Mutex
cachedStructFields []structFields
cachedStructFields sync.Map
)

func getStructFields(t reflect.Type, canonical bool) fields {
flds := fields(nil)

structFieldsMu.Lock()
for i := 0; i < len(cachedStructFields); i++ {
if cachedStructFields[i].typ == t {
if canonical {
if len(cachedStructFields[i].fields) != len(cachedStructFields[i].canonicalFields) {
canonicalFields := make(fields, len(cachedStructFields[i].fields))
copy(canonicalFields, cachedStructFields[i].fields)
sort.Sort(byCanonicalRule{canonicalFields})
cachedStructFields[i].canonicalFields = canonicalFields
}
flds = cachedStructFields[i].canonicalFields
} else {
flds = cachedStructFields[i].fields
}
break
}
}
if flds == nil {
sfs := structFields{typ: t, fields: getFields(t)}
v, _ := cachedStructFields.Load(t)
if v == nil {
flds := getFields(t)

canonicalFields := make(fields, len(flds))
copy(canonicalFields, flds)
sort.Sort(byCanonicalRule{canonicalFields})

cachedStructFields.Store(t, structFields{typ: t, fields: flds, canonicalFields: canonicalFields})

if canonical {
canonicalFields := make(fields, len(sfs.fields))
copy(canonicalFields, sfs.fields)
sort.Sort(byCanonicalRule{canonicalFields})
sfs.canonicalFields = canonicalFields
flds = sfs.canonicalFields
} else {
flds = sfs.fields
return canonicalFields
}
cachedStructFields = append(cachedStructFields, sfs)
return flds
}
structFieldsMu.Unlock()

return flds
if canonical {
return v.(structFields).canonicalFields
}
return v.(structFields).fields
}

0 comments on commit 79019c2

Please sign in to comment.