Remove unnecessary prefix

This commit is contained in:
Masaaki Goshima 2021-06-03 18:57:41 +09:00
parent 5a024ca2f1
commit de4e97ed09
3 changed files with 64 additions and 64 deletions

View File

@ -44,13 +44,13 @@ func storeDecoder(typ uintptr, dec decoder, m map[uintptr]decoder) {
atomic.StorePointer(&cachedDecoderMap, *(*unsafe.Pointer)(unsafe.Pointer(&newDecoderMap))) atomic.StorePointer(&cachedDecoderMap, *(*unsafe.Pointer)(unsafe.Pointer(&newDecoderMap)))
} }
func decodeCompileToGetDecoderSlowPath(typeptr uintptr, typ *runtime.Type) (decoder, error) { func compileToGetDecoderSlowPath(typeptr uintptr, typ *runtime.Type) (decoder, error) {
decoderMap := loadDecoderMap() decoderMap := loadDecoderMap()
if dec, exists := decoderMap[typeptr]; exists { if dec, exists := decoderMap[typeptr]; exists {
return dec, nil return dec, nil
} }
dec, err := decodeCompileHead(typ, map[uintptr]decoder{}) dec, err := compileHead(typ, map[uintptr]decoder{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -58,17 +58,17 @@ func decodeCompileToGetDecoderSlowPath(typeptr uintptr, typ *runtime.Type) (deco
return dec, nil return dec, nil
} }
func decodeCompileHead(typ *runtime.Type, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compileHead(typ *runtime.Type, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
switch { switch {
case runtime.PtrTo(typ).Implements(unmarshalJSONType): case runtime.PtrTo(typ).Implements(unmarshalJSONType):
return newUnmarshalJSONDecoder(runtime.PtrTo(typ), "", ""), nil return newUnmarshalJSONDecoder(runtime.PtrTo(typ), "", ""), nil
case runtime.PtrTo(typ).Implements(unmarshalTextType): case runtime.PtrTo(typ).Implements(unmarshalTextType):
return newUnmarshalTextDecoder(runtime.PtrTo(typ), "", ""), nil return newUnmarshalTextDecoder(runtime.PtrTo(typ), "", ""), nil
} }
return decodeCompile(typ.Elem(), "", "", structTypeToDecoder) return compile(typ.Elem(), "", "", structTypeToDecoder)
} }
func decodeCompile(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compile(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
switch { switch {
case runtime.PtrTo(typ).Implements(unmarshalJSONType): case runtime.PtrTo(typ).Implements(unmarshalJSONType):
return newUnmarshalJSONDecoder(runtime.PtrTo(typ), structName, fieldName), nil return newUnmarshalJSONDecoder(runtime.PtrTo(typ), structName, fieldName), nil
@ -78,51 +78,51 @@ func decodeCompile(typ *runtime.Type, structName, fieldName string, structTypeTo
switch typ.Kind() { switch typ.Kind() {
case reflect.Ptr: case reflect.Ptr:
return decodeCompilePtr(typ, structName, fieldName, structTypeToDecoder) return compilePtr(typ, structName, fieldName, structTypeToDecoder)
case reflect.Struct: case reflect.Struct:
return decodeCompileStruct(typ, structName, fieldName, structTypeToDecoder) return compileStruct(typ, structName, fieldName, structTypeToDecoder)
case reflect.Slice: case reflect.Slice:
elem := typ.Elem() elem := typ.Elem()
if elem.Kind() == reflect.Uint8 { if elem.Kind() == reflect.Uint8 {
return decodeCompileBytes(elem, structName, fieldName) return compileBytes(elem, structName, fieldName)
} }
return decodeCompileSlice(typ, structName, fieldName, structTypeToDecoder) return compileSlice(typ, structName, fieldName, structTypeToDecoder)
case reflect.Array: case reflect.Array:
return decodeCompileArray(typ, structName, fieldName, structTypeToDecoder) return compileArray(typ, structName, fieldName, structTypeToDecoder)
case reflect.Map: case reflect.Map:
return decodeCompileMap(typ, structName, fieldName, structTypeToDecoder) return compileMap(typ, structName, fieldName, structTypeToDecoder)
case reflect.Interface: case reflect.Interface:
return decodeCompileInterface(typ, structName, fieldName) return compileInterface(typ, structName, fieldName)
case reflect.Uintptr: case reflect.Uintptr:
return decodeCompileUint(typ, structName, fieldName) return compileUint(typ, structName, fieldName)
case reflect.Int: case reflect.Int:
return decodeCompileInt(typ, structName, fieldName) return compileInt(typ, structName, fieldName)
case reflect.Int8: case reflect.Int8:
return decodeCompileInt8(typ, structName, fieldName) return compileInt8(typ, structName, fieldName)
case reflect.Int16: case reflect.Int16:
return decodeCompileInt16(typ, structName, fieldName) return compileInt16(typ, structName, fieldName)
case reflect.Int32: case reflect.Int32:
return decodeCompileInt32(typ, structName, fieldName) return compileInt32(typ, structName, fieldName)
case reflect.Int64: case reflect.Int64:
return decodeCompileInt64(typ, structName, fieldName) return compileInt64(typ, structName, fieldName)
case reflect.Uint: case reflect.Uint:
return decodeCompileUint(typ, structName, fieldName) return compileUint(typ, structName, fieldName)
case reflect.Uint8: case reflect.Uint8:
return decodeCompileUint8(typ, structName, fieldName) return compileUint8(typ, structName, fieldName)
case reflect.Uint16: case reflect.Uint16:
return decodeCompileUint16(typ, structName, fieldName) return compileUint16(typ, structName, fieldName)
case reflect.Uint32: case reflect.Uint32:
return decodeCompileUint32(typ, structName, fieldName) return compileUint32(typ, structName, fieldName)
case reflect.Uint64: case reflect.Uint64:
return decodeCompileUint64(typ, structName, fieldName) return compileUint64(typ, structName, fieldName)
case reflect.String: case reflect.String:
return decodeCompileString(typ, structName, fieldName) return compileString(typ, structName, fieldName)
case reflect.Bool: case reflect.Bool:
return decodeCompileBool(structName, fieldName) return compileBool(structName, fieldName)
case reflect.Float32: case reflect.Float32:
return decodeCompileFloat32(structName, fieldName) return compileFloat32(structName, fieldName)
case reflect.Float64: case reflect.Float64:
return decodeCompileFloat64(structName, fieldName) return compileFloat64(structName, fieldName)
} }
return nil, &errors.UnmarshalTypeError{ return nil, &errors.UnmarshalTypeError{
Value: "object", Value: "object",
@ -153,11 +153,11 @@ func isStringTagSupportedType(typ *runtime.Type) bool {
return true return true
} }
func decodeCompileMapKey(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compileMapKey(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
if runtime.PtrTo(typ).Implements(unmarshalTextType) { if runtime.PtrTo(typ).Implements(unmarshalTextType) {
return newUnmarshalTextDecoder(runtime.PtrTo(typ), structName, fieldName), nil return newUnmarshalTextDecoder(runtime.PtrTo(typ), structName, fieldName), nil
} }
dec, err := decodeCompile(typ, structName, fieldName, structTypeToDecoder) dec, err := compile(typ, structName, fieldName, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -181,87 +181,87 @@ ERROR:
} }
} }
func decodeCompilePtr(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compilePtr(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
dec, err := decodeCompile(typ.Elem(), structName, fieldName, structTypeToDecoder) dec, err := compile(typ.Elem(), structName, fieldName, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil
} }
func decodeCompileInt(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileInt(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int)(p) = int(v) *(*int)(p) = int(v)
}), nil }), nil
} }
func decodeCompileInt8(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileInt8(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int8)(p) = int8(v) *(*int8)(p) = int8(v)
}), nil }), nil
} }
func decodeCompileInt16(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileInt16(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int16)(p) = int16(v) *(*int16)(p) = int16(v)
}), nil }), nil
} }
func decodeCompileInt32(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileInt32(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int32)(p) = int32(v) *(*int32)(p) = int32(v)
}), nil }), nil
} }
func decodeCompileInt64(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileInt64(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int64)(p) = v *(*int64)(p) = v
}), nil }), nil
} }
func decodeCompileUint(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileUint(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) { return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
*(*uint)(p) = uint(v) *(*uint)(p) = uint(v)
}), nil }), nil
} }
func decodeCompileUint8(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileUint8(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) { return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
*(*uint8)(p) = uint8(v) *(*uint8)(p) = uint8(v)
}), nil }), nil
} }
func decodeCompileUint16(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileUint16(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) { return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
*(*uint16)(p) = uint16(v) *(*uint16)(p) = uint16(v)
}), nil }), nil
} }
func decodeCompileUint32(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileUint32(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) { return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
*(*uint32)(p) = uint32(v) *(*uint32)(p) = uint32(v)
}), nil }), nil
} }
func decodeCompileUint64(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileUint64(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) { return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
*(*uint64)(p) = v *(*uint64)(p) = v
}), nil }), nil
} }
func decodeCompileFloat32(structName, fieldName string) (decoder, error) { func compileFloat32(structName, fieldName string) (decoder, error) {
return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) { return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) {
*(*float32)(p) = float32(v) *(*float32)(p) = float32(v)
}), nil }), nil
} }
func decodeCompileFloat64(structName, fieldName string) (decoder, error) { func compileFloat64(structName, fieldName string) (decoder, error) {
return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) { return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) {
*(*float64)(p) = v *(*float64)(p) = v
}), nil }), nil
} }
func decodeCompileString(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileString(typ *runtime.Type, structName, fieldName string) (decoder, error) {
if typ == runtime.Type2RType(jsonNumberType) { if typ == runtime.Type2RType(jsonNumberType) {
return newNumberDecoder(structName, fieldName, func(p unsafe.Pointer, v json.Number) { return newNumberDecoder(structName, fieldName, func(p unsafe.Pointer, v json.Number) {
*(*json.Number)(p) = v *(*json.Number)(p) = v
@ -270,49 +270,49 @@ func decodeCompileString(typ *runtime.Type, structName, fieldName string) (decod
return newStringDecoder(structName, fieldName), nil return newStringDecoder(structName, fieldName), nil
} }
func decodeCompileBool(structName, fieldName string) (decoder, error) { func compileBool(structName, fieldName string) (decoder, error) {
return newBoolDecoder(structName, fieldName), nil return newBoolDecoder(structName, fieldName), nil
} }
func decodeCompileBytes(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileBytes(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newBytesDecoder(typ, structName, fieldName), nil return newBytesDecoder(typ, structName, fieldName), nil
} }
func decodeCompileSlice(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compileSlice(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
elem := typ.Elem() elem := typ.Elem()
decoder, err := decodeCompile(elem, structName, fieldName, structTypeToDecoder) decoder, err := compile(elem, structName, fieldName, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newSliceDecoder(decoder, elem, elem.Size(), structName, fieldName), nil return newSliceDecoder(decoder, elem, elem.Size(), structName, fieldName), nil
} }
func decodeCompileArray(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compileArray(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
elem := typ.Elem() elem := typ.Elem()
decoder, err := decodeCompile(elem, structName, fieldName, structTypeToDecoder) decoder, err := compile(elem, structName, fieldName, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newArrayDecoder(decoder, elem, typ.Len(), structName, fieldName), nil return newArrayDecoder(decoder, elem, typ.Len(), structName, fieldName), nil
} }
func decodeCompileMap(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compileMap(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
keyDec, err := decodeCompileMapKey(typ.Key(), structName, fieldName, structTypeToDecoder) keyDec, err := compileMapKey(typ.Key(), structName, fieldName, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
valueDec, err := decodeCompile(typ.Elem(), structName, fieldName, structTypeToDecoder) valueDec, err := compile(typ.Elem(), structName, fieldName, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newMapDecoder(typ, typ.Key(), keyDec, typ.Elem(), valueDec, structName, fieldName), nil return newMapDecoder(typ, typ.Key(), keyDec, typ.Elem(), valueDec, structName, fieldName), nil
} }
func decodeCompileInterface(typ *runtime.Type, structName, fieldName string) (decoder, error) { func compileInterface(typ *runtime.Type, structName, fieldName string) (decoder, error) {
return newInterfaceDecoder(typ, structName, fieldName), nil return newInterfaceDecoder(typ, structName, fieldName), nil
} }
func decodeRemoveConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, field reflect.StructField) { func removeConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, field reflect.StructField) {
for k, v := range dec.fieldMap { for k, v := range dec.fieldMap {
if _, exists := conflictedMap[k]; exists { if _, exists := conflictedMap[k]; exists {
// already conflicted key // already conflicted key
@ -367,7 +367,7 @@ func decodeRemoveConflictFields(fieldMap map[string]*structFieldSet, conflictedM
} }
} }
func decodeCompileStruct(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func compileStruct(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
fieldNum := typ.NumField() fieldNum := typ.NumField()
conflictedMap := map[string]struct{}{} conflictedMap := map[string]struct{}{}
fieldMap := map[string]*structFieldSet{} fieldMap := map[string]*structFieldSet{}
@ -385,7 +385,7 @@ func decodeCompileStruct(typ *runtime.Type, structName, fieldName string, struct
} }
isUnexportedField := unicode.IsLower([]rune(field.Name)[0]) isUnexportedField := unicode.IsLower([]rune(field.Name)[0])
tag := runtime.StructTagFromField(field) tag := runtime.StructTagFromField(field)
dec, err := decodeCompile(runtime.Type2RType(field.Type), structName, field.Name, structTypeToDecoder) dec, err := compile(runtime.Type2RType(field.Type), structName, field.Name, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -395,7 +395,7 @@ func decodeCompileStruct(typ *runtime.Type, structName, fieldName string, struct
// recursive definition // recursive definition
continue continue
} }
decodeRemoveConflictFields(fieldMap, conflictedMap, stDec, field) removeConflictFields(fieldMap, conflictedMap, stDec, field)
} else if pdec, ok := dec.(*ptrDecoder); ok { } else if pdec, ok := dec.(*ptrDecoder); ok {
contentDec := pdec.contentDecoder() contentDec := pdec.contentDecoder()
if pdec.typ == typ { if pdec.typ == typ {

View File

@ -11,7 +11,7 @@ import (
func CompileToGetDecoder(typ *runtime.Type) (decoder, error) { func CompileToGetDecoder(typ *runtime.Type) (decoder, error) {
typeptr := uintptr(unsafe.Pointer(typ)) typeptr := uintptr(unsafe.Pointer(typ))
if typeptr > typeAddr.MaxTypeAddr { if typeptr > typeAddr.MaxTypeAddr {
return decodeCompileToGetDecoderSlowPath(typeptr, typ) return compileToGetDecoderSlowPath(typeptr, typ)
} }
index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
@ -19,7 +19,7 @@ func CompileToGetDecoder(typ *runtime.Type) (decoder, error) {
return dec, nil return dec, nil
} }
dec, err := decodeCompileHead(typ, map[uintptr]decoder{}) dec, err := compileHead(typ, map[uintptr]decoder{})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -13,11 +13,11 @@ var decMu sync.RWMutex
func CompileToGetDecoder(typ *runtime.Type) (decoder, error) { func CompileToGetDecoder(typ *runtime.Type) (decoder, error) {
typeptr := uintptr(unsafe.Pointer(typ)) typeptr := uintptr(unsafe.Pointer(typ))
if typeptr > maxTypeAddr { if typeptr > typeAddr.MaxTypeAddr {
return decodeCompileToGetDecoderSlowPath(typeptr, typ) return compileToGetDecoderSlowPath(typeptr, typ)
} }
index := (typeptr - baseTypeAddr) >> typeAddrShift index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
decMu.RLock() decMu.RLock()
if dec := cachedDecoder[index]; dec != nil { if dec := cachedDecoder[index]; dec != nil {
decMu.RUnlock() decMu.RUnlock()
@ -25,7 +25,7 @@ func CompileToGetDecoder(typ *runtime.Type) (decoder, error) {
} }
decMu.RUnlock() decMu.RUnlock()
dec, err := decodeCompileHead(typ, map[uintptr]decoder{}) dec, err := compileHead(typ, map[uintptr]decoder{})
if err != nil { if err != nil {
return nil, err return nil, err
} }