From e3e2d32e199f698bf1c65ce6a7a27a78633b1ebe Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 18 Feb 2015 14:11:10 +0900 Subject: [PATCH] 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% --- benchmark_test.go | 1 + connection.go | 35 ++++++----------------------------- 2 files changed, 7 insertions(+), 29 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index 623ba28fa..fb8a2f5f3 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -222,6 +222,7 @@ func BenchmarkInterpolation(b *testing.B) { }, maxPacketAllowed: maxPacketSize, maxWriteSize: maxPacketSize - 1, + buf: newBuffer(nil), } args := []driver.Value{ diff --git a/connection.go b/connection.go index 82e39628e..a6d39bec9 100644 --- a/connection.go +++ b/connection.go @@ -165,37 +165,14 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) { return stmt, err } -// estimateParamLength calculates upper bound of string length from types. -func estimateParamLength(args []driver.Value) (int, bool) { - l := 0 - for _, a := range args { - switch v := a.(type) { - case int64, float64: - // 24 (-1.7976931348623157e+308) may be upper bound. But I'm not sure. - l += 25 - case bool: - l += 1 // 0 or 1 - case time.Time: - l += 30 // '1234-12-23 12:34:56.777777' - case string: - l += len(v)*2 + 2 - case []byte: - l += len(v)*2 + 2 - default: - return 0, false - } - } - return l, true -} - func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (string, error) { - estimated, ok := estimateParamLength(args) - if !ok { - return "", driver.ErrSkip + buf := mc.buf.takeCompleteBuffer() + if buf == nil { + // can not take the buffer. Something must be wrong with the connection + errLog.Print(ErrBusyBuffer) + return "", driver.ErrBadConn } - estimated += len(query) - - buf := make([]byte, 0, estimated) + buf = buf[:0] argPos := 0 for i := 0; i < len(query); i++ {