Skip to content

Commit

Permalink
Merge pull request go-sql-driver#318 from methane/interpolate-reduce-…
Browse files Browse the repository at this point in the history
…alloc

Use mc.buf as scratchpad for intarpolation
  • Loading branch information
arnehormann committed Feb 18, 2015
2 parents 96b3f4c + e3e2d32 commit 60fe63a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
1 change: 1 addition & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func BenchmarkInterpolation(b *testing.B) {
},
maxPacketAllowed: maxPacketSize,
maxWriteSize: maxPacketSize - 1,
buf: newBuffer(nil),
}

args := []driver.Value{
Expand Down
35 changes: 6 additions & 29 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down

0 comments on commit 60fe63a

Please sign in to comment.