Skip to content

Commit 77dc012

Browse files
committed
perf: avoid extra allocation
1 parent 5595a62 commit 77dc012

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

internal/utils/singleflightutil/singleFlight.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ type Group struct {
2828
// CheckAndDo implements a double-checked pattern to ensure that `fn` is executed only once for a given key.
2929
// It performs an initial check with `checkFn` before calling `singleflight.Do`. If the value is found, return.
3030
// Otherwise, call `Do` and execute `checkFn` again before executing `fn`.
31-
func (g *Group) CheckAndDo(key string, checkFn func() (any, bool), fn func() (any, error)) (v any, err error, shared bool) {
32-
if value, loaded := checkFn(); loaded {
31+
func (g *Group) CheckAndDo(key string, checkFn func(key string) (any, bool), fn func() (any, error)) (v any, err error, shared bool) {
32+
if value, loaded := checkFn(key); loaded {
3333
return value, nil, true
3434
}
3535
return g.Do(key, func() (any, error) {
36-
if value, loaded := checkFn(); loaded {
36+
if value, loaded := checkFn(key); loaded {
3737
return value, nil
3838
}
3939
return fn()

internal/utils/singleflightutil/singleFlight_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func TestCheckAndDo(t *testing.T) {
3939
return "new", nil
4040
}
4141

42-
checkFunc := func() (any, bool) {
43-
return cache.Load(key)
42+
checkFunc := func(str string) (any, bool) {
43+
return cache.Load(str)
4444
}
4545

4646
val, err, shared := g.CheckAndDo(key, checkFunc, fn)
@@ -61,8 +61,8 @@ func TestCheckAndDo(t *testing.T) {
6161
cache.Store(key, curr)
6262
return curr, nil
6363
}
64-
checkFunc := func() (any, bool) {
65-
return cache.Load(key)
64+
checkFunc := func(str string) (any, bool) {
65+
return cache.Load(str)
6666
}
6767

6868
var wg sync.WaitGroup

pkg/loadbalance/consist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ func (cb *consistBalancer) GetPicker(e discovery.Result) Picker {
248248
if e.Cacheable {
249249
cii, _, _ := cb.sfg.CheckAndDo(
250250
e.CacheKey,
251-
func() (any, bool) {
252-
return cb.cachedConsistInfo.Load(e.CacheKey)
251+
func(key string) (any, bool) {
252+
return cb.cachedConsistInfo.Load(key)
253253
},
254254
func() (interface{}, error) {
255255
res := cb.newConsistInfo(e)

pkg/loadbalance/lbcache/cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ func NewBalancerFactory(resolver discovery.Resolver, balancer loadbalance.Loadba
120120
}
121121
uniqueKey := cacheKey(resolver.Name(), balancer.Name(), opts)
122122
val, _, _ := balancerFactoriesSfg.CheckAndDo(uniqueKey,
123-
func() (any, bool) {
124-
return balancerFactories.Load(uniqueKey)
123+
func(key string) (any, bool) {
124+
return balancerFactories.Load(key)
125125
},
126126
func() (interface{}, error) {
127127
b := newBalancerFactory(resolver, balancer, opts)
@@ -158,8 +158,8 @@ func renameResultCacheKey(res *discovery.Result, resolverName string) {
158158
func (b *BalancerFactory) Get(ctx context.Context, target rpcinfo.EndpointInfo) (*Balancer, error) {
159159
desc := b.resolver.Target(ctx, target)
160160
val, err, _ := b.sfg.CheckAndDo(desc,
161-
func() (any, bool) {
162-
return b.cache.Load(desc)
161+
func(key string) (any, bool) {
162+
return b.cache.Load(key)
163163
},
164164
func() (interface{}, error) {
165165
res, err := b.resolver.Resolve(ctx, desc)

pkg/loadbalance/lbcache/shared_ticker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var (
3333
func getSharedTicker(b *Balancer, refreshInterval time.Duration) *utils.SharedTicker {
3434
v, _, _ := sharedTickersSfg.CheckAndDo(
3535
refreshInterval.String(),
36-
func() (any, bool) {
37-
return sharedTickers.Load(refreshInterval)
36+
func(key string) (any, bool) {
37+
return sharedTickers.Load(key)
3838
},
3939
func() (interface{}, error) {
4040
st := utils.NewSharedTicker(refreshInterval)

pkg/loadbalance/weighted_balancer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (wb *weightedBalancer) GetPicker(e discovery.Result) Picker {
7373
return picker
7474
}
7575
picker, _, _ := wb.sfg.CheckAndDo(e.CacheKey,
76-
func() (any, bool) {
77-
return wb.pickerCache.Load(e.CacheKey)
76+
func(key string) (any, bool) {
77+
return wb.pickerCache.Load(key)
7878
},
7979
func() (interface{}, error) {
8080
p := wb.createPicker(e)

0 commit comments

Comments
 (0)