Skip to content

Commit 84af78b

Browse files
committed
Use strings.Builder to avoid an allocation for In
1 parent 0794cb1 commit 84af78b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

bind.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
169169
}
170170

171171
newArgs := make([]interface{}, 0, flatArgsCount)
172-
buf := make([]byte, 0, len(query)+len(", ?")*flatArgsCount)
172+
173+
var buf strings.Builder
174+
buf.Grow(len(query) + len(", ?")*flatArgsCount)
173175

174176
var arg, offset int
175177

@@ -195,10 +197,10 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
195197
}
196198

197199
// write everything up to and including our ? character
198-
buf = append(buf, query[:offset+i+1]...)
200+
buf.WriteString(query[:offset+i+1])
199201

200202
for si := 1; si < argMeta.length; si++ {
201-
buf = append(buf, ", ?"...)
203+
buf.WriteString(", ?")
202204
}
203205

204206
newArgs = appendReflectSlice(newArgs, argMeta.v, argMeta.length)
@@ -209,13 +211,13 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
209211
offset = 0
210212
}
211213

212-
buf = append(buf, query...)
214+
buf.WriteString(query)
213215

214216
if arg < len(meta) {
215217
return "", nil, errors.New("number of bindVars less than number arguments")
216218
}
217219

218-
return string(buf), newArgs, nil
220+
return buf.String(), newArgs, nil
219221
}
220222

221223
func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interface{} {

0 commit comments

Comments
 (0)