redis client: Issue fewer write sys calls by fill hspatial request into a temp buffer first and write it in one go.

This commit is contained in:
Sascha L. Teichmann 2017-03-06 11:44:06 +01:00
parent a074eeb54b
commit 2cba483d32

View File

@ -18,7 +18,7 @@ type RedisClient struct {
conn net.Conn conn net.Conn
reader *bufio.Reader reader *bufio.Reader
arena []byte arena []byte
scratch [80]byte scratch [130]byte
} }
func NewRedisClient(network, address string) (client *RedisClient, err error) { func NewRedisClient(network, address string) (client *RedisClient, err error) {
@ -42,34 +42,25 @@ var (
ignore = []byte("IGNORE") ignore = []byte("IGNORE")
) )
func (client *RedisClient) writeBulkString(buf []byte, data []byte) (err error) { func writeBulkString(buf []byte, data []byte) []byte {
buf = append(buf, '$') buf = append(buf, '$')
buf = strconv.AppendInt(buf, int64(len(data)), 10) buf = strconv.AppendInt(buf, int64(len(data)), 10)
buf = append(buf, nl...) buf = append(buf, nl...)
buf = append(buf, data...) buf = append(buf, data...)
buf = append(buf, nl...) buf = append(buf, nl...)
_, err = client.conn.Write(buf) return buf
return
} }
func (client *RedisClient) writeHSpatial(p1, p2 int64) (err error) { func (client *RedisClient) writeHSpatial(p1, p2 int64) error {
if _, err = client.conn.Write(writeArray4); err != nil { tmp := client.scratch[:0:40]
return buf := client.scratch[40:40]
} buf = append(buf, writeArray4...)
b1 := client.scratch[:0:40] buf = writeBulkString(buf, hspatial)
b2 := client.scratch[40:40:80] buf = writeBulkString(buf, ignore)
buf = writeBulkString(buf, keyToBytes(p1, tmp))
if err = client.writeBulkString(b1, hspatial); err != nil { buf = writeBulkString(buf, keyToBytes(p2, tmp))
return _, err := client.conn.Write(buf)
} return err
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 isError(line []byte) error { func isError(line []byte) error {