Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use zero copy improve string and byte convert #525

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions redis/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package redis
import (
"errors"
"fmt"
"reflect"
"strconv"
"time"
"unsafe"
)

// ErrNil indicates that a reply value is nil.
Expand All @@ -45,7 +47,7 @@ func Int(reply interface{}, err error) (int, error) {
}
return x, nil
case []byte:
n, err := strconv.ParseInt(string(reply), 10, 0)
n, err := strconv.ParseInt(bytesToString(reply), 10, 0)
return int(n), err
case nil:
return 0, ErrNil
Expand All @@ -72,7 +74,7 @@ func Int64(reply interface{}, err error) (int64, error) {
case int64:
return reply, nil
case []byte:
n, err := strconv.ParseInt(string(reply), 10, 64)
n, err := strconv.ParseInt(bytesToString(reply), 10, 64)
return n, err
case nil:
return 0, ErrNil
Expand Down Expand Up @@ -106,7 +108,7 @@ func Uint64(reply interface{}, err error) (uint64, error) {
}
return uint64(reply), nil
case []byte:
n, err := strconv.ParseUint(string(reply), 10, 64)
n, err := strconv.ParseUint(bytesToString(reply), 10, 64)
return n, err
case nil:
return 0, ErrNil
Expand All @@ -130,7 +132,7 @@ func Float64(reply interface{}, err error) (float64, error) {
}
switch reply := reply.(type) {
case []byte:
n, err := strconv.ParseFloat(string(reply), 64)
n, err := strconv.ParseFloat(bytesToString(reply), 64)
return n, err
case nil:
return 0, ErrNil
Expand All @@ -155,7 +157,7 @@ func String(reply interface{}, err error) (string, error) {
}
switch reply := reply.(type) {
case []byte:
return string(reply), nil
return bytesToString(reply), nil
case string:
return reply, nil
case nil:
Expand Down Expand Up @@ -183,7 +185,7 @@ func Bytes(reply interface{}, err error) ([]byte, error) {
case []byte:
return reply, nil
case string:
return []byte(reply), nil
return stringToBytes(reply), nil
case nil:
return nil, ErrNil
case Error:
Expand Down Expand Up @@ -281,7 +283,7 @@ func Float64s(reply interface{}, err error) ([]float64, error) {
if !ok {
return fmt.Errorf("redigo: unexpected element type for Floats64, got type %T", v)
}
f, err := strconv.ParseFloat(string(p), 64)
f, err := strconv.ParseFloat(bytesToString(p), 64)
result[i] = f
return err
})
Expand All @@ -300,7 +302,7 @@ func Strings(reply interface{}, err error) ([]string, error) {
result[i] = v
return nil
case []byte:
result[i] = string(v)
result[i] = bytesToString(v)
return nil
default:
return fmt.Errorf("redigo: unexpected element type for Strings, got type %T", v)
Expand Down Expand Up @@ -338,7 +340,7 @@ func Int64s(reply interface{}, err error) ([]int64, error) {
result[i] = v
return nil
case []byte:
n, err := strconv.ParseInt(string(v), 10, 64)
n, err := strconv.ParseInt(bytesToString(v), 10, 64)
result[i] = n
return err
default:
Expand All @@ -364,7 +366,7 @@ func Ints(reply interface{}, err error) ([]int, error) {
result[i] = n
return nil
case []byte:
n, err := strconv.Atoi(string(v))
n, err := strconv.Atoi(bytesToString(v))
result[i] = n
return err
default:
Expand Down Expand Up @@ -392,7 +394,7 @@ func StringMap(result interface{}, err error) (map[string]string, error) {
if !okKey || !okValue {
return nil, errors.New("redigo: StringMap key not a bulk string value")
}
m[string(key)] = string(value)
m[bytesToString(key)] = bytesToString(value)
}
return m, nil
}
Expand All @@ -418,7 +420,7 @@ func IntMap(result interface{}, err error) (map[string]int, error) {
if err != nil {
return nil, err
}
m[string(key)] = value
m[bytesToString(key)] = value
}
return m, nil
}
Expand All @@ -444,7 +446,7 @@ func Int64Map(result interface{}, err error) (map[string]int64, error) {
if err != nil {
return nil, err
}
m[string(key)] = value
m[bytesToString(key)] = value
}
return m, nil
}
Expand Down Expand Up @@ -493,7 +495,7 @@ func Uint64s(reply interface{}, err error) ([]uint64, error) {
result[i] = v
return nil
case []byte:
n, err := strconv.ParseUint(string(v), 10, 64)
n, err := strconv.ParseUint(bytesToString(v), 10, 64)
result[i] = n
return err
default:
Expand Down Expand Up @@ -524,7 +526,7 @@ func Uint64Map(result interface{}, err error) (map[string]uint64, error) {
if err != nil {
return nil, err
}
m[string(key)] = value
m[bytesToString(key)] = value
}
return m, nil
}
Expand Down Expand Up @@ -581,3 +583,15 @@ func SlowLogs(result interface{}, err error) ([]SlowLog, error) {
}
return logs, nil
}

func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

func stringToBytes(s string) []byte {
var b []byte
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
return b
}
32 changes: 32 additions & 0 deletions redis/reply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,35 @@ func ExampleString() {
// Output:
// "world"
}

func BenchmarkIntForByte(b *testing.B) {
b.StopTimer()
c, err := dial()
if err != nil {
fmt.Println(err)
return
}
c.Do("SET", "k1", "456")
reply, err := c.Do("GET", "k1")
defer c.Close()
b.StartTimer()
for i := 0; i < b.N; i++ {
redis.Int(reply, err)
}
}

func BenchmarkString(b *testing.B) {
b.StopTimer()
c, err := dial()
if err != nil {
fmt.Println(err)
return
}
c.Do("SET", "k1", "abcd")
reply, err := c.Do("GET", "k1")
defer c.Close()
b.StartTimer()
for i := 0; i < b.N; i++ {
redis.String(reply, err)
}
}