Skip to content

Commit b37cd94

Browse files
committed
mmaprototype: pass in scratch stores for computeMeansForStoreSet
Previously, computeMeansForStoreSet allocated a new map on every call to deduplicate the provided stores list. This commit refactors it to reuse a scratchStores map (similar to scratchNodes). The caller now allocates this map once and stores it in the function scope or struct, reducing repeated allocations.
1 parent 49e5b1c commit b37cd94

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

pkg/kv/kvserver/allocator/mmaprototype/allocator_state.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ func (a *allocatorState) rebalanceStores(
356356
var storesToExclude storeIDPostingList
357357
var storesToExcludeForRange storeIDPostingList
358358
scratchNodes := map[roachpb.NodeID]*NodeLoad{}
359+
scratchStores := map[roachpb.StoreID]struct{}{}
359360
// The caller has a fixed concurrency limit it can move ranges at, when it
360361
// is the sender of the snapshot. So we don't want to create too many
361362
// changes, since then the allocator gets too far ahead of what has been
@@ -475,7 +476,7 @@ func (a *allocatorState) rebalanceStores(
475476
}
476477
var means meansLoad
477478
clear(scratchNodes)
478-
computeMeansForStoreSet(a.cs, &means, candsPL, scratchNodes)
479+
computeMeansForStoreSet(a.cs, &means, candsPL, scratchNodes, scratchStores)
479480
sls := a.cs.computeLoadSummary(ctx, store.StoreID, &means.storeLoad, &means.nodeLoad)
480481
log.KvDistribution.VInfof(ctx, 2, "considering lease-transfer r%v from s%v: candidates are %v", rangeID, store.StoreID, candsPL)
481482
if sls.dimSummary[CPURate] < overloadSlow {

pkg/kv/kvserver/allocator/mmaprototype/load.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ type meansMemo struct {
280280
constraintMatcher *constraintMatcher
281281
meansMap *clearableMemoMap[constraintsDisj, *meansForStoreSet]
282282

283-
scratchNodes map[roachpb.NodeID]*NodeLoad
283+
scratchNodes map[roachpb.NodeID]*NodeLoad
284+
scratchStores map[roachpb.StoreID]struct{}
284285
}
285286

286287
var meansForStoreSetSlicePool = sync.Pool{
@@ -320,7 +321,8 @@ func newMeansMemo(
320321
constraintMatcher: constraintMatcher,
321322
meansMap: newClearableMapMemo[constraintsDisj, *meansForStoreSet](
322323
meansForStoreSetAllocator{}, meansForStoreSetSlicePoolImpl{}),
323-
scratchNodes: map[roachpb.NodeID]*NodeLoad{},
324+
scratchNodes: map[roachpb.NodeID]*NodeLoad{},
325+
scratchStores: map[roachpb.StoreID]struct{}{},
324326
}
325327
}
326328

@@ -342,7 +344,7 @@ func (mm *meansMemo) getMeans(expr constraintsDisj) *meansForStoreSet {
342344
}
343345
means.constraintsDisj = expr
344346
mm.constraintMatcher.constrainStoresForExpr(expr, &means.stores)
345-
computeMeansForStoreSet(mm.loadInfoProvider, &means.meansLoad, means.stores, mm.scratchNodes)
347+
computeMeansForStoreSet(mm.loadInfoProvider, &means.meansLoad, means.stores, mm.scratchNodes, mm.scratchStores)
346348
return means
347349
}
348350

@@ -375,20 +377,21 @@ func computeMeansForStoreSet(
375377
means *meansLoad,
376378
stores []roachpb.StoreID,
377379
scratchNodes map[roachpb.NodeID]*NodeLoad,
380+
scratchStores map[roachpb.StoreID]struct{},
378381
) {
379382
if len(stores) == 0 {
380383
panic(fmt.Sprintf("no stores for meansForStoreSet: %v", *means))
381384
}
382385
clear(scratchNodes)
383-
seen := make(map[roachpb.StoreID]struct{})
386+
clear(scratchStores)
384387
n := 0
385388
for _, storeID := range stores {
386389
nodeID, sload := loadProvider.getStoreReportedLoad(storeID)
387-
if _, ok := seen[storeID]; ok {
390+
if _, ok := scratchStores[storeID]; ok {
388391
continue
389392
}
390393
n++
391-
seen[storeID] = struct{}{}
394+
scratchStores[storeID] = struct{}{}
392395
for j := range sload.reportedLoad {
393396
means.storeLoad.load[j] += sload.reportedLoad[j]
394397
if sload.capacity[j] == UnknownCapacity {

0 commit comments

Comments
 (0)