mirror of https://github.com/go-redis/redis.git
Speedup ScanSlice
This commit is contained in:
parent
3802e09b42
commit
f29951c899
|
@ -122,7 +122,7 @@ func ScanSlice(data []string, slice interface{}) error {
|
|||
|
||||
for i, s := range data {
|
||||
elem := internal.SliceNextElem(v)
|
||||
if err := Scan([]byte(s), elem.Addr().Interface()); err != nil {
|
||||
if err := Scan(internal.StringToBytes(s), elem.Addr().Interface()); err != nil {
|
||||
return fmt.Errorf("redis: ScanSlice(index=%d value=%q) failed: %s", i, s, err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,3 +5,7 @@ package internal
|
|||
func BytesToString(b []byte) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func StringToBytes(s string) []byte {
|
||||
return []byte(s)
|
||||
}
|
||||
|
|
|
@ -9,6 +9,19 @@ import (
|
|||
|
||||
func BytesToString(b []byte) string {
|
||||
bytesHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||
strHeader := reflect.StringHeader{bytesHeader.Data, bytesHeader.Len}
|
||||
strHeader := reflect.StringHeader{
|
||||
Data: bytesHeader.Data,
|
||||
Len: bytesHeader.Len,
|
||||
}
|
||||
return *(*string)(unsafe.Pointer(&strHeader))
|
||||
}
|
||||
|
||||
func StringToBytes(s string) []byte {
|
||||
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||
bh := reflect.SliceHeader{
|
||||
Data: sh.Data,
|
||||
Len: sh.Len,
|
||||
Cap: sh.Len,
|
||||
}
|
||||
return *(*[]byte)(unsafe.Pointer(&bh))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue