From 2cba483d325debc5a023c204a29c746c71101c0b Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 6 Mar 2017 11:44:06 +0100 Subject: [PATCH] redis client: Issue fewer write sys calls by fill hspatial request into a temp buffer first and write it in one go. --- common/redisclient.go | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/common/redisclient.go b/common/redisclient.go index 7a8e48e..23c04bb 100644 --- a/common/redisclient.go +++ b/common/redisclient.go @@ -18,7 +18,7 @@ type RedisClient struct { conn net.Conn reader *bufio.Reader arena []byte - scratch [80]byte + scratch [130]byte } func NewRedisClient(network, address string) (client *RedisClient, err error) { @@ -42,34 +42,25 @@ var ( ignore = []byte("IGNORE") ) -func (client *RedisClient) writeBulkString(buf []byte, data []byte) (err error) { +func writeBulkString(buf []byte, data []byte) []byte { buf = append(buf, '$') buf = strconv.AppendInt(buf, int64(len(data)), 10) buf = append(buf, nl...) buf = append(buf, data...) buf = append(buf, nl...) - _, err = client.conn.Write(buf) - return + return buf } -func (client *RedisClient) writeHSpatial(p1, p2 int64) (err error) { - if _, err = client.conn.Write(writeArray4); err != nil { - return - } - b1 := client.scratch[:0:40] - b2 := client.scratch[40:40:80] - - if err = client.writeBulkString(b1, hspatial); err != nil { - return - } - if err = client.writeBulkString(b1, ignore); err != nil { - return - } - if err = client.writeBulkString(b1, keyToBytes(p1, b2)); err != nil { - return - } - err = client.writeBulkString(b1, keyToBytes(p2, b2)) - return +func (client *RedisClient) writeHSpatial(p1, p2 int64) error { + tmp := client.scratch[:0:40] + buf := client.scratch[40:40] + buf = append(buf, writeArray4...) + buf = writeBulkString(buf, hspatial) + buf = writeBulkString(buf, ignore) + buf = writeBulkString(buf, keyToBytes(p1, tmp)) + buf = writeBulkString(buf, keyToBytes(p2, tmp)) + _, err := client.conn.Write(buf) + return err } func isError(line []byte) error {