Skip to content

Commit 1b3dc2d

Browse files
cxljsndyakov
authored andcommitted
perf: reduce unnecessary memory allocation (redis#3399)
Signed-off-by: fukua95 <[email protected]> Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent 8321f51 commit 1b3dc2d

File tree

6 files changed

+29
-59
lines changed

6 files changed

+29
-59
lines changed

commands_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ var _ = Describe("Commands", func() {
461461
}
462462

463463
// read the defaults to set them back later
464-
for setting, _ := range expected {
464+
for setting := range expected {
465465
val, err := client.ConfigGet(ctx, setting).Result()
466466
Expect(err).NotTo(HaveOccurred())
467467
defaults[setting] = val[setting]

internal/pool/pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ var _ = Describe("race", func() {
410410
_, err = p.Get(ctx)
411411
Expect(err).To(MatchError(pool.ErrPoolTimeout))
412412
p.Put(ctx, conn)
413-
conn, err = p.Get(ctx)
413+
_, err = p.Get(ctx)
414414
Expect(err).NotTo(HaveOccurred())
415415

416416
stats = p.Stats()

probabilistic.go

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,18 +1156,14 @@ func (c cmdable) TopKListWithCount(ctx context.Context, key string) *MapStringIn
11561156
// Returns OK on success or an error if the operation could not be completed.
11571157
// For more information - https://redis.io/commands/tdigest.add/
11581158
func (c cmdable) TDigestAdd(ctx context.Context, key string, elements ...float64) *StatusCmd {
1159-
args := make([]interface{}, 2, 2+len(elements))
1159+
args := make([]interface{}, 2+len(elements))
11601160
args[0] = "TDIGEST.ADD"
11611161
args[1] = key
11621162

1163-
// Convert floatSlice to []interface{}
1164-
interfaceSlice := make([]interface{}, len(elements))
11651163
for i, v := range elements {
1166-
interfaceSlice[i] = v
1164+
args[2+i] = v
11671165
}
11681166

1169-
args = append(args, interfaceSlice...)
1170-
11711167
cmd := NewStatusCmd(ctx, args...)
11721168
_ = c(ctx, cmd)
11731169
return cmd
@@ -1178,18 +1174,14 @@ func (c cmdable) TDigestAdd(ctx context.Context, key string, elements ...float64
11781174
// Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed.
11791175
// For more information - https://redis.io/commands/tdigest.byrank/
11801176
func (c cmdable) TDigestByRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd {
1181-
args := make([]interface{}, 2, 2+len(rank))
1177+
args := make([]interface{}, 2+len(rank))
11821178
args[0] = "TDIGEST.BYRANK"
11831179
args[1] = key
11841180

1185-
// Convert uint slice to []interface{}
1186-
interfaceSlice := make([]interface{}, len(rank))
1187-
for i, v := range rank {
1188-
interfaceSlice[i] = v
1181+
for i, r := range rank {
1182+
args[2+i] = r
11891183
}
11901184

1191-
args = append(args, interfaceSlice...)
1192-
11931185
cmd := NewFloatSliceCmd(ctx, args...)
11941186
_ = c(ctx, cmd)
11951187
return cmd
@@ -1200,18 +1192,14 @@ func (c cmdable) TDigestByRank(ctx context.Context, key string, rank ...uint64)
12001192
// Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed.
12011193
// For more information - https://redis.io/commands/tdigest.byrevrank/
12021194
func (c cmdable) TDigestByRevRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd {
1203-
args := make([]interface{}, 2, 2+len(rank))
1195+
args := make([]interface{}, 2+len(rank))
12041196
args[0] = "TDIGEST.BYREVRANK"
12051197
args[1] = key
12061198

1207-
// Convert uint slice to []interface{}
1208-
interfaceSlice := make([]interface{}, len(rank))
1209-
for i, v := range rank {
1210-
interfaceSlice[i] = v
1199+
for i, r := range rank {
1200+
args[2+i] = r
12111201
}
12121202

1213-
args = append(args, interfaceSlice...)
1214-
12151203
cmd := NewFloatSliceCmd(ctx, args...)
12161204
_ = c(ctx, cmd)
12171205
return cmd
@@ -1222,18 +1210,14 @@ func (c cmdable) TDigestByRevRank(ctx context.Context, key string, rank ...uint6
12221210
// Returns an array of floats representing the CDF values for each element or an error if the operation could not be completed.
12231211
// For more information - https://redis.io/commands/tdigest.cdf/
12241212
func (c cmdable) TDigestCDF(ctx context.Context, key string, elements ...float64) *FloatSliceCmd {
1225-
args := make([]interface{}, 2, 2+len(elements))
1213+
args := make([]interface{}, 2+len(elements))
12261214
args[0] = "TDIGEST.CDF"
12271215
args[1] = key
12281216

1229-
// Convert floatSlice to []interface{}
1230-
interfaceSlice := make([]interface{}, len(elements))
12311217
for i, v := range elements {
1232-
interfaceSlice[i] = v
1218+
args[2+i] = v
12331219
}
12341220

1235-
args = append(args, interfaceSlice...)
1236-
12371221
cmd := NewFloatSliceCmd(ctx, args...)
12381222
_ = c(ctx, cmd)
12391223
return cmd
@@ -1424,18 +1408,14 @@ func (c cmdable) TDigestMin(ctx context.Context, key string) *FloatCmd {
14241408
// Returns an array of floats representing the quantile values for each element or an error if the operation could not be completed.
14251409
// For more information - https://redis.io/commands/tdigest.quantile/
14261410
func (c cmdable) TDigestQuantile(ctx context.Context, key string, elements ...float64) *FloatSliceCmd {
1427-
args := make([]interface{}, 2, 2+len(elements))
1411+
args := make([]interface{}, 2+len(elements))
14281412
args[0] = "TDIGEST.QUANTILE"
14291413
args[1] = key
14301414

1431-
// Convert floatSlice to []interface{}
1432-
interfaceSlice := make([]interface{}, len(elements))
14331415
for i, v := range elements {
1434-
interfaceSlice[i] = v
1416+
args[2+i] = v
14351417
}
14361418

1437-
args = append(args, interfaceSlice...)
1438-
14391419
cmd := NewFloatSliceCmd(ctx, args...)
14401420
_ = c(ctx, cmd)
14411421
return cmd
@@ -1446,18 +1426,14 @@ func (c cmdable) TDigestQuantile(ctx context.Context, key string, elements ...fl
14461426
// Returns an array of integers representing the rank values for each element or an error if the operation could not be completed.
14471427
// For more information - https://redis.io/commands/tdigest.rank/
14481428
func (c cmdable) TDigestRank(ctx context.Context, key string, values ...float64) *IntSliceCmd {
1449-
args := make([]interface{}, 2, 2+len(values))
1429+
args := make([]interface{}, 2+len(values))
14501430
args[0] = "TDIGEST.RANK"
14511431
args[1] = key
14521432

1453-
// Convert floatSlice to []interface{}
1454-
interfaceSlice := make([]interface{}, len(values))
14551433
for i, v := range values {
1456-
interfaceSlice[i] = v
1434+
args[i+2] = v
14571435
}
14581436

1459-
args = append(args, interfaceSlice...)
1460-
14611437
cmd := NewIntSliceCmd(ctx, args...)
14621438
_ = c(ctx, cmd)
14631439
return cmd
@@ -1479,18 +1455,14 @@ func (c cmdable) TDigestReset(ctx context.Context, key string) *StatusCmd {
14791455
// Returns an array of integers representing the reverse rank values for each element or an error if the operation could not be completed.
14801456
// For more information - https://redis.io/commands/tdigest.revrank/
14811457
func (c cmdable) TDigestRevRank(ctx context.Context, key string, values ...float64) *IntSliceCmd {
1482-
args := make([]interface{}, 2, 2+len(values))
1458+
args := make([]interface{}, 2+len(values))
14831459
args[0] = "TDIGEST.REVRANK"
14841460
args[1] = key
14851461

1486-
// Convert floatSlice to []interface{}
1487-
interfaceSlice := make([]interface{}, len(values))
14881462
for i, v := range values {
1489-
interfaceSlice[i] = v
1463+
args[2+i] = v
14901464
}
14911465

1492-
args = append(args, interfaceSlice...)
1493-
14941466
cmd := NewIntSliceCmd(ctx, args...)
14951467
_ = c(ctx, cmd)
14961468
return cmd

set_commands.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,15 @@ func (c cmdable) SInter(ctx context.Context, keys ...string) *StringSliceCmd {
7878
}
7979

8080
func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd {
81-
args := make([]interface{}, 4+len(keys))
81+
numKeys := len(keys)
82+
args := make([]interface{}, 4+numKeys)
8283
args[0] = "sintercard"
83-
numkeys := int64(0)
84+
args[1] = numKeys
8485
for i, key := range keys {
8586
args[2+i] = key
86-
numkeys++
8787
}
88-
args[1] = numkeys
89-
args[2+numkeys] = "limit"
90-
args[3+numkeys] = limit
88+
args[2+numKeys] = "limit"
89+
args[3+numKeys] = limit
9190
cmd := NewIntCmd(ctx, args...)
9291
_ = c(ctx, cmd)
9392
return cmd

sortedset_commands.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,15 @@ func (c cmdable) ZInterWithScores(ctx context.Context, store *ZStore) *ZSliceCmd
257257
}
258258

259259
func (c cmdable) ZInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd {
260-
args := make([]interface{}, 4+len(keys))
260+
numKeys := len(keys)
261+
args := make([]interface{}, 4+numKeys)
261262
args[0] = "zintercard"
262-
numkeys := int64(0)
263+
args[1] = numKeys
263264
for i, key := range keys {
264265
args[2+i] = key
265-
numkeys++
266266
}
267-
args[1] = numkeys
268-
args[2+numkeys] = "limit"
269-
args[3+numkeys] = limit
267+
args[2+numKeys] = "limit"
268+
args[3+numKeys] = limit
270269
cmd := NewIntCmd(ctx, args...)
271270
_ = c(ctx, cmd)
272271
return cmd

unit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type mockCmdable struct {
1111
returnErr error
1212
}
1313

14-
func (m *mockCmdable) call(ctx context.Context, cmd Cmder) error {
14+
func (m *mockCmdable) call(_ context.Context, cmd Cmder) error {
1515
m.lastCmd = cmd
1616
if m.returnErr != nil {
1717
cmd.SetErr(m.returnErr)

0 commit comments

Comments
 (0)