mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +03:00
Use single read and write buffer where possible
This commit is contained in:
@ -7,23 +7,50 @@ import (
|
||||
)
|
||||
|
||||
type WriteBuffer struct {
|
||||
b []byte
|
||||
rb *BufioReader
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func NewWriteBuffer() *WriteBuffer {
|
||||
func NewWriteBuffer(rb *BufioReader) *WriteBuffer {
|
||||
return &WriteBuffer{
|
||||
b: make([]byte, 0, 4096),
|
||||
rb: rb,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) Len() int { return len(w.b) }
|
||||
func (w *WriteBuffer) Bytes() []byte { return w.b }
|
||||
func (w *WriteBuffer) Reset() { w.b = w.b[:0] }
|
||||
func (w *WriteBuffer) Len() int {
|
||||
return len(w.buf)
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) Bytes() []byte {
|
||||
return w.buf
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) AllocBuffer() {
|
||||
w.rb = nil
|
||||
w.buf = make([]byte, defaultBufSize)
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) Reset() {
|
||||
if w.rb != nil {
|
||||
w.buf = w.rb.Buffer()[:0]
|
||||
} else {
|
||||
w.buf = w.buf[:0]
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) Flush() []byte {
|
||||
b := w.buf
|
||||
if w.rb != nil {
|
||||
w.rb.ResetBuffer(w.buf[:cap(w.buf)])
|
||||
w.buf = nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) Append(args []interface{}) error {
|
||||
w.b = append(w.b, ArrayReply)
|
||||
w.b = strconv.AppendUint(w.b, uint64(len(args)), 10)
|
||||
w.b = append(w.b, '\r', '\n')
|
||||
w.buf = append(w.buf, ArrayReply)
|
||||
w.buf = strconv.AppendUint(w.buf, uint64(len(args)), 10)
|
||||
w.buf = append(w.buf, '\r', '\n')
|
||||
|
||||
for _, arg := range args {
|
||||
if err := w.append(arg); err != nil {
|
||||
@ -85,19 +112,19 @@ func (w *WriteBuffer) append(val interface{}) error {
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) AppendString(s string) {
|
||||
w.b = append(w.b, StringReply)
|
||||
w.b = strconv.AppendUint(w.b, uint64(len(s)), 10)
|
||||
w.b = append(w.b, '\r', '\n')
|
||||
w.b = append(w.b, s...)
|
||||
w.b = append(w.b, '\r', '\n')
|
||||
w.buf = append(w.buf, StringReply)
|
||||
w.buf = strconv.AppendUint(w.buf, uint64(len(s)), 10)
|
||||
w.buf = append(w.buf, '\r', '\n')
|
||||
w.buf = append(w.buf, s...)
|
||||
w.buf = append(w.buf, '\r', '\n')
|
||||
}
|
||||
|
||||
func (w *WriteBuffer) AppendBytes(p []byte) {
|
||||
w.b = append(w.b, StringReply)
|
||||
w.b = strconv.AppendUint(w.b, uint64(len(p)), 10)
|
||||
w.b = append(w.b, '\r', '\n')
|
||||
w.b = append(w.b, p...)
|
||||
w.b = append(w.b, '\r', '\n')
|
||||
w.buf = append(w.buf, StringReply)
|
||||
w.buf = strconv.AppendUint(w.buf, uint64(len(p)), 10)
|
||||
w.buf = append(w.buf, '\r', '\n')
|
||||
w.buf = append(w.buf, p...)
|
||||
w.buf = append(w.buf, '\r', '\n')
|
||||
}
|
||||
|
||||
func formatInt(n int64) string {
|
||||
|
Reference in New Issue
Block a user