2016-02-22 14:44:12 -05:00
|
|
|
// +build !notfastpath
|
|
|
|
|
|
|
|
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license found in the LICENSE file.
|
|
|
|
|
2019-10-14 10:21:52 -04:00
|
|
|
// Code generated from fast-path.go.tmpl - DO NOT EDIT.
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
package codec
|
|
|
|
|
|
|
|
// Fast path functions try to create a fast path encode or decode implementation
|
|
|
|
// for common maps and slices.
|
|
|
|
//
|
|
|
|
// We define the functions and register then in this single file
|
|
|
|
// so as not to pollute the encode.go and decode.go, and create a dependency in there.
|
|
|
|
// This file can be omitted without causing a build failure.
|
|
|
|
//
|
|
|
|
// The advantage of fast paths is:
|
2019-10-14 10:21:52 -04:00
|
|
|
// - Many calls bypass reflection altogether
|
2016-02-22 14:44:12 -05:00
|
|
|
//
|
|
|
|
// Currently support
|
2019-10-14 10:21:52 -04:00
|
|
|
// - slice of all builtin types,
|
|
|
|
// - map of all builtin types to string or interface value
|
|
|
|
// - symmetrical maps of all builtin types (e.g. str-str, uint8-uint8)
|
2016-02-22 14:44:12 -05:00
|
|
|
// This should provide adequate "typical" implementations.
|
|
|
|
//
|
|
|
|
// Note that fast track decode functions must handle values for which an address cannot be obtained.
|
|
|
|
// For example:
|
2019-10-14 10:21:52 -04:00
|
|
|
// m2 := map[string]int{}
|
|
|
|
// p2 := []interface{}{m2}
|
|
|
|
// // decoding into p2 will bomb if fast track functions do not treat like unaddressable.
|
2016-02-22 14:44:12 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2019-10-14 10:21:52 -04:00
|
|
|
const fastpathEnabled = true
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
type fastpathT struct {}
|
|
|
|
|
|
|
|
var fastpathTV fastpathT
|
|
|
|
|
|
|
|
type fastpathE struct {
|
|
|
|
rtid uintptr
|
|
|
|
rt reflect.Type
|
2019-10-14 10:21:52 -04:00
|
|
|
encfn func(*Encoder, *codecFnInfo, reflect.Value)
|
|
|
|
decfn func(*Decoder, *codecFnInfo, reflect.Value)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type fastpathA [{{ .FastpathLen }}]fastpathE
|
|
|
|
|
|
|
|
func (x *fastpathA) index(rtid uintptr) int {
|
|
|
|
// use binary search to grab the index (adapted from sort/search.go)
|
|
|
|
h, i, j := 0, 0, {{ .FastpathLen }} // len(x)
|
|
|
|
for i < j {
|
|
|
|
h = i + (j-i)/2
|
|
|
|
if x[h].rtid < rtid {
|
|
|
|
i = h + 1
|
|
|
|
} else {
|
|
|
|
j = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i < {{ .FastpathLen }} && x[i].rtid == rtid {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
type fastpathAslice []fastpathE
|
|
|
|
|
|
|
|
func (x fastpathAslice) Len() int { return len(x) }
|
|
|
|
func (x fastpathAslice) Less(i, j int) bool { return x[i].rtid < x[j].rtid }
|
|
|
|
func (x fastpathAslice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
|
|
|
|
|
|
var fastpathAV fastpathA
|
|
|
|
|
|
|
|
// due to possible initialization loop error, make fastpath in an init()
|
|
|
|
func init() {
|
|
|
|
i := 0
|
2019-10-14 10:21:52 -04:00
|
|
|
fn := func(v interface{},
|
|
|
|
fe func(*Encoder, *codecFnInfo, reflect.Value),
|
|
|
|
fd func(*Decoder, *codecFnInfo, reflect.Value)) (f fastpathE) {
|
2016-02-22 14:44:12 -05:00
|
|
|
xrt := reflect.TypeOf(v)
|
2019-10-14 10:21:52 -04:00
|
|
|
xptr := rt2id(xrt)
|
2016-02-22 14:44:12 -05:00
|
|
|
fastpathAV[i] = fastpathE{xptr, xrt, fe, fd}
|
|
|
|
i++
|
|
|
|
return
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
{{/* do not register []uint8 in fast-path */}}
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if not .MapKey }}{{if ne .Elem "uint8"}}
|
|
|
|
fn([]{{ .Elem }}(nil), (*Encoder).{{ .MethodNamePfx "fastpathEnc" false }}R, (*Decoder).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}{{end}}
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if .MapKey }}
|
2019-10-14 10:21:52 -04:00
|
|
|
fn(map[{{ .MapKey }}]{{ .Elem }}(nil), (*Encoder).{{ .MethodNamePfx "fastpathEnc" false }}R, (*Decoder).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
sort.Sort(fastpathAslice(fastpathAV[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- encode
|
|
|
|
|
|
|
|
// -- -- fast path type switch
|
|
|
|
func fastpathEncodeTypeSwitch(iv interface{}, e *Encoder) bool {
|
|
|
|
switch v := iv.(type) {
|
2019-10-14 10:21:52 -04:00
|
|
|
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if not .MapKey }}{{if ne .Elem "uint8"}}
|
|
|
|
case []{{ .Elem }}:
|
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
|
|
|
|
case *[]{{ .Elem }}:
|
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e){{/*
|
|
|
|
*/}}{{end}}{{end}}{{end}}{{end}}
|
|
|
|
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if .MapKey }}
|
|
|
|
case map[{{ .MapKey }}]{{ .Elem }}:
|
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
|
|
|
|
case *map[{{ .MapKey }}]{{ .Elem }}:
|
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e){{/*
|
|
|
|
*/}}{{end}}{{end}}{{end}}
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
default:
|
2019-10-14 10:21:52 -04:00
|
|
|
_ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
|
2016-02-22 14:44:12 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:21:52 -04:00
|
|
|
{{/*
|
|
|
|
**** removing this block, as they are never called directly ****
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**** removing this block, as they are never called directly ****
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
func fastpathEncodeTypeSwitchSlice(iv interface{}, e *Encoder) bool {
|
|
|
|
switch v := iv.(type) {
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
|
|
|
|
case []{{ .Elem }}:
|
2019-10-14 10:21:52 -04:00
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
|
2016-02-22 14:44:12 -05:00
|
|
|
case *[]{{ .Elem }}:
|
2019-10-14 10:21:52 -04:00
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e)
|
2016-02-22 14:44:12 -05:00
|
|
|
{{end}}{{end}}{{end}}
|
|
|
|
default:
|
2019-10-14 10:21:52 -04:00
|
|
|
_ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
|
2016-02-22 14:44:12 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func fastpathEncodeTypeSwitchMap(iv interface{}, e *Encoder) bool {
|
|
|
|
switch v := iv.(type) {
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if .MapKey }}
|
|
|
|
case map[{{ .MapKey }}]{{ .Elem }}:
|
2019-10-14 10:21:52 -04:00
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
|
2016-02-22 14:44:12 -05:00
|
|
|
case *map[{{ .MapKey }}]{{ .Elem }}:
|
2019-10-14 10:21:52 -04:00
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e)
|
2016-02-22 14:44:12 -05:00
|
|
|
{{end}}{{end}}{{end}}
|
|
|
|
default:
|
2019-10-14 10:21:52 -04:00
|
|
|
_ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
|
2016-02-22 14:44:12 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:21:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
**** removing this block, as they are never called directly ****
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**** removing this block, as they are never called directly ****
|
|
|
|
*/}}
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
// -- -- fast path functions
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (e *Encoder) {{ .MethodNamePfx "fastpathEnc" false }}R(f *codecFnInfo, rv reflect.Value) {
|
2016-02-22 14:44:12 -05:00
|
|
|
if f.ti.mbs {
|
2019-10-14 10:21:52 -04:00
|
|
|
fastpathTV.{{ .MethodNamePfx "EncAsMap" false }}V(rv2i(rv).([]{{ .Elem }}), e)
|
2016-02-22 14:44:12 -05:00
|
|
|
} else {
|
2019-10-14 10:21:52 -04:00
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv2i(rv).([]{{ .Elem }}), e)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v []{{ .Elem }}, e *Encoder) {
|
|
|
|
if v == nil { e.e.EncodeNil(); return }
|
|
|
|
ee, esep := e.e, e.hh.hasElemSeparators()
|
|
|
|
ee.WriteArrayStart(len(v))
|
|
|
|
if esep {
|
|
|
|
for _, v2 := range v {
|
|
|
|
ee.WriteArrayElem()
|
|
|
|
{{ encmd .Elem "v2"}}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, v2 := range v {
|
|
|
|
{{ encmd .Elem "v2"}}
|
|
|
|
}
|
|
|
|
} {{/*
|
2016-02-22 14:44:12 -05:00
|
|
|
for _, v2 := range v {
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep { ee.WriteArrayElem() }
|
2016-02-22 14:44:12 -05:00
|
|
|
{{ encmd .Elem "v2"}}
|
2019-10-14 10:21:52 -04:00
|
|
|
} */}}
|
|
|
|
ee.WriteArrayEnd()
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (_ fastpathT) {{ .MethodNamePfx "EncAsMap" false }}V(v []{{ .Elem }}, e *Encoder) {
|
|
|
|
ee, esep := e.e, e.hh.hasElemSeparators()
|
2016-02-22 14:44:12 -05:00
|
|
|
if len(v)%2 == 1 {
|
|
|
|
e.errorf("mapBySlice requires even slice length, but got %v", len(v))
|
|
|
|
return
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
ee.WriteMapStart(len(v) / 2)
|
|
|
|
if esep {
|
|
|
|
for j, v2 := range v {
|
|
|
|
if j%2 == 0 {
|
|
|
|
ee.WriteMapElemKey()
|
|
|
|
} else {
|
|
|
|
ee.WriteMapElemValue()
|
|
|
|
}
|
|
|
|
{{ encmd .Elem "v2"}}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, v2 := range v {
|
|
|
|
{{ encmd .Elem "v2"}}
|
|
|
|
}
|
|
|
|
} {{/*
|
2016-02-22 14:44:12 -05:00
|
|
|
for j, v2 := range v {
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep {
|
2016-02-22 14:44:12 -05:00
|
|
|
if j%2 == 0 {
|
2019-10-14 10:21:52 -04:00
|
|
|
ee.WriteMapElemKey()
|
2016-02-22 14:44:12 -05:00
|
|
|
} else {
|
2019-10-14 10:21:52 -04:00
|
|
|
ee.WriteMapElemValue()
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{{ encmd .Elem "v2"}}
|
2019-10-14 10:21:52 -04:00
|
|
|
} */}}
|
|
|
|
ee.WriteMapEnd()
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
{{end}}{{end}}{{end}}
|
|
|
|
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if .MapKey }}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (e *Encoder) {{ .MethodNamePfx "fastpathEnc" false }}R(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv2i(rv).(map[{{ .MapKey }}]{{ .Elem }}), e)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, e *Encoder) {
|
|
|
|
if v == nil { e.e.EncodeNil(); return }
|
|
|
|
ee, esep := e.e, e.hh.hasElemSeparators()
|
|
|
|
ee.WriteMapStart(len(v))
|
|
|
|
if e.h.Canonical {
|
2016-02-22 14:44:12 -05:00
|
|
|
{{if eq .MapKey "interface{}"}}{{/* out of band
|
|
|
|
*/}}var mksv []byte = make([]byte, 0, len(v)*16) // temporary byte slice for the encoding
|
|
|
|
e2 := NewEncoderBytes(&mksv, e.hh)
|
|
|
|
v2 := make([]bytesI, len(v))
|
|
|
|
var i, l int
|
|
|
|
var vp *bytesI {{/* put loop variables outside. seems currently needed for better perf */}}
|
|
|
|
for k2, _ := range v {
|
|
|
|
l = len(mksv)
|
|
|
|
e2.MustEncode(k2)
|
|
|
|
vp = &v2[i]
|
|
|
|
vp.v = mksv[l:]
|
|
|
|
vp.i = k2
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sort.Sort(bytesISlice(v2))
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep {
|
|
|
|
for j := range v2 {
|
|
|
|
ee.WriteMapElemKey()
|
|
|
|
e.asis(v2[j].v)
|
|
|
|
ee.WriteMapElemValue()
|
|
|
|
e.encode(v[v2[j].i])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for j := range v2 {
|
|
|
|
e.asis(v2[j].v)
|
|
|
|
e.encode(v[v2[j].i])
|
|
|
|
}
|
|
|
|
} {{/*
|
2016-02-22 14:44:12 -05:00
|
|
|
for j := range v2 {
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep { ee.WriteMapElemKey() }
|
2016-02-22 14:44:12 -05:00
|
|
|
e.asis(v2[j].v)
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep { ee.WriteMapElemValue() }
|
2016-02-22 14:44:12 -05:00
|
|
|
e.encode(v[v2[j].i])
|
2019-10-14 10:21:52 -04:00
|
|
|
} */}} {{else}}{{ $x := sorttype .MapKey true}}v2 := make([]{{ $x }}, len(v))
|
2016-02-22 14:44:12 -05:00
|
|
|
var i int
|
|
|
|
for k, _ := range v {
|
|
|
|
v2[i] = {{ $x }}(k)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sort.Sort({{ sorttype .MapKey false}}(v2))
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep {
|
|
|
|
for _, k2 := range v2 {
|
|
|
|
ee.WriteMapElemKey()
|
|
|
|
{{if eq .MapKey "string"}}ee.EncodeString(cUTF8, k2){{else}}{{ $y := printf "%s(k2)" .MapKey }}{{ encmd .MapKey $y }}{{end}}
|
|
|
|
ee.WriteMapElemValue()
|
|
|
|
{{ $y := printf "v[%s(k2)]" .MapKey }}{{ encmd .Elem $y }}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, k2 := range v2 {
|
|
|
|
{{if eq .MapKey "string"}}ee.EncodeString(cUTF8, k2){{else}}{{ $y := printf "%s(k2)" .MapKey }}{{ encmd .MapKey $y }}{{end}}
|
|
|
|
{{ $y := printf "v[%s(k2)]" .MapKey }}{{ encmd .Elem $y }}
|
|
|
|
}
|
|
|
|
} {{/*
|
2016-02-22 14:44:12 -05:00
|
|
|
for _, k2 := range v2 {
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep { ee.WriteMapElemKey() }
|
|
|
|
{{if eq .MapKey "string"}}ee.EncodeString(cUTF8, k2){{else}}{{ $y := printf "%s(k2)" .MapKey }}{{ encmd .MapKey $y }}{{end}}
|
|
|
|
if esep { ee.WriteMapElemValue() }
|
2016-02-22 14:44:12 -05:00
|
|
|
{{ $y := printf "v[%s(k2)]" .MapKey }}{{ encmd .Elem $y }}
|
2019-10-14 10:21:52 -04:00
|
|
|
} */}} {{end}}
|
2016-02-22 14:44:12 -05:00
|
|
|
} else {
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep {
|
|
|
|
for k2, v2 := range v {
|
|
|
|
ee.WriteMapElemKey()
|
|
|
|
{{if eq .MapKey "string"}}ee.EncodeString(cUTF8, k2){{else}}{{ encmd .MapKey "k2"}}{{end}}
|
|
|
|
ee.WriteMapElemValue()
|
|
|
|
{{ encmd .Elem "v2"}}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k2, v2 := range v {
|
|
|
|
{{if eq .MapKey "string"}}ee.EncodeString(cUTF8, k2){{else}}{{ encmd .MapKey "k2"}}{{end}}
|
|
|
|
{{ encmd .Elem "v2"}}
|
|
|
|
}
|
|
|
|
} {{/*
|
2016-02-22 14:44:12 -05:00
|
|
|
for k2, v2 := range v {
|
2019-10-14 10:21:52 -04:00
|
|
|
if esep { ee.WriteMapElemKey() }
|
|
|
|
{{if eq .MapKey "string"}}ee.EncodeString(cUTF8, k2){{else}}{{ encmd .MapKey "k2"}}{{end}}
|
|
|
|
if esep { ee.WriteMapElemValue() }
|
2016-02-22 14:44:12 -05:00
|
|
|
{{ encmd .Elem "v2"}}
|
2019-10-14 10:21:52 -04:00
|
|
|
} */}}
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
ee.WriteMapEnd()
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
{{end}}{{end}}{{end}}
|
|
|
|
|
|
|
|
// -- decode
|
|
|
|
|
|
|
|
// -- -- fast path type switch
|
|
|
|
func fastpathDecodeTypeSwitch(iv interface{}, d *Decoder) bool {
|
2019-10-14 10:21:52 -04:00
|
|
|
var changed bool
|
|
|
|
switch v := iv.(type) {
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if not .MapKey }}{{if ne .Elem "uint8"}}
|
|
|
|
case []{{ .Elem }}:
|
|
|
|
var v2 []{{ .Elem }}
|
|
|
|
v2, changed = fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, false, d)
|
|
|
|
if changed && len(v) > 0 && len(v2) > 0 && !(len(v2) == len(v) && &v2[0] == &v[0]) {
|
|
|
|
copy(v, v2)
|
|
|
|
}
|
|
|
|
case *[]{{ .Elem }}:
|
|
|
|
var v2 []{{ .Elem }}
|
|
|
|
v2, changed = fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*v, true, d)
|
|
|
|
if changed {
|
|
|
|
*v = v2
|
|
|
|
}{{/*
|
|
|
|
*/}}{{end}}{{end}}{{end}}{{end}}
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if .MapKey }}{{/*
|
|
|
|
// maps only change if nil, and in that case, there's no point copying
|
|
|
|
*/}}
|
|
|
|
case map[{{ .MapKey }}]{{ .Elem }}:
|
|
|
|
fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, false, d)
|
|
|
|
case *map[{{ .MapKey }}]{{ .Elem }}:
|
|
|
|
var v2 map[{{ .MapKey }}]{{ .Elem }}
|
|
|
|
v2, changed = fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*v, true, d)
|
|
|
|
if changed {
|
|
|
|
*v = v2
|
|
|
|
}{{/*
|
|
|
|
*/}}{{end}}{{end}}{{end}}
|
|
|
|
default:
|
|
|
|
_ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
|
2016-02-22 14:44:12 -05:00
|
|
|
return false
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func fastpathDecodeSetZeroTypeSwitch(iv interface{}) bool {
|
2016-02-22 14:44:12 -05:00
|
|
|
switch v := iv.(type) {
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
|
2019-10-14 10:21:52 -04:00
|
|
|
case *[]{{ .Elem }}:
|
|
|
|
*v = nil {{/*
|
|
|
|
*/}}{{end}}{{end}}{{end}}
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if .MapKey }}
|
|
|
|
case *map[{{ .MapKey }}]{{ .Elem }}:
|
|
|
|
*v = nil {{/*
|
|
|
|
*/}}{{end}}{{end}}{{end}}
|
2016-02-22 14:44:12 -05:00
|
|
|
default:
|
2019-10-14 10:21:52 -04:00
|
|
|
_ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
|
2016-02-22 14:44:12 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- -- fast path functions
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
|
|
|
|
{{/*
|
|
|
|
Slices can change if they
|
|
|
|
- did not come from an array
|
|
|
|
- are addressable (from a ptr)
|
|
|
|
- are settable (e.g. contained in an interface{})
|
|
|
|
*/}}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (d *Decoder) {{ .MethodNamePfx "fastpathDec" false }}R(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
if array := f.seq == seqTypeArray; !array && rv.Kind() == reflect.Ptr {
|
|
|
|
vp := rv2i(rv).(*[]{{ .Elem }})
|
|
|
|
v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, !array, d)
|
|
|
|
if changed { *vp = v }
|
2016-02-22 14:44:12 -05:00
|
|
|
} else {
|
2019-10-14 10:21:52 -04:00
|
|
|
v := rv2i(rv).([]{{ .Elem }})
|
|
|
|
v2, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, !array, d)
|
|
|
|
if changed && len(v) > 0 && len(v2) > 0 && !(len(v2) == len(v) && &v2[0] == &v[0]) {
|
|
|
|
copy(v, v2)
|
|
|
|
}
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *[]{{ .Elem }}, d *Decoder) {
|
|
|
|
v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d)
|
|
|
|
if changed { *vp = v }
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v []{{ .Elem }}, canChange bool, d *Decoder) (_ []{{ .Elem }}, changed bool) {
|
|
|
|
dd := d.d{{/*
|
|
|
|
// if dd.isContainerType(valueTypeNil) { dd.TryDecodeAsNil()
|
|
|
|
*/}}
|
2016-02-22 14:44:12 -05:00
|
|
|
slh, containerLenS := d.decSliceHelperStart()
|
|
|
|
if containerLenS == 0 {
|
|
|
|
if canChange {
|
2019-10-14 10:21:52 -04:00
|
|
|
if v == nil { v = []{{ .Elem }}{} } else if len(v) != 0 { v = v[:0] }
|
2016-02-22 14:44:12 -05:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
slh.End()
|
|
|
|
return v, changed
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
hasLen := containerLenS > 0
|
|
|
|
var xlen int
|
|
|
|
if hasLen && canChange {
|
2016-02-22 14:44:12 -05:00
|
|
|
if containerLenS > cap(v) {
|
2019-10-14 10:21:52 -04:00
|
|
|
xlen = decInferLen(containerLenS, d.h.MaxInitLen, {{ .Size }})
|
|
|
|
if xlen <= cap(v) {
|
|
|
|
v = v[:xlen]
|
2016-02-22 14:44:12 -05:00
|
|
|
} else {
|
2019-10-14 10:21:52 -04:00
|
|
|
v = make([]{{ .Elem }}, xlen)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
changed = true
|
2016-02-22 14:44:12 -05:00
|
|
|
} else if containerLenS != len(v) {
|
2019-10-14 10:21:52 -04:00
|
|
|
v = v[:containerLenS]
|
|
|
|
changed = true
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
}
|
|
|
|
j := 0
|
|
|
|
for ; (hasLen && j < containerLenS) || !(hasLen || dd.CheckBreak()); j++ {
|
|
|
|
if j == 0 && len(v) == 0 && canChange {
|
|
|
|
if hasLen {
|
|
|
|
xlen = decInferLen(containerLenS, d.h.MaxInitLen, {{ .Size }})
|
|
|
|
} else {
|
|
|
|
xlen = 8
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
v = make([]{{ .Elem }}, xlen)
|
|
|
|
changed = true
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
// if indefinite, etc, then expand the slice if necessary
|
|
|
|
var decodeIntoBlank bool
|
|
|
|
if j >= len(v) {
|
2016-02-22 14:44:12 -05:00
|
|
|
if canChange {
|
2019-10-14 10:21:52 -04:00
|
|
|
v = append(v, {{ zerocmd .Elem }})
|
2016-02-22 14:44:12 -05:00
|
|
|
changed = true
|
|
|
|
} else {
|
2019-10-14 10:21:52 -04:00
|
|
|
d.arrayCannotExpand(len(v), j+1)
|
|
|
|
decodeIntoBlank = true
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
}
|
|
|
|
slh.ElemContainerState(j)
|
|
|
|
if decodeIntoBlank {
|
|
|
|
d.swallow()
|
|
|
|
} else if dd.TryDecodeAsNil() {
|
|
|
|
v[j] = {{ zerocmd .Elem }}
|
|
|
|
} else {
|
|
|
|
{{ if eq .Elem "interface{}" }}d.decode(&v[j]){{ else }}v[j] = {{ decmd .Elem }}{{ end }}
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
}
|
|
|
|
if canChange {
|
|
|
|
if j < len(v) {
|
2016-02-22 14:44:12 -05:00
|
|
|
v = v[:j]
|
|
|
|
changed = true
|
2019-10-14 10:21:52 -04:00
|
|
|
} else if j == 0 && v == nil {
|
|
|
|
v = make([]{{ .Elem }}, 0)
|
|
|
|
changed = true
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
slh.End()
|
2016-02-22 14:44:12 -05:00
|
|
|
return v, changed
|
|
|
|
}
|
|
|
|
{{end}}{{end}}{{end}}
|
|
|
|
|
|
|
|
{{range .Values}}{{if not .Primitive}}{{if .MapKey }}
|
|
|
|
{{/*
|
|
|
|
Maps can change if they are
|
|
|
|
- addressable (from a ptr)
|
|
|
|
- settable (e.g. contained in an interface{})
|
|
|
|
*/}}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (d *Decoder) {{ .MethodNamePfx "fastpathDec" false }}R(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
if rv.Kind() == reflect.Ptr {
|
|
|
|
vp := rv2i(rv).(*map[{{ .MapKey }}]{{ .Elem }})
|
|
|
|
v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d);
|
|
|
|
if changed { *vp = v }
|
2016-02-22 14:44:12 -05:00
|
|
|
} else {
|
2019-10-14 10:21:52 -04:00
|
|
|
fastpathTV.{{ .MethodNamePfx "Dec" false }}V(rv2i(rv).(map[{{ .MapKey }}]{{ .Elem }}), false, d)
|
|
|
|
}
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *map[{{ .MapKey }}]{{ .Elem }}, d *Decoder) {
|
|
|
|
v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d)
|
|
|
|
if changed { *vp = v }
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, canChange bool,
|
2016-02-22 14:44:12 -05:00
|
|
|
d *Decoder) (_ map[{{ .MapKey }}]{{ .Elem }}, changed bool) {
|
2019-10-14 10:21:52 -04:00
|
|
|
dd, esep := d.d, d.hh.hasElemSeparators(){{/*
|
|
|
|
// if dd.isContainerType(valueTypeNil) {dd.TryDecodeAsNil()
|
|
|
|
*/}}
|
2016-02-22 14:44:12 -05:00
|
|
|
containerLen := dd.ReadMapStart()
|
|
|
|
if canChange && v == nil {
|
2019-10-14 10:21:52 -04:00
|
|
|
xlen := decInferLen(containerLen, d.h.MaxInitLen, {{ .Size }})
|
2016-02-22 14:44:12 -05:00
|
|
|
v = make(map[{{ .MapKey }}]{{ .Elem }}, xlen)
|
|
|
|
changed = true
|
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
if containerLen == 0 {
|
|
|
|
dd.ReadMapEnd()
|
|
|
|
return v, changed
|
|
|
|
}
|
|
|
|
{{ if eq .Elem "interface{}" }}mapGet := v != nil && !d.h.MapValueReset && !d.h.InterfaceReset
|
|
|
|
{{end}}var mk {{ .MapKey }}
|
2016-02-22 14:44:12 -05:00
|
|
|
var mv {{ .Elem }}
|
2019-10-14 10:21:52 -04:00
|
|
|
hasLen := containerLen > 0
|
|
|
|
for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ {
|
|
|
|
if esep { dd.ReadMapElemKey() }
|
|
|
|
{{ if eq .MapKey "interface{}" }}mk = nil
|
|
|
|
d.decode(&mk)
|
|
|
|
if bv, bok := mk.([]byte); bok {
|
|
|
|
mk = d.string(bv) {{/* // maps cannot have []byte as key. switch to string. */}}
|
|
|
|
}{{ else }}mk = {{ decmd .MapKey }}{{ end }}
|
|
|
|
if esep { dd.ReadMapElemValue() }
|
|
|
|
if dd.TryDecodeAsNil() {
|
|
|
|
if v == nil {} else if d.h.DeleteOnNilMapValue { delete(v, mk) } else { v[mk] = {{ zerocmd .Elem }} }
|
|
|
|
continue
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
{{ if eq .Elem "interface{}" }}if mapGet { mv = v[mk] } else { mv = nil }
|
|
|
|
d.decode(&mv){{ else }}mv = {{ decmd .Elem }}{{ end }}
|
|
|
|
if v != nil { v[mk] = mv }
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-10-14 10:21:52 -04:00
|
|
|
dd.ReadMapEnd()
|
2016-02-22 14:44:12 -05:00
|
|
|
return v, changed
|
|
|
|
}
|
|
|
|
{{end}}{{end}}{{end}}
|