Skip to content

Commit

Permalink
reduce memory usage of building insert values
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Jan 6, 2025
1 parent 0c0dc8f commit 0363d4a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
18 changes: 13 additions & 5 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,25 @@ func commaTables(scope scope, tables []Table) string {
return sqlBuilder.String()
}

func commaValues(scope scope, values []interface{}) (string, error) {
var sqlBuilder strings.Builder
func commaValuesBuilder(scope scope, builder *strings.Builder, values []interface{}) error {
builder.Grow(16 * (len(values) + 2)) // hopefully reduce copying
for i, item := range values {
if i > 0 {
sqlBuilder.WriteString(", ")
builder.WriteString(", ")
}
itemSql, _, err := getSQL(scope, item)
if err != nil {
return "", err
return err
}
sqlBuilder.WriteString(itemSql)
builder.WriteString(itemSql)
}
return nil
}

func commaValues(scope scope, values []interface{}) (string, error) {
var sqlBuilder strings.Builder
if err := commaValuesBuilder(scope, &sqlBuilder, values); err != nil {
return "", err
}
return sqlBuilder.String(), nil
}
Expand Down
27 changes: 17 additions & 10 deletions insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"
)

type insertStatus struct {
Expand Down Expand Up @@ -138,7 +139,7 @@ func (s insertStatus) OnDuplicateKeyIgnore() toInsertFinal {

func (s insertStatus) GetSQL() (string, error) {
var fields []Field
var values []interface{}
var valuesBuilder strings.Builder
if len(s.models) > 0 {
models := make([]Model, 0, len(s.models))
for _, model := range s.models {
Expand All @@ -149,11 +150,19 @@ func (s insertStatus) GetSQL() (string, error) {

if len(models) > 0 {
fields = models[0].GetTable().GetFields()
for _, model := range models {
valuesBuilder.Grow(64 * len(models))
for i, model := range models {
if model.GetTable().GetName() != s.scope.Tables[0].GetName() {
return "", errors.New("invalid table from model")
}
values = append(values, model.GetValues())
if i > 0 {
valuesBuilder.WriteString(", ")
}
valuesBuilder.WriteString("(")
if err := commaValuesBuilder(s.scope, &valuesBuilder, model.GetValues()); err != nil {
return "", err
}
valuesBuilder.WriteString(")")
}
}
} else {
Expand All @@ -162,10 +171,12 @@ func (s insertStatus) GetSQL() (string, error) {
} else {
fields = s.fields
}
values = s.values
if err := commaValuesBuilder(s.scope, &valuesBuilder, s.values); err != nil {
return "", err
}
}

if len(values) == 0 {
if valuesBuilder.Len() == 0 {
return "/* INSERT without VALUES */ DO 0", nil
}

Expand All @@ -174,12 +185,8 @@ func (s insertStatus) GetSQL() (string, error) {
if err != nil {
return "", err
}
valuesSql, err := commaValues(s.scope, values)
if err != nil {
return "", err
}

sqlString := s.method + " INTO " + tableSql + " (" + fieldsSql + ") VALUES " + valuesSql
sqlString := s.method + " INTO " + tableSql + " (" + fieldsSql + ") VALUES " + valuesBuilder.String()
if len(s.onDuplicateKeyUpdateAssignments) > 0 {
assignmentsSql, err := commaAssignments(s.scope, s.onDuplicateKeyUpdateAssignments)
if err != nil {
Expand Down

0 comments on commit 0363d4a

Please sign in to comment.