Tidy up optimization

This commit is contained in:
Aaron L 2016-08-31 21:18:30 -07:00
parent 2d0924a625
commit fdac827cd8

View file

@ -15,6 +15,11 @@ var (
bindAccepts = []reflect.Kind{reflect.Ptr, reflect.Slice, reflect.Ptr, reflect.Struct} bindAccepts = []reflect.Kind{reflect.Ptr, reflect.Slice, reflect.Ptr, reflect.Struct}
) )
var (
mut sync.RWMutex
bindingMaps = make(map[string][]uint64)
)
const ( const (
loadMethodPrefix = "Load" loadMethodPrefix = "Load"
relationshipStructName = "R" relationshipStructName = "R"
@ -250,9 +255,6 @@ func bindChecks(obj interface{}) (structType reflect.Type, sliceType reflect.Typ
return structType, sliceType, singular, nil return structType, sliceType, singular, nil
} }
var mut sync.RWMutex
var mappingThings = map[string][]uint64{}
func bind(rows *sql.Rows, obj interface{}, structType, sliceType reflect.Type, singular bool, titleCases map[string]string) error { func bind(rows *sql.Rows, obj interface{}, structType, sliceType reflect.Type, singular bool, titleCases map[string]string) error {
cols, err := rows.Columns() cols, err := rows.Columns()
if err != nil { if err != nil {
@ -267,17 +269,9 @@ func bind(rows *sql.Rows, obj interface{}, structType, sliceType reflect.Type, s
var mapping []uint64 var mapping []uint64
var ok bool var ok bool
buf := strmangle.GetBuffer() mapKey := makeCacheKey(structType.String(), cols)
buf.WriteString(structType.String())
for _, s := range cols {
buf.WriteString(s)
}
mapKey := buf.String()
strmangle.PutBuffer(buf)
mut.RLock() mut.RLock()
mapping, ok = mappingThings[mapKey] mapping, ok = bindingMaps[mapKey]
mut.RUnlock() mut.RUnlock()
if !ok { if !ok {
@ -287,7 +281,7 @@ func bind(rows *sql.Rows, obj interface{}, structType, sliceType reflect.Type, s
} }
mut.Lock() mut.Lock()
mappingThings[mapKey] = mapping bindingMaps[mapKey] = mapping
mut.Unlock() mut.Unlock()
} }
@ -445,6 +439,18 @@ func getBoilTag(field reflect.StructField, titleCases map[string]string) (name s
return name, true return name, true
} }
func makeCacheKey(typ string, cols []string) string {
buf := strmangle.GetBuffer()
buf.WriteString(typ)
for _, s := range cols {
buf.WriteString(s)
}
mapKey := buf.String()
strmangle.PutBuffer(buf)
return mapKey
}
// GetStructValues returns the values (as interface) of the matching columns in obj // GetStructValues returns the values (as interface) of the matching columns in obj
func GetStructValues(obj interface{}, titleCases map[string]string, columns ...string) []interface{} { func GetStructValues(obj interface{}, titleCases map[string]string, columns ...string) []interface{} {
ret := make([]interface{}, len(columns)) ret := make([]interface{}, len(columns))