use sync.Map to store type info

This commit is contained in:
JunDao
2019-05-18 20:10:05 +08:00
parent 7953808e36
commit d3c7f45a21
3 changed files with 12 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package nbt
import (
"reflect"
"sync"
)
type typeInfo struct {
@ -9,7 +10,13 @@ type typeInfo struct {
nameToIndex map[string]int
}
var tinfoMap sync.Map
func getTypeInfo(typ reflect.Type) *typeInfo {
if ti, ok := tinfoMap.Load(typ); ok {
return ti.(*typeInfo)
}
tinfo := new(typeInfo)
tinfo.nameToIndex = make(map[string]int)
if typ.Kind() == reflect.Struct {
@ -27,7 +34,9 @@ func getTypeInfo(typ reflect.Type) *typeInfo {
}
}
}
return tinfo
ti, _ := tinfoMap.LoadOrStore(typ, tinfo)
return ti.(*typeInfo)
}
func (t *typeInfo) findIndexByName(name string) int {