connect: reduce allocations when building SET command#1111
connect: reduce allocations when building SET command#1111julienschmidt merged 2 commits intogo-sql-driver:masterfrom
Conversation
connection.go
Outdated
| 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)) |
There was a problem hiding this comment.
Wouldn't it make sense to use either a const-sized buffer on the heap for the common case or try to use the mc.buf?
There was a problem hiding this comment.
A better memory saving would occur by building the SET NAMES and SET commands just once for all connections as the DSN is frozen, and store them in the connector. But this is more profound refactoring.
There was a problem hiding this comment.
#1115 is the first step to move the connection-independent parts of handleParams to a connector constructor.
methane
left a comment
There was a problem hiding this comment.
Please use strings.Builder.
https://golang.org/pkg/strings/#Builder
OK, |
|
@methane Code pushed using strings.Builder. |
9f37b88 to
acb481b
Compare
…1111) * connect: reduce allocations when building SET command * handleParams: use strings.Builder instead of direct []byte
…1111) * connect: reduce allocations when building SET command * handleParams: use strings.Builder instead of direct []byte
Description
As a follow-up to #1099 (see #1099 (comment)), here is an improvement of the
handleParamsfunction to reduce number of allocations when building theSETcommand to set session variables.Checklist