From 2e3402d33d8c2d63d027edf75d63592eb10cbe01 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 14 Feb 2020 12:44:03 +0200 Subject: [PATCH] Don't allocate tmp slice in txPipelineWriteMulti --- cluster.go | 2 +- command.go | 9 ++++++--- redis.go | 22 ++++++++++++++++------ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cluster.go b/cluster.go index c5fcb9bb..12721c3f 100644 --- a/cluster.go +++ b/cluster.go @@ -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 := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error { - return writeCmd(wr, cmds...) + return writeCmds(wr, cmds) }) if err != nil { return err diff --git a/command.go b/command.go index 0a4a345d..8c5fbe24 100644 --- a/command.go +++ b/command.go @@ -41,16 +41,19 @@ func cmdsFirstErr(cmds []Cmder) error { return nil } -func writeCmd(wr *proto.Writer, cmds ...Cmder) error { +func writeCmds(wr *proto.Writer, cmds []Cmder) error { for _, cmd := range cmds { - err := wr.WriteArgs(cmd.Args()) - if err != nil { + if err := writeCmd(wr, cmd); err != nil { return err } } return nil } +func writeCmd(wr *proto.Writer, cmd Cmder) error { + return wr.WriteArgs(cmd.Args()) +} + func cmdString(cmd Cmder, val interface{}) string { ss := make([]string, 0, len(cmd.Args())) for _, arg := range cmd.Args() { diff --git a/redis.go b/redis.go index 2b50fff0..caba631b 100644 --- a/redis.go +++ b/redis.go @@ -411,7 +411,7 @@ func (c *baseClient) pipelineProcessCmds( ctx context.Context, cn *pool.Conn, cmds []Cmder, ) (bool, error) { err := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error { - return writeCmd(wr, cmds...) + return writeCmds(wr, cmds) }) if err != nil { return true, err @@ -453,12 +453,22 @@ func (c *baseClient) txPipelineProcessCmds( return false, err } +var ( + multi = NewStatusCmd("multi") + exec = NewSliceCmd("exec") +) + func txPipelineWriteMulti(wr *proto.Writer, cmds []Cmder) error { - multiExec := make([]Cmder, 0, len(cmds)+2) - multiExec = append(multiExec, NewStatusCmd("MULTI")) - multiExec = append(multiExec, cmds...) - multiExec = append(multiExec, NewSliceCmd("EXEC")) - return writeCmd(wr, multiExec...) + if err := writeCmd(wr, multi); err != nil { + return err + } + if err := writeCmds(wr, cmds); err != nil { + return err + } + if err := writeCmd(wr, exec); err != nil { + return err + } + return nil } func txPipelineReadQueued(rd *proto.Reader, cmds []Cmder) error {