Skip to content

perf: reduce unnecessary memory allocation operations #3399

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

Merged
merged 3 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ var _ = Describe("Commands", func() {
}

// read the defaults to set them back later
for setting, _ := range expected {
for setting := range expected {
val, err := client.ConfigGet(ctx, setting).Result()
Expect(err).NotTo(HaveOccurred())
defaults[setting] = val[setting]
Expand Down
2 changes: 1 addition & 1 deletion internal/pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ var _ = Describe("race", func() {
_, err = p.Get(ctx)
Expect(err).To(MatchError(pool.ErrPoolTimeout))
p.Put(ctx, conn)
conn, err = p.Get(ctx)
_, err = p.Get(ctx)
Expect(err).NotTo(HaveOccurred())

stats = p.Stats()
Expand Down
60 changes: 16 additions & 44 deletions probabilistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// For more information - https://redis.io/commands/tdigest.add/
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[1] = key

// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(elements))
for i, v := range elements {
interfaceSlice[i] = v
args[2+i] = v
}

args = append(args, interfaceSlice...)

cmd := NewStatusCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand All @@ -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.
// For more information - https://redis.io/commands/tdigest.byrank/
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[1] = key

// Convert uint slice to []interface{}
interfaceSlice := make([]interface{}, len(rank))
for i, v := range rank {
interfaceSlice[i] = v
for i, r := range rank {
args[2+i] = r
}

args = append(args, interfaceSlice...)

cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand All @@ -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.
// For more information - https://redis.io/commands/tdigest.byrevrank/
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[1] = key

// Convert uint slice to []interface{}
interfaceSlice := make([]interface{}, len(rank))
for i, v := range rank {
interfaceSlice[i] = v
for i, r := range rank {
args[2+i] = r
}

args = append(args, interfaceSlice...)

cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand All @@ -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.
// For more information - https://redis.io/commands/tdigest.cdf/
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[1] = key

// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(elements))
for i, v := range elements {
interfaceSlice[i] = v
args[2+i] = v
}

args = append(args, interfaceSlice...)

cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand Down Expand Up @@ -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.
// For more information - https://redis.io/commands/tdigest.quantile/
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[1] = key

// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(elements))
for i, v := range elements {
interfaceSlice[i] = v
args[2+i] = v
}

args = append(args, interfaceSlice...)

cmd := NewFloatSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand All @@ -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.
// For more information - https://redis.io/commands/tdigest.rank/
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[1] = key

// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(values))
for i, v := range values {
interfaceSlice[i] = v
args[i+2] = v
}

args = append(args, interfaceSlice...)

cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand All @@ -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.
// For more information - https://redis.io/commands/tdigest.revrank/
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[1] = key

// Convert floatSlice to []interface{}
interfaceSlice := make([]interface{}, len(values))
for i, v := range values {
interfaceSlice[i] = v
args[2+i] = v
}

args = append(args, interfaceSlice...)

cmd := NewIntSliceCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand Down
11 changes: 5 additions & 6 deletions set_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
args := make([]interface{}, 4+len(keys))
numKeys := len(keys)
args := make([]interface{}, 4+numKeys)
args[0] = "sintercard"
numkeys := int64(0)
args[1] = numKeys
for i, key := range keys {
args[2+i] = key
numkeys++
}
args[1] = numkeys
args[2+numkeys] = "limit"
args[3+numkeys] = limit
args[2+numKeys] = "limit"
args[3+numKeys] = limit
cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand Down
11 changes: 5 additions & 6 deletions sortedset_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
args := make([]interface{}, 4+len(keys))
numKeys := len(keys)
args := make([]interface{}, 4+numKeys)
args[0] = "zintercard"
numkeys := int64(0)
args[1] = numKeys
for i, key := range keys {
args[2+i] = key
numkeys++
}
args[1] = numkeys
args[2+numkeys] = "limit"
args[3+numkeys] = limit
args[2+numKeys] = "limit"
args[3+numKeys] = limit
cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
Expand Down
2 changes: 1 addition & 1 deletion unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type mockCmdable struct {
returnErr error
}

func (m *mockCmdable) call(ctx context.Context, cmd Cmder) error {
func (m *mockCmdable) call(_ context.Context, cmd Cmder) error {
m.lastCmd = cmd
if m.returnErr != nil {
cmd.SetErr(m.returnErr)
Expand Down
Loading