use sync.Map to store type info
This commit is contained in:
@ -27,7 +27,6 @@ type Decoder struct {
|
|||||||
io.ByteReader
|
io.ByteReader
|
||||||
io.Reader
|
io.Reader
|
||||||
}
|
}
|
||||||
nameless bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(r io.Reader) *Decoder {
|
func NewDecoder(r io.Reader) *Decoder {
|
||||||
|
@ -157,6 +157,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte, tagName string) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case TagCompound:
|
case TagCompound:
|
||||||
if vk := val.Kind(); vk != reflect.Struct {
|
if vk := val.Kind(); vk != reflect.Struct {
|
||||||
return errors.New("cannot parse TagCompound as " + vk.String())
|
return errors.New("cannot parse TagCompound as " + vk.String())
|
||||||
@ -252,7 +253,7 @@ func (d *Decoder) readTag() (tagType byte, tagName string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tagType != TagEnd && !d.nameless { //Read Tag
|
if tagType != TagEnd { //Read Tag
|
||||||
tagName, err = d.readString()
|
tagName, err = d.readString()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -2,6 +2,7 @@ package nbt
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type typeInfo struct {
|
type typeInfo struct {
|
||||||
@ -9,7 +10,13 @@ type typeInfo struct {
|
|||||||
nameToIndex map[string]int
|
nameToIndex map[string]int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tinfoMap sync.Map
|
||||||
|
|
||||||
func getTypeInfo(typ reflect.Type) *typeInfo {
|
func getTypeInfo(typ reflect.Type) *typeInfo {
|
||||||
|
if ti, ok := tinfoMap.Load(typ); ok {
|
||||||
|
return ti.(*typeInfo)
|
||||||
|
}
|
||||||
|
|
||||||
tinfo := new(typeInfo)
|
tinfo := new(typeInfo)
|
||||||
tinfo.nameToIndex = make(map[string]int)
|
tinfo.nameToIndex = make(map[string]int)
|
||||||
if typ.Kind() == reflect.Struct {
|
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 {
|
func (t *typeInfo) findIndexByName(name string) int {
|
||||||
|
Reference in New Issue
Block a user