Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect: reduce allocations when building SET command #1111

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 strings.Builder
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.Len() == 0 {
// Heuristic: 29 chars for each other key=value to reduce reallocations
cmdSet.Grow(4 + len(param) + 1 + len(val) + 30*(len(mc.cfg.Params)-1))
cmdSet.WriteString("SET ")
} else {
cmdSet.WriteByte(',')
}
cmdSet.WriteString(param)
cmdSet.WriteByte('=')
cmdSet.WriteString(val)
}
}

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