Skip to content

Commit

Permalink
Don't allocate tmp slice in txPipelineWriteMulti
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Feb 14, 2020
1 parent c01b1dc commit 2e3402d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 16 additions & 6 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2e3402d

Please sign in to comment.