Skip to content

Commit

Permalink
handleParams: use strings.Builder instead of direct []byte
Browse files Browse the repository at this point in the history
  • Loading branch information
dolmen committed May 27, 2020
1 parent 5748e04 commit 9f37b88
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions connection.go
Expand Up @@ -47,7 +47,7 @@ type mysqlConn struct {

// Handles parameters set in DSN after the connection is established
func (mc *mysqlConn) handleParams() (err error) {
var cmdSet []byte
var cmdSet strings.Builder
for param, val := range mc.cfg.Params {
switch param {
// Charset: character_set_connection, character_set_client, character_set_results
Expand All @@ -66,21 +66,22 @@ func (mc *mysqlConn) handleParams() (err error) {

// Other system vars accumulated in a single SET command
default:
if cmdSet == nil {
if cmdSet.Len() == 0 {
// Heuristic: 29 chars for each other key=value to reduce reallocations
cmdSet = make([]byte, 0, 4+len(param)+1+len(val)+30*(len(mc.cfg.Params)-1))
cmdSet = append(cmdSet, "SET "...)
cmdSet.Grow(4 + len(param) + 1 + len(val) + 30*(len(mc.cfg.Params)-1))
cmdSet.WriteString("SET ")
} else {
cmdSet = append(cmdSet, ',')
cmdSet.Grow(1 + len(param) + 1 + len(val))
cmdSet.WriteByte(',')
}
cmdSet = append(cmdSet, param...)
cmdSet = append(cmdSet, '=')
cmdSet = append(cmdSet, val...)
cmdSet.WriteString(param)
cmdSet.WriteByte('=')
cmdSet.WriteString(val)
}
}

if cmdSet != nil {
err = mc.exec(string(cmdSet))
if cmdSet.Len() > 0 {
err = mc.exec(cmdSet.String())
if err != nil {
return
}
Expand Down

0 comments on commit 9f37b88

Please sign in to comment.