From 8ffd1cf51e5bac24926fbdec8d579ffe0e2ffebb Mon Sep 17 00:00:00 2001 From: doashenzzg Date: Wed, 6 Mar 2024 16:55:45 +0800 Subject: [PATCH] perf: map make with cap --- cache.go | 10 +++++----- remote/goredisv8adapter.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cache.go b/cache.go index 874dece..8b0e361 100644 --- a/cache.go +++ b/cache.go @@ -313,8 +313,8 @@ func (c *jetCache[K, V]) MGet(ctx context.Context, key string, ids []K, fn func( c.statsHandler.IncrQueryFail(err) logger.Error("MGet#fn(%s) error(%v)", util.JoinAny(",", ids), err) } else { - placeholderValues := make(map[string]any) - cacheValues := make(map[string]any) + placeholderValues := make(map[string]any, len(ids)) + cacheValues := make(map[string]any, len(ids)) for rk, rv := range fnValues { values[rk] = rv cacheKey := util.JoinAny(":", key, rk) @@ -365,8 +365,8 @@ func (c *jetCache[K, V]) MGet(ctx context.Context, key string, ids []K, fn func( } func (c *jetCache[K, V]) mGetCache(ctx context.Context, key string, ids []K) (v map[K]V, missIds []K) { - v = make(map[K]V) - miss := make(map[string]K) + v = make(map[K]V, len(ids)) + miss := make(map[string]K, len(ids)) for _, id := range ids { cacheKey := util.JoinAny(":", key, id) @@ -393,7 +393,7 @@ func (c *jetCache[K, V]) mGetCache(ctx context.Context, key string, ids []K) (v } if len(miss) > 0 && c.remote != nil { - missKeys := make([]string, 0) + missKeys := make([]string, 0, len(miss)) for k := range miss { missKeys = append(missKeys, k) } diff --git a/remote/goredisv8adapter.go b/remote/goredisv8adapter.go index 7a2e61e..bfd1218 100644 --- a/remote/goredisv8adapter.go +++ b/remote/goredisv8adapter.go @@ -46,7 +46,7 @@ func (r *GoRedisV8Adaptor) Del(ctx context.Context, key string) (val int64, err func (r *GoRedisV8Adaptor) MGet(ctx context.Context, keys ...string) (map[string]any, error) { pipeline := r.client.Pipeline() keyIdxMap := make(map[int]string, len(keys)) - ret := make(map[string]any) + ret := make(map[string]any, len(keys)) for idx, key := range keys { keyIdxMap[idx] = key