Skip to content

Commit 26060e1

Browse files
authored
connect: reduce allocations when building SET command (#1111)
* connect: reduce allocations when building SET command * handleParams: use strings.Builder instead of direct []byte
1 parent 8c3a2d9 commit 26060e1

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

‎connection.go‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type mysqlConn struct{
4747

4848
// Handles parameters set in DSN after the connection is established
4949
func (mc*mysqlConn) handleParams() (errerror){
50-
varparams []string
50+
varcmdSet strings.Builder
5151
forparam, val:=rangemc.cfg.Params{
5252
switchparam{
5353
// Charset: character_set_connection, character_set_client, character_set_results
@@ -64,14 +64,23 @@ func (mc *mysqlConn) handleParams() (err error){
6464
return
6565
}
6666

67-
// Other system vars
67+
// Other system vars accumulated in a single SET command
6868
default:
69-
params=append(params, param+"="+val)
69+
ifcmdSet.Len() ==0{
70+
// Heuristic: 29 chars for each other key=value to reduce reallocations
71+
cmdSet.Grow(4+len(param) +1+len(val) +30*(len(mc.cfg.Params)-1))
72+
cmdSet.WriteString("SET ")
73+
} else{
74+
cmdSet.WriteByte(',')
75+
}
76+
cmdSet.WriteString(param)
77+
cmdSet.WriteByte('=')
78+
cmdSet.WriteString(val)
7079
}
7180
}
7281

73-
iflen(params) >0{
74-
err=mc.exec("SET "+strings.Join(params, ","))
82+
ifcmdSet.Len() >0{
83+
err=mc.exec(cmdSet.String())
7584
iferr!=nil{
7685
return
7786
}

0 commit comments

Comments
(0)