Replaced one sync.Pool with a scratch byte arrary in redis client.

This commit is contained in:
Sascha L. Teichmann 2017-03-02 10:57:03 +01:00
parent 956f7b5e8b
commit 41187af7db
1 changed files with 13 additions and 21 deletions

View File

@ -16,8 +16,9 @@ import (
)
type RedisClient struct {
conn net.Conn
reader *bufio.Reader
conn net.Conn
reader *bufio.Reader
scratch [80]byte
}
func NewRedisClient(network, address string) (client *RedisClient, err error) {
@ -41,14 +42,6 @@ var (
ignore = []byte("IGNORE")
)
var bufPool = sync.Pool{
New: func() interface{} {
// Some fun with slices
buf := make([]byte, 40)
return [2][]byte{buf[:20:20][:0], buf[20:][:0]}
},
}
func (client *RedisClient) writeBulkString(buf []byte, data []byte) (err error) {
buf = append(buf, '$')
buf = strconv.AppendInt(buf, int64(len(data)), 10)
@ -63,20 +56,19 @@ func (client *RedisClient) writeHSpatial(p1, p2 int64) (err error) {
if _, err = client.conn.Write(writeArray4); err != nil {
return
}
buf := bufPool.Get().([2][]byte)
// defer bufPool.Put(buf) // not used to avoid allocation.
if err = client.writeBulkString(buf[0], hspatial); err != nil {
goto exit
b1 := client.scratch[:0:40]
b2 := client.scratch[40:40:80]
if err = client.writeBulkString(b1, hspatial); err != nil {
return
}
if err = client.writeBulkString(buf[0], ignore); err != nil {
goto exit
if err = client.writeBulkString(b1, ignore); err != nil {
return
}
if err = client.writeBulkString(buf[0], keyToBytes(p1, buf[1])); err != nil {
goto exit
if err = client.writeBulkString(b1, keyToBytes(p1, b2)); err != nil {
return
}
err = client.writeBulkString(buf[0], keyToBytes(p2, buf[1]))
exit:
bufPool.Put(buf)
err = client.writeBulkString(b1, keyToBytes(p2, b2))
return
}