Skip to content

Commit eb40ac8

Browse files
cxljsndyakov
andauthored
perf: reduce unnecessary memory allocation (#3399)
Signed-off-by: fukua95 <[email protected]> Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent 0f40ae3 commit eb40ac8

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
@@ -460,7 +460,7 @@ var _ = Describe("Commands", func() {
460460
}
461461

462462
// read the defaults to set them back later
463-
for setting, _ := range expected {
463+
for setting := range expected {
464464
val, err := client.ConfigGet(ctx, setting).Result()
465465
Expect(err).NotTo(HaveOccurred())
466466
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
@@ -1116,18 +1116,14 @@ func (c cmdable) TopKListWithCount(ctx context.Context, key string) *MapStringIn
11161116
// Returns OK on success or an error if the operation could not be completed.
11171117
// For more information - https://redis.io/commands/tdigest.add/
11181118
func (c cmdable) TDigestAdd(ctx context.Context, key string, elements ...float64) *StatusCmd {
1119-
args := make([]interface{}, 2, 2+len(elements))
1119+
args := make([]interface{}, 2+len(elements))
11201120
args[0] = "TDIGEST.ADD"
11211121
args[1] = key
11221122

1123-
// Convert floatSlice to []interface{}
1124-
interfaceSlice := make([]interface{}, len(elements))
11251123
for i, v := range elements {
1126-
interfaceSlice[i] = v
1124+
args[2+i] = v
11271125
}
11281126

1129-
args = append(args, interfaceSlice...)
1130-
11311127
cmd := NewStatusCmd(ctx, args...)
11321128
_ = c(ctx, cmd)
11331129
return cmd
@@ -1138,18 +1134,14 @@ func (c cmdable) TDigestAdd(ctx context.Context, key string, elements ...float64
11381134
// Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed.
11391135
// For more information - https://redis.io/commands/tdigest.byrank/
11401136
func (c cmdable) TDigestByRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd {
1141-
args := make([]interface{}, 2, 2+len(rank))
1137+
args := make([]interface{}, 2+len(rank))
11421138
args[0] = "TDIGEST.BYRANK"
11431139
args[1] = key
11441140

1145-
// Convert uint slice to []interface{}
1146-
interfaceSlice := make([]interface{}, len(rank))
1147-
for i, v := range rank {
1148-
interfaceSlice[i] = v
1141+
for i, r := range rank {
1142+
args[2+i] = r
11491143
}
11501144

1151-
args = append(args, interfaceSlice...)
1152-
11531145
cmd := NewFloatSliceCmd(ctx, args...)
11541146
_ = c(ctx, cmd)
11551147
return cmd
@@ -1160,18 +1152,14 @@ func (c cmdable) TDigestByRank(ctx context.Context, key string, rank ...uint64)
11601152
// Returns an array of floats representing the values at the specified ranks or an error if the operation could not be completed.
11611153
// For more information - https://redis.io/commands/tdigest.byrevrank/
11621154
func (c cmdable) TDigestByRevRank(ctx context.Context, key string, rank ...uint64) *FloatSliceCmd {
1163-
args := make([]interface{}, 2, 2+len(rank))
1155+
args := make([]interface{}, 2+len(rank))
11641156
args[0] = "TDIGEST.BYREVRANK"
11651157
args[1] = key
11661158

1167-
// Convert uint slice to []interface{}
1168-
interfaceSlice := make([]interface{}, len(rank))
1169-
for i, v := range rank {
1170-
interfaceSlice[i] = v
1159+
for i, r := range rank {
1160+
args[2+i] = r
11711161
}
11721162

1173-
args = append(args, interfaceSlice...)
1174-
11751163
cmd := NewFloatSliceCmd(ctx, args...)
11761164
_ = c(ctx, cmd)
11771165
return cmd
@@ -1182,18 +1170,14 @@ func (c cmdable) TDigestByRevRank(ctx context.Context, key string, rank ...uint6
11821170
// Returns an array of floats representing the CDF values for each element or an error if the operation could not be completed.
11831171
// For more information - https://redis.io/commands/tdigest.cdf/
11841172
func (c cmdable) TDigestCDF(ctx context.Context, key string, elements ...float64) *FloatSliceCmd {
1185-
args := make([]interface{}, 2, 2+len(elements))
1173+
args := make([]interface{}, 2+len(elements))
11861174
args[0] = "TDIGEST.CDF"
11871175
args[1] = key
11881176

1189-
// Convert floatSlice to []interface{}
1190-
interfaceSlice := make([]interface{}, len(elements))
11911177
for i, v := range elements {
1192-
interfaceSlice[i] = v
1178+
args[2+i] = v
11931179
}
11941180

1195-
args = append(args, interfaceSlice...)
1196-
11971181
cmd := NewFloatSliceCmd(ctx, args...)
11981182
_ = c(ctx, cmd)
11991183
return cmd
@@ -1376,18 +1360,14 @@ func (c cmdable) TDigestMin(ctx context.Context, key string) *FloatCmd {
13761360
// Returns an array of floats representing the quantile values for each element or an error if the operation could not be completed.
13771361
// For more information - https://redis.io/commands/tdigest.quantile/
13781362
func (c cmdable) TDigestQuantile(ctx context.Context, key string, elements ...float64) *FloatSliceCmd {
1379-
args := make([]interface{}, 2, 2+len(elements))
1363+
args := make([]interface{}, 2+len(elements))
13801364
args[0] = "TDIGEST.QUANTILE"
13811365
args[1] = key
13821366

1383-
// Convert floatSlice to []interface{}
1384-
interfaceSlice := make([]interface{}, len(elements))
13851367
for i, v := range elements {
1386-
interfaceSlice[i] = v
1368+
args[2+i] = v
13871369
}
13881370

1389-
args = append(args, interfaceSlice...)
1390-
13911371
cmd := NewFloatSliceCmd(ctx, args...)
13921372
_ = c(ctx, cmd)
13931373
return cmd
@@ -1398,18 +1378,14 @@ func (c cmdable) TDigestQuantile(ctx context.Context, key string, elements ...fl
13981378
// Returns an array of integers representing the rank values for each element or an error if the operation could not be completed.
13991379
// For more information - https://redis.io/commands/tdigest.rank/
14001380
func (c cmdable) TDigestRank(ctx context.Context, key string, values ...float64) *IntSliceCmd {
1401-
args := make([]interface{}, 2, 2+len(values))
1381+
args := make([]interface{}, 2+len(values))
14021382
args[0] = "TDIGEST.RANK"
14031383
args[1] = key
14041384

1405-
// Convert floatSlice to []interface{}
1406-
interfaceSlice := make([]interface{}, len(values))
14071385
for i, v := range values {
1408-
interfaceSlice[i] = v
1386+
args[i+2] = v
14091387
}
14101388

1411-
args = append(args, interfaceSlice...)
1412-
14131389
cmd := NewIntSliceCmd(ctx, args...)
14141390
_ = c(ctx, cmd)
14151391
return cmd
@@ -1431,18 +1407,14 @@ func (c cmdable) TDigestReset(ctx context.Context, key string) *StatusCmd {
14311407
// Returns an array of integers representing the reverse rank values for each element or an error if the operation could not be completed.
14321408
// For more information - https://redis.io/commands/tdigest.revrank/
14331409
func (c cmdable) TDigestRevRank(ctx context.Context, key string, values ...float64) *IntSliceCmd {
1434-
args := make([]interface{}, 2, 2+len(values))
1410+
args := make([]interface{}, 2+len(values))
14351411
args[0] = "TDIGEST.REVRANK"
14361412
args[1] = key
14371413

1438-
// Convert floatSlice to []interface{}
1439-
interfaceSlice := make([]interface{}, len(values))
14401414
for i, v := range values {
1441-
interfaceSlice[i] = v
1415+
args[2+i] = v
14421416
}
14431417

1444-
args = append(args, interfaceSlice...)
1445-
14461418
cmd := NewIntSliceCmd(ctx, args...)
14471419
_ = c(ctx, cmd)
14481420
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)