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

perf: avoid unnecessary runtime.duffcopy when the compiler fails to inline the helper function #714

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 18 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ 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++
goto retry
}
}
if resp.NonRedisError() == nil { // not recycle cmds if error, since cmds may be used later in pipe. consider recycle them by pipe
if resp.err == nil { // not recycle cmds if error, since cmds may be used later in pipe. consider recycle them by pipe
cmds.PutCompleted(cmd)
}
return resp
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 @@ -100,7 +100,7 @@ retry:
}
}
for i, cmd := range multi {
if resps[i].NonRedisError() == nil {
if resps[i].err == nil {
cmds.PutCompleted(cmd)
}
}
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 @@ -128,7 +128,7 @@ retry:
}
}
for i, cmd := range multi {
if err := resps[i].NonRedisError(); err == nil || err == ErrDoCacheAborted {
if err := resps[i].err; err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd.Cmd)
}
}
Expand All @@ -139,14 +139,14 @@ 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
}
}
if err := resp.NonRedisError(); err == nil || err == ErrDoCacheAborted {
if err := resp.err; err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd)
}
return resp
Expand Down Expand Up @@ -214,16 +214,16 @@ 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++
goto retry
}
}
if resp.NonRedisError() == nil {
if resp.err == nil {
cmds.PutCompleted(cmd)
}
return resp
Expand All @@ -244,16 +244,16 @@ 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++
goto retry
}
}
if resp[i].NonRedisError() == nil {
if resp[i].err == nil {
cmds.PutCompleted(cmd)
}
}
Expand Down
44 changes: 22 additions & 22 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 @@ -482,7 +482,7 @@ func (c *clusterClient) B() Builder {
}

func (c *clusterClient) Do(ctx context.Context, cmd Completed) (resp RedisResult) {
if resp = c.do(ctx, cmd); resp.NonRedisError() == nil { // not recycle cmds if error, since cmds may be used later in pipe. consider recycle them by pipe
if resp = c.do(ctx, cmd); resp.err == nil { // not recycle cmds if error, since cmds may be used later in pipe. consider recycle them by pipe
cmds.PutCompleted(cmd)
}
return resp
Expand All @@ -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 @@ -659,19 +659,19 @@ func (c *clusterClient) doresultfn(
ei := -1
clean = true
for i, resp := range resps {
clean = clean && resp.NonRedisError() == nil
clean = clean && resp.err == nil
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 @@ -803,7 +803,7 @@ retry:
}

for i, cmd := range multi {
if results.s[i].NonRedisError() == nil {
if results.s[i].err == nil {
cmds.PutCompleted(cmd)
}
}
Expand All @@ -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 All @@ -851,7 +851,7 @@ process:

func (c *clusterClient) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) (resp RedisResult) {
resp = c.doCache(ctx, cmd, ttl)
if err := resp.NonRedisError(); err == nil || err == ErrDoCacheAborted {
if err := resp.err; err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd)
}
return resp
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 @@ -984,19 +984,19 @@ func (c *clusterClient) resultcachefn(
) (clean bool) {
clean = true
for i, resp := range resps {
clean = clean && resp.NonRedisError() == nil
clean = clean && resp.err == nil
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 @@ -1097,7 +1097,7 @@ retry:
}

for i, cmd := range multi {
if err := results.s[i].NonRedisError(); err == nil || err == ErrDoCacheAborted {
if err := results.s[i].err; err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd.Cmd)
}
}
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 All @@ -1308,7 +1308,7 @@ retry:
}
}
}
if resp.NonRedisError() == nil {
if resp.err == nil {
cmds.PutCompleted(cmd)
}
return resp
Expand All @@ -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 All @@ -1352,7 +1352,7 @@ retry:
}
}
for i, cmd := range multi {
if resp[i].NonRedisError() == nil {
if resp[i].err == nil {
cmds.PutCompleted(cmd)
}
}
Expand Down
6 changes: 3 additions & 3 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func doMultiCache(cc Client, ctx context.Context, cmds []CacheableTTL, keys []st
resps := cc.DoMultiCache(ctx, cmds...)
defer resultsp.Put(&redisresults{s: resps})
for i, resp := range resps {
if err := resp.NonRedisError(); err != nil {
if err := resp.err; err != nil {
return nil, err
}
ret[keys[i]] = resp.val
Expand All @@ -247,7 +247,7 @@ func doMultiGet(cc Client, ctx context.Context, cmds []Completed, keys []string)
resps := cc.DoMulti(ctx, cmds...)
defer resultsp.Put(&redisresults{s: resps})
for i, resp := range resps {
if err := resp.NonRedisError(); err != nil {
if err := resp.err; err != nil {
return nil, err
}
ret[keys[i]] = resp.val
Expand All @@ -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.NonRedisError() == 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
12 changes: 6 additions & 6 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ block:
func (m *mux) blocking(pool *pool, ctx context.Context, cmd Completed) (resp RedisResult) {
wire := pool.Acquire()
resp = wire.Do(ctx, cmd)
if resp.NonRedisError() != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
if resp.err != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
wire.Close()
}
pool.Store(wire)
Expand All @@ -250,7 +250,7 @@ func (m *mux) blockingMulti(pool *pool, ctx context.Context, cmd []Completed) (r
wire := pool.Acquire()
resp = wire.DoMulti(ctx, cmd...)
for _, res := range resp.s {
if res.NonRedisError() != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
if res.err != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
wire.Close()
break
}
Expand All @@ -262,7 +262,7 @@ func (m *mux) blockingMulti(pool *pool, ctx context.Context, cmd []Completed) (r
func (m *mux) pipeline(ctx context.Context, cmd Completed) (resp RedisResult) {
slot := slotfn(len(m.wire), cmd.Slot(), cmd.NoReply())
wire := m.pipe(slot)
if resp = wire.Do(ctx, cmd); isBroken(resp.NonRedisError(), wire) {
if resp = wire.Do(ctx, cmd); isBroken(resp.err, wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
}
return resp
Expand All @@ -273,7 +273,7 @@ func (m *mux) pipelineMulti(ctx context.Context, cmd []Completed) (resp *redisre
wire := m.pipe(slot)
resp = wire.DoMulti(ctx, cmd...)
for _, r := range resp.s {
if isBroken(r.NonRedisError(), wire) {
if isBroken(r.err, wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
return resp
}
Expand All @@ -285,7 +285,7 @@ func (m *mux) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) Red
slot := cmd.Slot() & uint16(len(m.wire)-1)
wire := m.pipe(slot)
resp := wire.DoCache(ctx, cmd, ttl)
if isBroken(resp.NonRedisError(), wire) {
if isBroken(resp.err, wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
}
return resp
Expand Down Expand Up @@ -344,7 +344,7 @@ func (m *mux) doMultiCache(ctx context.Context, slot uint16, multi []CacheableTT
wire := m.pipe(slot)
resps = wire.DoMultiCache(ctx, multi...)
for _, r := range resps.s {
if isBroken(r.NonRedisError(), wire) {
if isBroken(r.err, wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
return resps
}
Expand Down
Loading
Loading