Skip to content

Commit

Permalink
Reuse buffer for constructing request.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Aug 14, 2012
1 parent 625adac commit a2f5423
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 44 deletions.
10 changes: 6 additions & 4 deletions connpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
)

type Conn struct {
RW io.ReadWriteCloser
Rd *bufio.Reader
RW io.ReadWriteCloser
Rd *bufio.Reader
ReqBuf []byte
}

func NewConn(rw io.ReadWriteCloser) *Conn {
return &Conn{
RW: rw,
Rd: bufio.NewReaderSize(rw, 1024),
RW: rw,
Rd: bufio.NewReaderSize(rw, 1024),
ReqBuf: make([]byte, 0, 1024),
}
}

Expand Down
26 changes: 9 additions & 17 deletions multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@ func (c *MultiClient) Unwatch(keys ...string) *StatusReq {

func (c *MultiClient) Discard() {
c.mtx.Lock()
c.reqs = c.reqs[:0]
c.reqs = []Req{NewStatusReq("MULTI")}
c.mtx.Unlock()
}

func (c *MultiClient) Exec(do func()) ([]Req, error) {
c.mtx.Lock()
c.reqs = make([]Req, 0)
c.mtx.Unlock()
c.Discard()

do()

c.mtx.Lock()
if len(c.reqs) == 0 {
c.reqs = append(c.reqs, NewMultiBulkReq("EXEC"))
if len(c.reqs) == 2 {
c.mtx.Unlock()
return c.reqs, nil
return []Req{}, nil
}
reqs := c.reqs
c.reqs = nil
Expand All @@ -67,18 +66,11 @@ func (c *MultiClient) Exec(do func()) ([]Req, error) {
}

c.ConnPool.Add(conn)
return reqs, nil
return reqs[1 : len(reqs)-1], nil
}

func (c *MultiClient) ExecReqs(reqs []Req, conn *Conn) error {
multiReq := make([]byte, 0, 1024)
multiReq = append(multiReq, PackReq([]string{"MULTI"})...)
for _, req := range reqs {
multiReq = append(multiReq, req.Req()...)
}
multiReq = append(multiReq, PackReq([]string{"EXEC"})...)

err := c.WriteReq(multiReq, conn)
err := c.WriteReq(conn, reqs...)
if err != nil {
return err
}
Expand All @@ -92,7 +84,7 @@ func (c *MultiClient) ExecReqs(reqs []Req, conn *Conn) error {
}

// Parse queued replies.
for _ = range reqs {
for i := 1; i < len(reqs)-1; i++ {
_, err = statusReq.ParseReply(conn.Rd)
if err != nil {
return err
Expand All @@ -112,7 +104,7 @@ func (c *MultiClient) ExecReqs(reqs []Req, conn *Conn) error {
}

// Parse replies.
for i := 0; i < len(reqs); i++ {
for i := 1; i < len(reqs)-1; i++ {
req := reqs[i]
val, err := req.ParseReply(conn.Rd)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ var (

//------------------------------------------------------------------------------

func PackReq(args []string) []byte {
buf := make([]byte, 0, 1024)
func AppendReq(buf []byte, args []string) []byte {
buf = append(buf, '*')
buf = strconv.AppendUint(buf, uint64(len(args)), 10)
buf = append(buf, '\r', '\n')
Expand Down
12 changes: 1 addition & 11 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,7 @@ func (c *PipelineClient) RunQueued() ([]Req, error) {
}

func (c *PipelineClient) RunReqs(reqs []Req, conn *Conn) error {
var multiReq []byte
if len(reqs) == 1 {
multiReq = reqs[0].Req()
} else {
multiReq = make([]byte, 0, 1024)
for _, req := range reqs {
multiReq = append(multiReq, req.Req()...)
}
}

err := c.WriteReq(multiReq, conn)
err := c.WriteReq(conn, reqs...)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *PubSubClient) subscribe(cmd string, channels ...string) (chan *Message,
return nil, err
}

if err := c.WriteReq(req.Req(), conn); err != nil {
if err := c.WriteReq(conn, req); err != nil {
return nil, err
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (c *PubSubClient) unsubscribe(cmd string, channels ...string) error {
return err
}

return c.WriteReq(req.Req(), conn)
return c.WriteReq(conn, req)
}

func (c *PubSubClient) Unsubscribe(channels ...string) error {
Expand Down
11 changes: 8 additions & 3 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ type BaseClient struct {
reqs []Req
}

func (c *BaseClient) WriteReq(buf []byte, conn *Conn) error {
_, err := conn.RW.Write(buf)
func (c *BaseClient) WriteReq(conn *Conn, reqs ...Req) error {
conn.ReqBuf = conn.ReqBuf[:0]
for _, req := range reqs {
conn.ReqBuf = AppendReq(conn.ReqBuf, req.Args())
}

_, err := conn.RW.Write(conn.ReqBuf)
return err
}

Expand Down Expand Up @@ -96,7 +101,7 @@ func (c *BaseClient) Run(req Req) {
return
}

err = c.WriteReq(req.Req(), conn)
err = c.WriteReq(conn, req)
if err != nil {
c.ConnPool.Remove(conn)
req.SetErr(err)
Expand Down
4 changes: 2 additions & 2 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,7 @@ func (t *RedisTest) BenchmarkRedisWriteRead(c *C) {
c.Assert(err, IsNil)

for i := 0; i < 10; i++ {
err := t.client.WriteReq([]byte("PING\r\n"), conn)
err := t.client.WriteReq(conn, redis.NewStatusReq("PING"))
c.Assert(err, IsNil)

line, _, err := conn.Rd.ReadLine()
Expand All @@ -2423,7 +2423,7 @@ func (t *RedisTest) BenchmarkRedisWriteRead(c *C) {
c.StartTimer()

for i := 0; i < c.N; i++ {
t.client.WriteReq([]byte("PING\r\n"), conn)
t.client.WriteReq(conn, redis.NewStatusReq("PING"))
conn.Rd.ReadLine()
}

Expand Down
6 changes: 3 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type Req interface {
Req() []byte
Args() []string
ParseReply(ReadLiner) (interface{}, error)
SetErr(error)
Err() error
Expand All @@ -28,8 +28,8 @@ func NewBaseReq(args ...string) *BaseReq {
}
}

func (r *BaseReq) Req() []byte {
return PackReq(r.args)
func (r *BaseReq) Args() []string {
return r.args
}

func (r *BaseReq) SetErr(err error) {
Expand Down

0 comments on commit a2f5423

Please sign in to comment.