a little performance improved

This commit is contained in:
Lunny Xiao 2014-02-10 15:36:25 +08:00
parent c31c21b051
commit a76d74804f
2 changed files with 7 additions and 5 deletions

View File

@ -27,7 +27,7 @@ type Engine struct {
dialect core.Dialect dialect core.Dialect
Tables map[reflect.Type]*core.Table Tables map[reflect.Type]*core.Table
mutex *sync.Mutex mutex *sync.RWMutex
ShowSQL bool ShowSQL bool
ShowErr bool ShowErr bool
ShowDebug bool ShowDebug bool
@ -388,12 +388,14 @@ func (engine *Engine) Having(conditions string) *Session {
} }
func (engine *Engine) autoMapType(t reflect.Type) *core.Table { func (engine *Engine) autoMapType(t reflect.Type) *core.Table {
engine.mutex.Lock() engine.mutex.RLock()
defer engine.mutex.Unlock()
table, ok := engine.Tables[t] table, ok := engine.Tables[t]
engine.mutex.RUnlock()
if !ok { if !ok {
table = engine.mapType(t) table = engine.mapType(t)
engine.mutex.Lock()
engine.Tables[t] = table engine.Tables[t] = table
engine.mutex.Unlock()
} }
return table return table
} }

View File

@ -50,12 +50,12 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
DataSourceName: dataSourceName, dialect: dialect, DataSourceName: dataSourceName, dialect: dialect,
tableCachers: make(map[reflect.Type]core.Cacher)} tableCachers: make(map[reflect.Type]core.Cacher)}
engine.SetMapper(core.SnakeMapper{}) engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))
engine.Filters = dialect.Filters() engine.Filters = dialect.Filters()
engine.Tables = make(map[reflect.Type]*core.Table) engine.Tables = make(map[reflect.Type]*core.Table)
engine.mutex = &sync.Mutex{} engine.mutex = &sync.RWMutex{}
engine.TagIdentifier = "xorm" engine.TagIdentifier = "xorm"
engine.Logger = os.Stdout engine.Logger = os.Stdout