From 32607b54f1fb17420b35616e3461ff5052f91194 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Thu, 26 Apr 2018 13:09:29 -0400 Subject: [PATCH] 12708: Precompute len(slots) to reduce mallocs. Rename setupCaches. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- services/keep-balance/balance.go | 10 +++++++--- services/keep-balance/balance_test.go | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/services/keep-balance/balance.go b/services/keep-balance/balance.go index 7ad2d484ae..5359bc1eaf 100644 --- a/services/keep-balance/balance.go +++ b/services/keep-balance/balance.go @@ -51,6 +51,7 @@ type Balancer struct { MinMtime int64 classes []string + mounts int mountsByClass map[string]map[*KeepMount]bool collScanned int serviceRoots map[string]string @@ -358,7 +359,7 @@ func (bal *Balancer) ComputeChangeSets() { // This just calls balanceBlock() once for each block, using a // pool of worker goroutines. defer timeMe(bal.Logger, "ComputeChangeSets")() - bal.setupCaches() + bal.setupLookupTables() type balanceTask struct { blkid arvados.SizedDigest @@ -391,13 +392,16 @@ func (bal *Balancer) ComputeChangeSets() { bal.collectStatistics(results) } -func (bal *Balancer) setupCaches() { +func (bal *Balancer) setupLookupTables() { bal.serviceRoots = make(map[string]string) bal.classes = []string{"default"} bal.mountsByClass = map[string]map[*KeepMount]bool{"default": {}} + bal.mounts = 0 for _, srv := range bal.KeepServices { bal.serviceRoots[srv.UUID] = srv.UUID for _, mnt := range srv.mounts { + bal.mounts++ + // All mounts on a read-only service are // effectively read-only. mnt.ReadOnly = mnt.ReadOnly || srv.ReadOnly @@ -457,7 +461,7 @@ func (bal *Balancer) balanceBlock(blkid arvados.SizedDigest, blk *BlockState) ba } // Build a list of all slots (one per mounted volume). - var slots []slot + slots := make([]slot, 0, bal.mounts) for _, srv := range bal.KeepServices { for _, mnt := range srv.mounts { var repl *Replica diff --git a/services/keep-balance/balance_test.go b/services/keep-balance/balance_test.go index 4741a58e29..cfdd47fc91 100644 --- a/services/keep-balance/balance_test.go +++ b/services/keep-balance/balance_test.go @@ -362,7 +362,7 @@ func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) { // the appropriate changes for that block have been added to the // changesets. func (bal *balancerSuite) try(c *check.C, t tester) { - bal.setupCaches() + bal.setupLookupTables() blk := &BlockState{ Replicas: bal.replList(t.known, t.current), Desired: t.desired, -- 2.30.2