Don't allocate tmp slice in txPipelineWriteMulti

This commit is contained in:
Vladimir Mihailenco 2020-02-14 12:44:03 +02:00
parent c01b1dcab8
commit 2e3402d33d
3 changed files with 23 additions and 10 deletions

View File

@ -1070,7 +1070,7 @@ func (c *ClusterClient) _processPipeline(ctx context.Context, cmds []Cmder) erro
err := node.Client.withConn(ctx, func(ctx context.Context, cn *pool.Conn) error { err := node.Client.withConn(ctx, func(ctx context.Context, cn *pool.Conn) error {
err := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error { err := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error {
return writeCmd(wr, cmds...) return writeCmds(wr, cmds)
}) })
if err != nil { if err != nil {
return err return err

View File

@ -41,16 +41,19 @@ func cmdsFirstErr(cmds []Cmder) error {
return nil return nil
} }
func writeCmd(wr *proto.Writer, cmds ...Cmder) error { func writeCmds(wr *proto.Writer, cmds []Cmder) error {
for _, cmd := range cmds { for _, cmd := range cmds {
err := wr.WriteArgs(cmd.Args()) if err := writeCmd(wr, cmd); err != nil {
if err != nil {
return err return err
} }
} }
return nil return nil
} }
func writeCmd(wr *proto.Writer, cmd Cmder) error {
return wr.WriteArgs(cmd.Args())
}
func cmdString(cmd Cmder, val interface{}) string { func cmdString(cmd Cmder, val interface{}) string {
ss := make([]string, 0, len(cmd.Args())) ss := make([]string, 0, len(cmd.Args()))
for _, arg := range cmd.Args() { for _, arg := range cmd.Args() {

View File

@ -411,7 +411,7 @@ func (c *baseClient) pipelineProcessCmds(
ctx context.Context, cn *pool.Conn, cmds []Cmder, ctx context.Context, cn *pool.Conn, cmds []Cmder,
) (bool, error) { ) (bool, error) {
err := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error { err := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error {
return writeCmd(wr, cmds...) return writeCmds(wr, cmds)
}) })
if err != nil { if err != nil {
return true, err return true, err
@ -453,12 +453,22 @@ func (c *baseClient) txPipelineProcessCmds(
return false, err return false, err
} }
var (
multi = NewStatusCmd("multi")
exec = NewSliceCmd("exec")
)
func txPipelineWriteMulti(wr *proto.Writer, cmds []Cmder) error { func txPipelineWriteMulti(wr *proto.Writer, cmds []Cmder) error {
multiExec := make([]Cmder, 0, len(cmds)+2) if err := writeCmd(wr, multi); err != nil {
multiExec = append(multiExec, NewStatusCmd("MULTI")) return err
multiExec = append(multiExec, cmds...) }
multiExec = append(multiExec, NewSliceCmd("EXEC")) if err := writeCmds(wr, cmds); err != nil {
return writeCmd(wr, multiExec...) return err
}
if err := writeCmd(wr, exec); err != nil {
return err
}
return nil
} }
func txPipelineReadQueued(rd *proto.Reader, cmds []Cmder) error { func txPipelineReadQueued(rd *proto.Reader, cmds []Cmder) error {