Second optimization pass

This commit is contained in:
Aaron L 2016-08-31 00:47:35 -07:00
parent 7fb0e43648
commit e4d7f1b8e2

View file

@ -6,6 +6,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/vattle/sqlboiler/strmangle" "github.com/vattle/sqlboiler/strmangle"
@ -249,6 +250,9 @@ 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 {
@ -261,9 +265,30 @@ func bind(rows *sql.Rows, obj interface{}, structType, sliceType reflect.Type, s
} }
var mapping []uint64 var mapping []uint64
mapping, err = bindMapping(structType, titleCases, cols) var ok bool
if err != nil {
return err buf := strmangle.GetBuffer()
buf.WriteString(structType.String())
for _, s := range cols {
buf.WriteString(s)
}
mapKey := buf.String()
strmangle.PutBuffer(buf)
mut.RLock()
mapping, ok = mappingThings[mapKey]
mut.RUnlock()
if !ok {
mapping, err = bindMapping(structType, titleCases, cols)
if err != nil {
return err
}
mut.Lock()
mappingThings[mapKey] = mapping
mut.Unlock()
} }
foundOne := false foundOne := false