Skip to content

Commit

Permalink
perf: avoid unnecessary runtime.duffcopy when the compiler fails to i…
Browse files Browse the repository at this point in the history
…nline the helper function

Signed-off-by: Rueian <[email protected]>
  • Loading branch information
rueian committed Jan 5, 2025
1 parent d36d4f6 commit 98a7df8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 42 deletions.
24 changes: 12 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (c *singleClient) Do(ctx context.Context, cmd Completed) (resp RedisResult)
attempts := 1
retry:
resp = c.conn.Do(ctx, cmd)
if c.retry && cmd.IsReadOnly() && c.isRetryable(resp.Error(), ctx) {
if c.retry && cmd.IsReadOnly() && c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, cmd, resp.Error(),
ctx, attempts, cmd, resp.error(),
)
if shouldRetry {
attempts++
Expand Down Expand Up @@ -88,9 +88,9 @@ retry:
resps = c.conn.DoMulti(ctx, multi...).s
if c.retry && allReadOnly(multi) {
for i, resp := range resps {
if c.isRetryable(resp.Error(), ctx) {
if c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, multi[i], resp.Error(),
ctx, attempts, multi[i], resp.error(),
)
if shouldRetry {
attempts++
Expand All @@ -116,9 +116,9 @@ retry:
resps = c.conn.DoMultiCache(ctx, multi...).s
if c.retry {
for i, resp := range resps {
if c.isRetryable(resp.Error(), ctx) {
if c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, Completed(multi[i].Cmd), resp.Error(),
ctx, attempts, Completed(multi[i].Cmd), resp.error(),
)
if shouldRetry {
attempts++
Expand All @@ -139,8 +139,8 @@ func (c *singleClient) DoCache(ctx context.Context, cmd Cacheable, ttl time.Dura
attempts := 1
retry:
resp = c.conn.DoCache(ctx, cmd, ttl)
if c.retry && c.isRetryable(resp.Error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, Completed(cmd), resp.Error())
if c.retry && c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, Completed(cmd), resp.error())
if shouldRetry {
attempts++
goto retry
Expand Down Expand Up @@ -214,9 +214,9 @@ retry:
return newErrResult(err)
}
resp = c.wire.Do(ctx, cmd)
if c.retry && cmd.IsReadOnly() && isRetryable(resp.Error(), c.wire, ctx) {
if c.retry && cmd.IsReadOnly() && isRetryable(resp.error(), c.wire, ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, cmd, resp.Error(),
ctx, attempts, cmd, resp.error(),
)
if shouldRetry {
attempts++
Expand Down Expand Up @@ -244,9 +244,9 @@ retry:
}
resp = c.wire.DoMulti(ctx, multi...).s
for i, cmd := range multi {
if retryable && isRetryable(resp[i].Error(), c.wire, ctx) {
if retryable && isRetryable(resp[i].error(), c.wire, ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, multi[i], resp[i].Error(),
ctx, attempts, multi[i], resp[i].error(),
)
if shouldRetry {
attempts++
Expand Down
28 changes: 14 additions & 14 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (c *clusterClient) _refresh() (err error) {
}
}
result = <-results
err = result.reply.Error()
err = result.reply.error()
if len(result.reply.val.values) != 0 {
break
}
Expand Down Expand Up @@ -497,7 +497,7 @@ retry:
}
resp = cc.Do(ctx, cmd)
process:
switch addr, mode := c.shouldRefreshRetry(resp.Error(), ctx); mode {
switch addr, mode := c.shouldRefreshRetry(resp.error(), ctx); mode {
case RedirectMove:
resp = c.redirectOrNew(addr, cc, cmd.Slot(), mode).Do(ctx, cmd)
goto process
Expand All @@ -508,7 +508,7 @@ process:
goto process
case RedirectRetry:
if c.retry && cmd.IsReadOnly() {
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, cmd, resp.Error())
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, cmd, resp.error())
if shouldRetry {
attempts++
goto retry
Expand Down Expand Up @@ -663,15 +663,15 @@ func (c *clusterClient) doresultfn(
ii := cIndexes[i]
cm := commands[i]
results.s[ii] = resp
addr, mode := c.shouldRefreshRetry(resp.Error(), ctx)
addr, mode := c.shouldRefreshRetry(resp.error(), ctx)
if mode != RedirectNone {
nc := cc
retryDelay := time.Duration(-1)
if mode == RedirectRetry {
if !c.retry || !cm.IsReadOnly() {
continue
}
retryDelay = c.retryHandler.RetryDelay(attempts, cm, resp.Error())
retryDelay = c.retryHandler.RetryDelay(attempts, cm, resp.error())
} else {
nc = c.redirectOrNew(addr, cc, cm.Slot(), mode)
}
Expand Down Expand Up @@ -828,7 +828,7 @@ retry:
}
resp = cc.DoCache(ctx, cmd, ttl)
process:
switch addr, mode := c.shouldRefreshRetry(resp.Error(), ctx); mode {
switch addr, mode := c.shouldRefreshRetry(resp.error(), ctx); mode {
case RedirectMove:
resp = c.redirectOrNew(addr, cc, cmd.Slot(), mode).DoCache(ctx, cmd, ttl)
goto process
Expand All @@ -839,7 +839,7 @@ process:
goto process
case RedirectRetry:
if c.retry {
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, Completed(cmd), resp.Error())
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, Completed(cmd), resp.error())
if shouldRetry {
attempts++
goto retry
Expand Down Expand Up @@ -890,7 +890,7 @@ func askingMultiCache(cc conn, ctx context.Context, multi []CacheableTTL) *redis
resps := cc.DoMulti(ctx, commands...)
for i := 5; i < len(resps.s); i += 6 {
if arr, err := resps.s[i].ToArray(); err != nil {
if preErr := resps.s[i-1].Error(); preErr != nil { // if {Cmd} get a RedisError
if preErr := resps.s[i-1].error(); preErr != nil { // if {Cmd} get a RedisError
err = preErr
}
results.s = append(results.s, newErrResult(err))
Expand Down Expand Up @@ -988,15 +988,15 @@ func (c *clusterClient) resultcachefn(
ii := cIndexes[i]
cm := commands[i]
results.s[ii] = resp
addr, mode := c.shouldRefreshRetry(resp.Error(), ctx)
addr, mode := c.shouldRefreshRetry(resp.error(), ctx)
if mode != RedirectNone {
nc := cc
retryDelay := time.Duration(-1)
if mode == RedirectRetry {
if !c.retry {
continue
}
retryDelay = c.retryHandler.RetryDelay(attempts, Completed(cm.Cmd), resp.Error())
retryDelay = c.retryHandler.RetryDelay(attempts, Completed(cm.Cmd), resp.error())
} else {
nc = c.redirectOrNew(addr, cc, cm.Cmd.Slot(), mode)
}
Expand Down Expand Up @@ -1295,11 +1295,11 @@ retry:
resp = newErrResult(err)
} else {
resp = w.Do(ctx, cmd)
switch _, mode := c.client.shouldRefreshRetry(resp.Error(), ctx); mode {
switch _, mode := c.client.shouldRefreshRetry(resp.error(), ctx); mode {
case RedirectRetry:
if c.retry && cmd.IsReadOnly() && w.Error() == nil {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, cmd, resp.Error(),
ctx, attempts, cmd, resp.error(),
)
if shouldRetry {
attempts++
Expand Down Expand Up @@ -1331,10 +1331,10 @@ retry:
if w, err := c.acquire(ctx, slot); err == nil {
resp = w.DoMulti(ctx, multi...).s
for i, r := range resp {
_, mode := c.client.shouldRefreshRetry(r.Error(), ctx)
_, mode := c.client.shouldRefreshRetry(r.error(), ctx)
if mode == RedirectRetry && retryable && w.Error() == nil {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, multi[i], r.Error(),
ctx, attempts, multi[i], r.error(),
)
if shouldRetry {
attempts++
Expand Down
2 changes: 1 addition & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func doMultiSet(cc Client, ctx context.Context, cmds []Completed) (ret map[strin
ret = make(map[string]error, len(cmds))
resps := cc.DoMulti(ctx, cmds...)
for i, resp := range resps {
if ret[cmds[i].Commands()[1]] = resp.Error(); resp.err == nil {
if ret[cmds[i].Commands()[1]] = resp.error(); resp.err == nil {
intl.PutCompletedForce(cmds[i])
}
}
Expand Down
4 changes: 4 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func (r RedisResult) NonRedisError() error {

// Error returns either underlying error or redis error or nil
func (r RedisResult) Error() (err error) {
return r.error()
}

func (r *RedisResult) error() (err error) {
if r.err != nil {
err = r.err
} else {
Expand Down
14 changes: 7 additions & 7 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
if i == 0 {
p.info, err = r.AsMap()
} else {
err = r.Error()
err = r.error()
}
if err != nil {
if init[i][0] == "READONLY" {
Expand Down Expand Up @@ -301,7 +301,7 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
// ignore READONLY command error
continue
}
if err = r.Error(); err != nil {
if err = r.error(); err != nil {
p.Close()
return nil, err
}
Expand Down Expand Up @@ -1315,7 +1315,7 @@ func (p *pipe) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) Re
if err != nil {
if _, ok := err.(*RedisError); ok {
err = ErrDoCacheAborted
if preErr := resp.s[3].Error(); preErr != nil { // if {cmd} get a RedisError
if preErr := resp.s[3].error(); preErr != nil { // if {cmd} get a RedisError
if _, ok := preErr.(*RedisError); ok {
err = preErr
}
Expand Down Expand Up @@ -1384,7 +1384,7 @@ func (p *pipe) doCacheMGet(ctx context.Context, cmd Cacheable, ttl time.Duration
if err != nil {
if _, ok := err.(*RedisError); ok {
err = ErrDoCacheAborted
if preErr := resp.s[len(multi)-2].Error(); preErr != nil { // if {rewritten} get a RedisError
if preErr := resp.s[len(multi)-2].error(); preErr != nil { // if {rewritten} get a RedisError
if _, ok := preErr.(*RedisError); ok {
err = preErr
}
Expand Down Expand Up @@ -1481,10 +1481,10 @@ func (p *pipe) DoMultiCache(ctx context.Context, multi ...CacheableTTL) *redisre
resp = p.DoMulti(ctx, missing...)
defer resultsp.Put(resp)
for i := 4; i < len(resp.s); i += 5 {
if err := resp.s[i].Error(); err != nil {
if err := resp.s[i].error(); err != nil {
if _, ok := err.(*RedisError); ok {
err = ErrDoCacheAborted
if preErr := resp.s[i-1].Error(); preErr != nil { // if {cmd} get a RedisError
if preErr := resp.s[i-1].error(); preErr != nil { // if {cmd} get a RedisError
if _, ok := preErr.(*RedisError); ok {
err = preErr
}
Expand Down Expand Up @@ -1512,7 +1512,7 @@ func (p *pipe) DoMultiCache(ctx context.Context, multi ...CacheableTTL) *redisre
if err != nil {
if _, ok := err.(*RedisError); ok {
err = ErrDoCacheAborted
if preErr := resp.s[i-1].Error(); preErr != nil { // if {cmd} get a RedisError
if preErr := resp.s[i-1].error(); preErr != nil { // if {cmd} get a RedisError
if _, ok := preErr.(*RedisError); ok {
err = preErr
}
Expand Down
16 changes: 8 additions & 8 deletions sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func (c *sentinelClient) Do(ctx context.Context, cmd Completed) (resp RedisResul
attempts := 1
retry:
resp = c.mConn.Load().(conn).Do(ctx, cmd)
if c.retry && cmd.IsReadOnly() && c.isRetryable(resp.Error(), ctx) {
if c.retry && cmd.IsReadOnly() && c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, cmd, resp.Error(),
ctx, attempts, cmd, resp.error(),
)
if shouldRetry {
attempts++
Expand All @@ -91,9 +91,9 @@ retry:
resps := c.mConn.Load().(conn).DoMulti(ctx, multi...)
if c.retry && allReadOnly(multi) {
for i, resp := range resps.s {
if c.isRetryable(resp.Error(), ctx) {
if c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, multi[i], resp.Error(),
ctx, attempts, multi[i], resp.error(),
)
if shouldRetry {
resultsp.Put(resps)
Expand All @@ -115,8 +115,8 @@ func (c *sentinelClient) DoCache(ctx context.Context, cmd Cacheable, ttl time.Du
attempts := 1
retry:
resp = c.mConn.Load().(conn).DoCache(ctx, cmd, ttl)
if c.retry && c.isRetryable(resp.Error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, Completed(cmd), resp.Error())
if c.retry && c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, Completed(cmd), resp.error())
if shouldRetry {
attempts++
goto retry
Expand All @@ -138,9 +138,9 @@ retry:
resps := c.mConn.Load().(conn).DoMultiCache(ctx, multi...)
if c.retry {
for i, resp := range resps.s {
if c.isRetryable(resp.Error(), ctx) {
if c.isRetryable(resp.error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, Completed(multi[i].Cmd), resp.Error(),
ctx, attempts, Completed(multi[i].Cmd), resp.error(),
)
if shouldRetry {
resultsp.Put(resps)
Expand Down

0 comments on commit 98a7df8

Please sign in to comment.