1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-18 00:20:57 +03:00

perf: reduce unnecessary memory allocation (#3399)

Signed-off-by: fukua95 <fukua95@gmail.com>
Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
This commit is contained in:
fukua95
2025-06-09 16:59:58 +08:00
committed by GitHub
parent 0f40ae3ff2
commit eb40ac8328
6 changed files with 29 additions and 59 deletions

View File

@ -460,7 +460,7 @@ var _ = Describe("Commands", func() {
} }
// read the defaults to set them back later // read the defaults to set them back later
for setting, _ := range expected { for setting := range expected {
val, err := client.ConfigGet(ctx, setting).Result() val, err := client.ConfigGet(ctx, setting).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
defaults[setting] = val[setting] defaults[setting] = val[setting]

View File

@ -410,7 +410,7 @@ var _ = Describe("race", func() {
_, err = p.Get(ctx) _, err = p.Get(ctx)
Expect(err).To(MatchError(pool.ErrPoolTimeout)) Expect(err).To(MatchError(pool.ErrPoolTimeout))
p.Put(ctx, conn) p.Put(ctx, conn)
conn, err = p.Get(ctx) _, err = p.Get(ctx)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
stats = p.Stats() stats = p.Stats()

View File

@ -1116,18 +1116,14 @@ func (c cmdable) TopKListWithCount(ctx context.Context, key string) *MapStringIn
// Returns OK on success or an error if the operation could not be completed. // Returns OK on success or an error if the operation could not be completed.
// For more information - https://redis.io/commands/tdigest.add/ // For more information - https://redis.io/commands/tdigest.add/
func (c cmdable) TDigestAdd(ctx context.Context, key string, elements ...float64) *StatusCmd { func (c cmdable) TDigestAdd(ctx context.Context, key string, elements ...float64) *StatusCmd {
args := make([]interface{}, 2, 2+len(elements)) args := make([]interface{}, 2+len(elements))
args[0] = "TDIGEST.ADD" args[0] = "TDIGEST.ADD"
args[1] = key args[1] = key
// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(elements))
for i, v := range elements { for i, v := range elements {
interfaceSlice[i] = v args[2+i] = v
} }
args = append(args, interfaceSlice...)
cmd := NewStatusCmd(ctx, args...) cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
@ -1138,18 +1134,14 @@ func (c cmdable) TDigestAdd(ctx context.Context, key string, elements ...float64
// Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed. // Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed.
// For more information - https://redis.io/commands/tdigest.byrank/ // For more information - https://redis.io/commands/tdigest.byrank/
func (c cmdable) TDigestByRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd { func (c cmdable) TDigestByRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd {
args := make([]interface{}, 2, 2+len(rank)) args := make([]interface{}, 2+len(rank))
args[0] = "TDIGEST.BYRANK" args[0] = "TDIGEST.BYRANK"
args[1] = key args[1] = key
// Convert uint slice to []interface{} for i, r := range rank {
interfaceSlice := make([]interface{}, len(rank)) args[2+i] = r
for i, v := range rank {
interfaceSlice[i] = v
} }
args = append(args, interfaceSlice...)
cmd := NewFloatSliceCmd(ctx, args...) cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
@ -1160,18 +1152,14 @@ func (c cmdable) TDigestByRank(ctx context.Context, key string, rank ...uint64)
// Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed. // Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed.
// For more information - https://redis.io/commands/tdigest.byrevrank/ // For more information - https://redis.io/commands/tdigest.byrevrank/
func (c cmdable) TDigestByRevRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd { func (c cmdable) TDigestByRevRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd {
args := make([]interface{}, 2, 2+len(rank)) args := make([]interface{}, 2+len(rank))
args[0] = "TDIGEST.BYREVRANK" args[0] = "TDIGEST.BYREVRANK"
args[1] = key args[1] = key
// Convert uint slice to []interface{} for i, r := range rank {
interfaceSlice := make([]interface{}, len(rank)) args[2+i] = r
for i, v := range rank {
interfaceSlice[i] = v
} }
args = append(args, interfaceSlice...)
cmd := NewFloatSliceCmd(ctx, args...) cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
@ -1182,18 +1170,14 @@ func (c cmdable) TDigestByRevRank(ctx context.Context, key string, rank ...uint6
// Returns an array of floats representing the CDF values for each element or an error if the operation could not be completed. // Returns an array of floats representing the CDF values for each element or an error if the operation could not be completed.
// For more information - https://redis.io/commands/tdigest.cdf/ // For more information - https://redis.io/commands/tdigest.cdf/
func (c cmdable) TDigestCDF(ctx context.Context, key string, elements ...float64) *FloatSliceCmd { func (c cmdable) TDigestCDF(ctx context.Context, key string, elements ...float64) *FloatSliceCmd {
args := make([]interface{}, 2, 2+len(elements)) args := make([]interface{}, 2+len(elements))
args[0] = "TDIGEST.CDF" args[0] = "TDIGEST.CDF"
args[1] = key args[1] = key
// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(elements))
for i, v := range elements { for i, v := range elements {
interfaceSlice[i] = v args[2+i] = v
} }
args = append(args, interfaceSlice...)
cmd := NewFloatSliceCmd(ctx, args...) cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
@ -1376,18 +1360,14 @@ func (c cmdable) TDigestMin(ctx context.Context, key string) *FloatCmd {
// Returns an array of floats representing the quantile values for each element or an error if the operation could not be completed. // Returns an array of floats representing the quantile values for each element or an error if the operation could not be completed.
// For more information - https://redis.io/commands/tdigest.quantile/ // For more information - https://redis.io/commands/tdigest.quantile/
func (c cmdable) TDigestQuantile(ctx context.Context, key string, elements ...float64) *FloatSliceCmd { func (c cmdable) TDigestQuantile(ctx context.Context, key string, elements ...float64) *FloatSliceCmd {
args := make([]interface{}, 2, 2+len(elements)) args := make([]interface{}, 2+len(elements))
args[0] = "TDIGEST.QUANTILE" args[0] = "TDIGEST.QUANTILE"
args[1] = key args[1] = key
// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(elements))
for i, v := range elements { for i, v := range elements {
interfaceSlice[i] = v args[2+i] = v
} }
args = append(args, interfaceSlice...)
cmd := NewFloatSliceCmd(ctx, args...) cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
@ -1398,18 +1378,14 @@ func (c cmdable) TDigestQuantile(ctx context.Context, key string, elements ...fl
// Returns an array of integers representing the rank values for each element or an error if the operation could not be completed. // Returns an array of integers representing the rank values for each element or an error if the operation could not be completed.
// For more information - https://redis.io/commands/tdigest.rank/ // For more information - https://redis.io/commands/tdigest.rank/
func (c cmdable) TDigestRank(ctx context.Context, key string, values ...float64) *IntSliceCmd { func (c cmdable) TDigestRank(ctx context.Context, key string, values ...float64) *IntSliceCmd {
args := make([]interface{}, 2, 2+len(values)) args := make([]interface{}, 2+len(values))
args[0] = "TDIGEST.RANK" args[0] = "TDIGEST.RANK"
args[1] = key args[1] = key
// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(values))
for i, v := range values { for i, v := range values {
interfaceSlice[i] = v args[i+2] = v
} }
args = append(args, interfaceSlice...)
cmd := NewIntSliceCmd(ctx, args...) cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
@ -1431,18 +1407,14 @@ func (c cmdable) TDigestReset(ctx context.Context, key string) *StatusCmd {
// Returns an array of integers representing the reverse rank values for each element or an error if the operation could not be completed. // Returns an array of integers representing the reverse rank values for each element or an error if the operation could not be completed.
// For more information - https://redis.io/commands/tdigest.revrank/ // For more information - https://redis.io/commands/tdigest.revrank/
func (c cmdable) TDigestRevRank(ctx context.Context, key string, values ...float64) *IntSliceCmd { func (c cmdable) TDigestRevRank(ctx context.Context, key string, values ...float64) *IntSliceCmd {
args := make([]interface{}, 2, 2+len(values)) args := make([]interface{}, 2+len(values))
args[0] = "TDIGEST.REVRANK" args[0] = "TDIGEST.REVRANK"
args[1] = key args[1] = key
// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(values))
for i, v := range values { for i, v := range values {
interfaceSlice[i] = v args[2+i] = v
} }
args = append(args, interfaceSlice...)
cmd := NewIntSliceCmd(ctx, args...) cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd

View File

@ -78,16 +78,15 @@ func (c cmdable) SInter(ctx context.Context, keys ...string) *StringSliceCmd {
} }
func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd { func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd {
args := make([]interface{}, 4+len(keys)) numKeys := len(keys)
args := make([]interface{}, 4+numKeys)
args[0] = "sintercard" args[0] = "sintercard"
numkeys := int64(0) args[1] = numKeys
for i, key := range keys { for i, key := range keys {
args[2+i] = key args[2+i] = key
numkeys++
} }
args[1] = numkeys args[2+numKeys] = "limit"
args[2+numkeys] = "limit" args[3+numKeys] = limit
args[3+numkeys] = limit
cmd := NewIntCmd(ctx, args...) cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd

View File

@ -257,16 +257,15 @@ func (c cmdable) ZInterWithScores(ctx context.Context, store *ZStore) *ZSliceCmd
} }
func (c cmdable) ZInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd { func (c cmdable) ZInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd {
args := make([]interface{}, 4+len(keys)) numKeys := len(keys)
args := make([]interface{}, 4+numKeys)
args[0] = "zintercard" args[0] = "zintercard"
numkeys := int64(0) args[1] = numKeys
for i, key := range keys { for i, key := range keys {
args[2+i] = key args[2+i] = key
numkeys++
} }
args[1] = numkeys args[2+numKeys] = "limit"
args[2+numkeys] = "limit" args[3+numKeys] = limit
args[3+numkeys] = limit
cmd := NewIntCmd(ctx, args...) cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd

View File

@ -11,7 +11,7 @@ type mockCmdable struct {
returnErr error returnErr error
} }
func (m *mockCmdable) call(ctx context.Context, cmd Cmder) error { func (m *mockCmdable) call(_ context.Context, cmd Cmder) error {
m.lastCmd = cmd m.lastCmd = cmd
if m.returnErr != nil { if m.returnErr != nil {
cmd.SetErr(m.returnErr) cmd.SetErr(m.returnErr)