Skip to content

Commit

Permalink
connect: reduce allocations when building SET command
Browse files Browse the repository at this point in the history
  • Loading branch information
dolmen committed May 22, 2020
1 parent 128a673 commit 5748e04
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 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 params []string
var cmdSet []byte
for param, val := range mc.cfg.Params {
switch param {
// Charset: character_set_connection, character_set_client, character_set_results
Expand All @@ -64,14 +64,23 @@ func (mc *mysqlConn) handleParams() (err error) {
return
}

// Other system vars
// Other system vars accumulated in a single SET command
default:
params = append(params, param+"="+val)
if cmdSet == nil {
// 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 "...)
} else {
cmdSet = append(cmdSet, ',')
}
cmdSet = append(cmdSet, param...)
cmdSet = append(cmdSet, '=')
cmdSet = append(cmdSet, val...)
}
}

if len(params) > 0 {
err = mc.exec("SET " + strings.Join(params, ","))
if cmdSet != nil {
err = mc.exec(string(cmdSet))
if err != nil {
return
}
Expand Down

0 comments on commit 5748e04

Please sign in to comment.