Grow SQL capacity to reduce allocation

This commit is contained in:
Jinzhu 2020-06-08 20:23:47 +08:00
parent aaf0725771
commit 9f19378304
4 changed files with 5 additions and 0 deletions

View File

@ -42,6 +42,7 @@ func Create(config *Config) func(db *gorm.DB) {
}
if db.Statement.SQL.String() == "" {
db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Insert{
Table: clause.Table{Name: db.Statement.Table},
})
@ -211,6 +212,7 @@ func ConvertToCreateValues(stmt *gorm.Statement) clause.Values {
switch stmt.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
stmt.SQL.Grow(stmt.ReflectValue.Len() * 15)
values.Values = make([][]interface{}, stmt.ReflectValue.Len())
defaultValueFieldsHavingValue := map[string][]interface{}{}
for i := 0; i < stmt.ReflectValue.Len(); i++ {

View File

@ -30,6 +30,7 @@ func Delete(db *gorm.DB) {
}
if db.Statement.SQL.String() == "" {
db.Statement.SQL.Grow(100)
db.Statement.AddClauseIfNotExists(clause.Delete{})
if db.Statement.Schema != nil {

View File

@ -37,6 +37,7 @@ func Query(db *gorm.DB) {
}
func BuildQuerySQL(db *gorm.DB) {
db.Statement.SQL.Grow(100)
clauseSelect := clause.Select{Distinct: db.Statement.Distinct}
if db.Statement.ReflectValue.Kind() == reflect.Struct {

View File

@ -59,6 +59,7 @@ func Update(db *gorm.DB) {
}
if db.Statement.SQL.String() == "" {
db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Update{})
if set := ConvertToAssignments(db.Statement); len(set) != 0 {
db.Statement.AddClause(set)