Skip to content

Commit 74a8ec7

Browse files
authored
Merge pull request #1337 from nicowolf91/improve-loads
Improve loads by using maps for deduplication
2 parents 682d8c9 + c7f1c20 commit 74a8ec7

File tree

3 files changed

+36
-49
lines changed

3 files changed

+36
-49
lines changed

templates/main/07_relationship_to_one_eager.go.tpl

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,29 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
3737
}
3838
}
3939

40-
args := make([]interface{}, 0, 1)
40+
args := make(map[interface{}]struct{})
4141
if singular {
4242
if object.R == nil {
4343
object.R = &{{$ltable.DownSingular}}R{}
4444
}
4545
{{if $usesPrimitives -}}
46-
args = append(args, object.{{$col}})
46+
args[object.{{$col}}] = struct{}{}
4747
{{else -}}
4848
if !queries.IsNil(object.{{$col}}) {
49-
args = append(args, object.{{$col}})
49+
args[object.{{$col}}] = struct{}{}
5050
}
5151
{{end}}
5252
} else {
53-
Outer:
5453
for _, obj := range slice {
5554
if obj.R == nil {
5655
obj.R = &{{$ltable.DownSingular}}R{}
5756
}
5857

59-
for _, a := range args {
60-
{{if $usesPrimitives -}}
61-
if a == obj.{{$col}} {
62-
{{else -}}
63-
if queries.Equal(a, obj.{{$col}}) {
64-
{{end -}}
65-
continue Outer
66-
}
67-
}
68-
6958
{{if $usesPrimitives -}}
70-
args = append(args, obj.{{$col}})
59+
args[obj.{{$col}}] = struct{}{}
7160
{{else -}}
7261
if !queries.IsNil(obj.{{$col}}) {
73-
args = append(args, obj.{{$col}})
62+
args[obj.{{$col}}] = struct{}{}
7463
}
7564
{{end}}
7665
}
@@ -80,9 +69,16 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
8069
return nil
8170
}
8271

72+
argsSlice := make([]interface{}, len(args))
73+
i := 0
74+
for arg := range args {
75+
argsSlice[i] = arg
76+
i++
77+
}
78+
8379
query := NewQuery(
8480
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
85-
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
81+
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, argsSlice...),
8682
{{if and $.AddSoftDeletes $canSoftDelete -}}
8783
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
8884
{{- end}}

templates/main/08_relationship_one_to_one_eager.go.tpl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,36 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
3737
}
3838
}
3939

40-
args := make([]interface{}, 0, 1)
40+
args := make(map[interface{}]struct{})
4141
if singular {
4242
if object.R == nil {
4343
object.R = &{{$ltable.DownSingular}}R{}
4444
}
45-
args = append(args, object.{{$col}})
45+
args[object.{{$col}}] = struct{}{}
4646
} else {
47-
Outer:
4847
for _, obj := range slice {
4948
if obj.R == nil {
5049
obj.R = &{{$ltable.DownSingular}}R{}
5150
}
5251

53-
for _, a := range args {
54-
{{if $usesPrimitives -}}
55-
if a == obj.{{$col}} {
56-
{{else -}}
57-
if queries.Equal(a, obj.{{$col}}) {
58-
{{end -}}
59-
continue Outer
60-
}
61-
}
62-
63-
args = append(args, obj.{{$col}})
52+
args[obj.{{$col}}] = struct{}{}
6453
}
6554
}
6655

6756
if len(args) == 0 {
6857
return nil
6958
}
7059

60+
argsSlice := make([]interface{}, len(args))
61+
i := 0
62+
for arg := range args {
63+
argsSlice[i] = arg
64+
i++
65+
}
66+
7167
query := NewQuery(
7268
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
73-
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
69+
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, argsSlice...),
7470
{{if and $.AddSoftDeletes $canSoftDelete -}}
7571
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
7672
{{- end}}

templates/main/09_relationship_to_many_eager.go.tpl

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,48 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
3838
}
3939
}
4040

41-
args := make([]interface{}, 0, 1)
41+
args := make(map[interface{}]struct{})
4242
if singular {
4343
if object.R == nil {
4444
object.R = &{{$ltable.DownSingular}}R{}
4545
}
46-
args = append(args, object.{{$col}})
46+
args[object.{{$col}}] = struct{}{}
4747
} else {
48-
Outer:
4948
for _, obj := range slice {
5049
if obj.R == nil {
5150
obj.R = &{{$ltable.DownSingular}}R{}
5251
}
53-
54-
for _, a := range args {
55-
{{if $usesPrimitives -}}
56-
if a == obj.{{$col}} {
57-
{{else -}}
58-
if queries.Equal(a, obj.{{$col}}) {
59-
{{end -}}
60-
continue Outer
61-
}
62-
}
63-
64-
args = append(args, obj.{{$col}})
52+
args[obj.{{$col}}] = struct{}{}
6553
}
6654
}
6755

6856
if len(args) == 0 {
6957
return nil
7058
}
7159

60+
argsSlice := make([]interface{}, len(args))
61+
i := 0
62+
for arg := range args {
63+
argsSlice[i] = arg
64+
i++
65+
}
66+
7267
{{if .ToJoinTable -}}
7368
{{- $schemaJoinTable := .JoinTable | $.SchemaTable -}}
7469
{{- $foreignTable := getTable $.Tables .ForeignTable -}}
7570
query := NewQuery(
7671
qm.Select("{{$foreignTable.Columns | columnNames | $.QuoteMap | prefixStringSlice (print $schemaForeignTable ".") | join ", "}}, {{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}}"),
7772
qm.From("{{$schemaForeignTable}}"),
7873
qm.InnerJoin("{{$schemaJoinTable}} as {{id 0 | $.Quotes}} on {{$schemaForeignTable}}.{{.ForeignColumn | $.Quotes}} = {{id 0 | $.Quotes}}.{{.JoinForeignColumn | $.Quotes}}"),
79-
qm.WhereIn("{{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}} in ?", args...),
74+
qm.WhereIn("{{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}} in ?", argsSlice...),
8075
{{if and $.AddSoftDeletes $canSoftDelete -}}
8176
qmhelper.WhereIsNull("{{$schemaForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at" | $.Quotes}}"),
8277
{{- end}}
8378
)
8479
{{else -}}
8580
query := NewQuery(
8681
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
87-
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
82+
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, argsSlice...),
8883
{{if and $.AddSoftDeletes $canSoftDelete -}}
8984
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
9085
{{- end}}

0 commit comments

Comments
 (0)