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

View File

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