1
0
mirror of https://github.com/redis/go-redis.git synced 2025-08-01 16:06:54 +03:00

Reduce number of allocations.

This commit is contained in:
Vladimir Mihailenco
2017-01-13 13:39:59 +02:00
parent b9cc17bae0
commit 69554c0ec5
11 changed files with 158 additions and 92 deletions

View File

@ -3,90 +3,89 @@ package proto
import (
"encoding"
"fmt"
"strconv"
"gopkg.in/redis.v5/internal"
)
func Scan(s string, v interface{}) error {
func Scan(b []byte, v interface{}) error {
switch v := v.(type) {
case nil:
return internal.RedisError("redis: Scan(nil)")
case *string:
*v = s
*v = internal.BytesToString(b)
return nil
case *[]byte:
*v = []byte(s)
*v = b
return nil
case *int:
var err error
*v, err = strconv.Atoi(s)
*v, err = atoi(b)
return err
case *int8:
n, err := strconv.ParseInt(s, 10, 8)
n, err := parseInt(b, 10, 8)
if err != nil {
return err
}
*v = int8(n)
return nil
case *int16:
n, err := strconv.ParseInt(s, 10, 16)
n, err := parseInt(b, 10, 16)
if err != nil {
return err
}
*v = int16(n)
return nil
case *int32:
n, err := strconv.ParseInt(s, 10, 32)
n, err := parseInt(b, 10, 32)
if err != nil {
return err
}
*v = int32(n)
return nil
case *int64:
n, err := strconv.ParseInt(s, 10, 64)
n, err := parseInt(b, 10, 64)
if err != nil {
return err
}
*v = n
return nil
case *uint:
n, err := strconv.ParseUint(s, 10, 64)
n, err := parseUint(b, 10, 64)
if err != nil {
return err
}
*v = uint(n)
return nil
case *uint8:
n, err := strconv.ParseUint(s, 10, 8)
n, err := parseUint(b, 10, 8)
if err != nil {
return err
}
*v = uint8(n)
return nil
case *uint16:
n, err := strconv.ParseUint(s, 10, 16)
n, err := parseUint(b, 10, 16)
if err != nil {
return err
}
*v = uint16(n)
return nil
case *uint32:
n, err := strconv.ParseUint(s, 10, 32)
n, err := parseUint(b, 10, 32)
if err != nil {
return err
}
*v = uint32(n)
return nil
case *uint64:
n, err := strconv.ParseUint(s, 10, 64)
n, err := parseUint(b, 10, 64)
if err != nil {
return err
}
*v = n
return nil
case *float32:
n, err := strconv.ParseFloat(s, 32)
n, err := parseFloat(b, 32)
if err != nil {
return err
}
@ -94,13 +93,13 @@ func Scan(s string, v interface{}) error {
return err
case *float64:
var err error
*v, err = strconv.ParseFloat(s, 64)
*v, err = parseFloat(b, 64)
return err
case *bool:
*v = len(s) == 1 && s[0] == '1'
*v = len(b) == 1 && b[0] == '1'
return nil
case encoding.BinaryUnmarshaler:
return v.UnmarshalBinary([]byte(s))
return v.UnmarshalBinary(b)
default:
return fmt.Errorf(
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)