lil perf boost

This commit is contained in:
Josh Baker 2017-11-09 05:50:32 -07:00
parent 3df12143a4
commit b6e65d498d
1 changed files with 14 additions and 12 deletions

View File

@ -229,6 +229,16 @@ func readTelnetCommand(packet []byte, argsbuf [][]byte) (
return false, args[:0], Telnet, packet, nil return false, args[:0], Telnet, packet, nil
} }
// appendPrefix will append a "$3\r\n" style redis prefix for a message.
func appendPrefix(b []byte, c byte, n int64) []byte {
if n >= 0 && n <= 9 {
return append(b, c, byte('0'+n), '\r', '\n')
}
b = append(b, c)
b = strconv.AppendInt(b, n, 10)
return append(b, '\r', '\n')
}
// AppendUint appends a Redis protocol uint64 to the input bytes. // AppendUint appends a Redis protocol uint64 to the input bytes.
func AppendUint(b []byte, n uint64) []byte { func AppendUint(b []byte, n uint64) []byte {
b = append(b, ':') b = append(b, ':')
@ -238,32 +248,24 @@ func AppendUint(b []byte, n uint64) []byte {
// AppendInt appends a Redis protocol int64 to the input bytes. // AppendInt appends a Redis protocol int64 to the input bytes.
func AppendInt(b []byte, n int64) []byte { func AppendInt(b []byte, n int64) []byte {
b = append(b, ':') return appendPrefix(b, ':', n)
b = strconv.AppendInt(b, n, 10)
return append(b, '\r', '\n')
} }
// AppendArray appends a Redis protocol array to the input bytes. // AppendArray appends a Redis protocol array to the input bytes.
func AppendArray(b []byte, n int) []byte { func AppendArray(b []byte, n int) []byte {
b = append(b, '*') return appendPrefix(b, '*', int64(n))
b = strconv.AppendInt(b, int64(n), 10)
return append(b, '\r', '\n')
} }
// AppendBulk appends a Redis protocol bulk byte slice to the input bytes. // AppendBulk appends a Redis protocol bulk byte slice to the input bytes.
func AppendBulk(b []byte, bulk []byte) []byte { func AppendBulk(b []byte, bulk []byte) []byte {
b = append(b, '$') b = appendPrefix(b, '$', int64(len(bulk)))
b = strconv.AppendInt(b, int64(len(bulk)), 10)
b = append(b, '\r', '\n')
b = append(b, bulk...) b = append(b, bulk...)
return append(b, '\r', '\n') return append(b, '\r', '\n')
} }
// AppendBulkString appends a Redis protocol bulk string to the input bytes. // AppendBulkString appends a Redis protocol bulk string to the input bytes.
func AppendBulkString(b []byte, bulk string) []byte { func AppendBulkString(b []byte, bulk string) []byte {
b = append(b, '$') b = appendPrefix(b, '$', int64(len(bulk)))
b = strconv.AppendInt(b, int64(len(bulk)), 10)
b = append(b, '\r', '\n')
b = append(b, bulk...) b = append(b, bulk...)
return append(b, '\r', '\n') return append(b, '\r', '\n')
} }