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

View File

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