forked from mirror/go-json
Merge pull request #256 from goccy/feature/improve-map-decoder-perf
Improve performance of decoding map
This commit is contained in:
commit
4f9edb7bc0
|
@ -1,6 +1,7 @@
|
||||||
package decoder
|
package decoder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/goccy/go-json/internal/errors"
|
"github.com/goccy/go-json/internal/errors"
|
||||||
|
@ -8,33 +9,49 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type mapDecoder struct {
|
type mapDecoder struct {
|
||||||
mapType *runtime.Type
|
mapType *runtime.Type
|
||||||
keyType *runtime.Type
|
keyType *runtime.Type
|
||||||
valueType *runtime.Type
|
valueType *runtime.Type
|
||||||
keyDecoder Decoder
|
stringKeyType bool
|
||||||
valueDecoder Decoder
|
keyDecoder Decoder
|
||||||
structName string
|
valueDecoder Decoder
|
||||||
fieldName string
|
structName string
|
||||||
|
fieldName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder, valueType *runtime.Type, valueDec Decoder, structName, fieldName string) *mapDecoder {
|
func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder, valueType *runtime.Type, valueDec Decoder, structName, fieldName string) *mapDecoder {
|
||||||
return &mapDecoder{
|
return &mapDecoder{
|
||||||
mapType: mapType,
|
mapType: mapType,
|
||||||
keyDecoder: keyDec,
|
keyDecoder: keyDec,
|
||||||
keyType: keyType,
|
keyType: keyType,
|
||||||
valueType: valueType,
|
stringKeyType: keyType.Kind() == reflect.String,
|
||||||
valueDecoder: valueDec,
|
valueType: valueType,
|
||||||
structName: structName,
|
valueDecoder: valueDec,
|
||||||
fieldName: fieldName,
|
structName: structName,
|
||||||
|
fieldName: fieldName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:linkname makemap reflect.makemap
|
//go:linkname makemap reflect.makemap
|
||||||
func makemap(*runtime.Type, int) unsafe.Pointer
|
func makemap(*runtime.Type, int) unsafe.Pointer
|
||||||
|
|
||||||
|
//nolint:golint
|
||||||
|
//go:linkname mapassign_faststr runtime.mapassign_faststr
|
||||||
|
//go:noescape
|
||||||
|
func mapassign_faststr(t *runtime.Type, m unsafe.Pointer, s string) unsafe.Pointer
|
||||||
|
|
||||||
//go:linkname mapassign reflect.mapassign
|
//go:linkname mapassign reflect.mapassign
|
||||||
//go:noescape
|
//go:noescape
|
||||||
func mapassign(t *runtime.Type, m unsafe.Pointer, key, val unsafe.Pointer)
|
func mapassign(t *runtime.Type, m unsafe.Pointer, k, v unsafe.Pointer)
|
||||||
|
|
||||||
|
func (d *mapDecoder) mapassign(t *runtime.Type, m, k, v unsafe.Pointer) {
|
||||||
|
if d.stringKeyType {
|
||||||
|
mapV := mapassign_faststr(d.mapType, m, *(*string)(k))
|
||||||
|
typedmemmove(d.valueType, mapV, v)
|
||||||
|
} else {
|
||||||
|
mapassign(t, m, k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
|
func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
|
||||||
depth++
|
depth++
|
||||||
|
@ -77,7 +94,7 @@ func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) erro
|
||||||
if err := d.valueDecoder.DecodeStream(s, depth, v); err != nil {
|
if err := d.valueDecoder.DecodeStream(s, depth, v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mapassign(d.mapType, mapValue, k, v)
|
d.mapassign(d.mapType, mapValue, k, v)
|
||||||
s.skipWhiteSpace()
|
s.skipWhiteSpace()
|
||||||
if s.equalChar('}') {
|
if s.equalChar('}') {
|
||||||
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
|
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
|
||||||
|
@ -141,7 +158,7 @@ func (d *mapDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.P
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
mapassign(d.mapType, mapValue, k, v)
|
d.mapassign(d.mapType, mapValue, k, v)
|
||||||
cursor = skipWhiteSpace(buf, valueCursor)
|
cursor = skipWhiteSpace(buf, valueCursor)
|
||||||
if buf[cursor] == '}' {
|
if buf[cursor] == '}' {
|
||||||
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
|
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
|
||||||
|
|
Loading…
Reference in New Issue