Skip to content

Commit

Permalink
perf: improve the speed of parsing a resp integer
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Jan 25, 2024
1 parent e869b8b commit c0ce698
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
46 changes: 21 additions & 25 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,30 @@ func readS(i *bufio.Reader) (string, error) {
return BinaryString(bs), nil
}

func readI(i *bufio.Reader) (int64, error) {
var v int64
var neg bool
for {
c, err := i.ReadByte()
if err != nil {
return 0, err
}
switch {
case c >= '0' && c <= '9':
v = v*10 + int64(c-'0')
case c == '\r':
_, err = i.Discard(1)
if neg {
return v * -1, err
}
return v, err
case c == '-':
neg = true
case c == '?':
if _, err = i.Discard(2); err == nil {
err = errChunked
}
return 0, err
default:
func readI(i *bufio.Reader) (v int64, err error) {
bs, err := i.ReadSlice('\n')
if err != nil {
return 0, err
}
if len(bs) < 3 {
return 0, errors.New(unexpectedNoCRLF)
}
if bs[0] == '?' {
return 0, errChunked
}
var s = int64(1)
if bs[0] == '-' {
s = -1
bs = bs[1:]
}
for _, c := range bs[:len(bs)-2] {
if d := int64(c - '0'); d >= 0 && d <= 9 {
v = v*10 + d
} else {
return 0, errors.New(unexpectedNumByte + strconv.Itoa(int(c)))
}
}
return v * s, nil
}

func readB(i *bufio.Reader) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions rueidis.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ type AuthCredentials struct {
// It will first try to connect as cluster client. If the len(ClientOption.InitAddress) == 1 and
// the address does not enable cluster mode, the NewClient() will use single client instead.
func NewClient(option ClientOption) (client Client, err error) {
if option.ReadBufferEachConn <= 0 {
if option.ReadBufferEachConn < 32 { // the buffer should be able to hold an int64 string at least
option.ReadBufferEachConn = DefaultReadBuffer
}
if option.WriteBufferEachConn <= 0 {
if option.WriteBufferEachConn < 32 {
option.WriteBufferEachConn = DefaultWriteBuffer
}
if option.CacheSizeEachConn <= 0 {
Expand Down

0 comments on commit c0ce698

Please sign in to comment.