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
1 changed files with 13 additions and 22 deletions

View File

@ -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 {