perf: pre-allocate all known length arrays to avoid re-scaling (#5029)

Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
Ioannis Pinakoulakis
2025-08-08 19:03:25 +03:00
committed by GitHub
parent a2b98dbcf7
commit 130e1ba963
19 changed files with 29 additions and 28 deletions

View File

@@ -316,7 +316,7 @@ func toLowerCaseInterface(v any, info *fieldInfo) any {
case map[string]any:
return toLowerCaseKeyMap(vv, info)
case []any:
var arr []any
arr := make([]any, 0, len(vv))
for _, vvv := range vv {
arr = append(arr, toLowerCaseInterface(vvv, info))
}

View File

@@ -207,7 +207,7 @@ func (c *cluster) getCurrent(key watchKey) []KV {
return nil
}
var kvs []KV
kvs := make([]KV, 0, len(watcher.values))
for k, v := range watcher.values {
kvs = append(kvs, KV{
Key: k,
@@ -308,7 +308,7 @@ func (c *cluster) load(cli EtcdClient, key watchKey) int64 {
time.Sleep(coolDownUnstable.AroundDuration(coolDownInterval))
}
var kvs []KV
kvs := make([]KV, 0, len(resp.Kvs))
for _, ev := range resp.Kvs {
kvs = append(kvs, KV{
Key: string(ev.Key),
@@ -352,7 +352,7 @@ func (c *cluster) reload(cli EtcdClient) {
// cancel the previous watches
close(c.done)
c.watchGroup.Wait()
var keys []watchKey
keys := make([]watchKey, 0, len(c.watchers))
for wk, wval := range c.watchers {
keys = append(keys, wk)
if wval.cancel != nil {

View File

@@ -211,7 +211,7 @@ func (r *SizeLimitRotateRule) OutdatedFiles() []string {
}
}
var result []string
result := make([]string, 0, len(outdated))
for k := range outdated {
result = append(result, k)
}

View File

@@ -1944,7 +1944,7 @@ func (s *Redis) ZaddsCtx(ctx context.Context, key string, ps ...Pair) (int64, er
return 0, err
}
var zs []red.Z
zs := make([]red.Z, 0, len(ps))
for _, p := range ps {
z := red.Z{Score: float64(p.Score), Member: p.Key}
zs = append(zs, z)