From f80d9e452c9a1e03386697f59bb97204d8e08900 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Wed, 27 May 2015 16:48:51 +0200 Subject: [PATCH] Don't use fmt.Sprintf when sending size of a redis bulk string. --- cmd/mtredisalize/connection.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/mtredisalize/connection.go b/cmd/mtredisalize/connection.go index 489ff0f..78aae99 100644 --- a/cmd/mtredisalize/connection.go +++ b/cmd/mtredisalize/connection.go @@ -6,9 +6,9 @@ package main import ( "bufio" - "fmt" "log" "net" + "strconv" ) var ( @@ -121,7 +121,7 @@ func (c *Connection) Hkeys(hash []byte) bool { return c.writeEmptyArray() } - if _, err := c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", n))); err != nil { + if _, err := c.conn.Write(redisLength('*', n)); err != nil { logError(err) return false } @@ -204,8 +204,13 @@ func (c *Connection) writeBool(b bool) bool { return true } +func redisLength(prefix byte, s int) []byte { + buf := append(make([]byte, 0, 16), prefix) + return append(strconv.AppendInt(buf, int64(s), 10), '\r', '\n') +} + func (c *Connection) writeBoolArray(arr []bool) bool { - if _, err := c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", len(arr)))); err != nil { + if _, err := c.conn.Write(redisLength('*', len(arr))); err != nil { logError(err) return false } @@ -246,7 +251,7 @@ func (c *Connection) writeBulkString(data []byte) (err error) { if data == nil { _, err = con.Write(redisNoSuchBlock) } else { - if _, err = con.Write([]byte(fmt.Sprintf("$%d\r\n", len(data)))); err != nil { + if _, err = con.Write(redisLength('$', len(data))); err != nil { return } if _, err = con.Write(data); err != nil {