Skip to content

Commit e3e2d32

Browse files
committed
Use mc.buf while interpolating
benchmark old ns/op new ns/op delta BenchmarkInterpolation 1900 1363 -28.26% benchmark old allocs new allocs delta BenchmarkInterpolation 2 1 -50.00% benchmark old bytes new bytes delta BenchmarkInterpolation 448 160 -64.29%
1 parent 96b3f4c commit e3e2d32

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

benchmark_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func BenchmarkInterpolation(b *testing.B) {
222222
},
223223
maxPacketAllowed: maxPacketSize,
224224
maxWriteSize: maxPacketSize - 1,
225+
buf: newBuffer(nil),
225226
}
226227

227228
args := []driver.Value{

connection.go

+6-29
Original file line numberDiff line numberDiff line change
@@ -165,37 +165,14 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
165165
return stmt, err
166166
}
167167

168-
// estimateParamLength calculates upper bound of string length from types.
169-
func estimateParamLength(args []driver.Value) (int, bool) {
170-
l := 0
171-
for _, a := range args {
172-
switch v := a.(type) {
173-
case int64, float64:
174-
// 24 (-1.7976931348623157e+308) may be upper bound. But I'm not sure.
175-
l += 25
176-
case bool:
177-
l += 1 // 0 or 1
178-
case time.Time:
179-
l += 30 // '1234-12-23 12:34:56.777777'
180-
case string:
181-
l += len(v)*2 + 2
182-
case []byte:
183-
l += len(v)*2 + 2
184-
default:
185-
return 0, false
186-
}
187-
}
188-
return l, true
189-
}
190-
191168
func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (string, error) {
192-
estimated, ok := estimateParamLength(args)
193-
if !ok {
194-
return "", driver.ErrSkip
169+
buf := mc.buf.takeCompleteBuffer()
170+
if buf == nil {
171+
// can not take the buffer. Something must be wrong with the connection
172+
errLog.Print(ErrBusyBuffer)
173+
return "", driver.ErrBadConn
195174
}
196-
estimated += len(query)
197-
198-
buf := make([]byte, 0, estimated)
175+
buf = buf[:0]
199176
argPos := 0
200177

201178
for i := 0; i < len(query); i++ {

0 commit comments

Comments
 (0)