Add poolInitializer

This commit is contained in:
Jinzhu 2022-02-15 15:38:53 +08:00
parent 20abf83a21
commit 6393ad5222

View File

@ -413,7 +413,9 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
return field return field
} }
// sync pools
var ( var (
normalPool sync.Map
stringPool = &sync.Pool{ stringPool = &sync.Pool{
New: func() interface{} { New: func() interface{} {
var v string var v string
@ -456,6 +458,18 @@ var (
return &ptrV return &ptrV
}, },
} }
poolInitializer = func(reflectType reflect.Type) FieldNewValuePool {
if v, ok := normalPool.Load(reflectType); ok {
return v.(FieldNewValuePool)
}
v, _ := normalPool.LoadOrStore(reflectType, &sync.Pool{
New: func() interface{} {
return reflect.New(reflectType).Interface()
},
})
return v.(FieldNewValuePool)
}
) )
// create valuer, setter when parse struct // create valuer, setter when parse struct
@ -481,12 +495,7 @@ func (field *Field) setupValuerAndSetter() {
} }
if field.NewValuePool == nil { if field.NewValuePool == nil {
field.NewValuePool = fieldNewValuePool{ field.NewValuePool = poolInitializer(reflect.PtrTo(field.IndirectFieldType))
getter: func() interface{} {
return reflect.New(reflect.PtrTo(field.IndirectFieldType)).Interface()
},
putter: func(interface{}) {},
}
} }
// ValueOf returns field's value and if it is zero // ValueOf returns field's value and if it is zero