From 89784b2e0ab573ffd220c9689dd9911b83984caa Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 14 Feb 2024 10:03:07 +0300 Subject: [PATCH] [#972] Use min/max builtins Signed-off-by: Evgenii Stratonikov --- .../modules/morph/initialize/initialize_register.go | 5 +---- pkg/innerring/processors/netmap/cleanup_table.go | 5 +---- pkg/local_object_storage/blobovnicza/iterate.go | 8 -------- pkg/local_object_storage/engine/list.go | 5 +---- pkg/local_object_storage/shard/gc.go | 12 ++---------- pkg/morph/client/notary.go | 6 +----- pkg/services/object/internal/client/client.go | 5 +---- pkg/services/object/transport_splitter.go | 5 +---- pkg/services/policer/policer_test.go | 5 +---- pkg/services/tree/sync.go | 8 ++------ 10 files changed, 11 insertions(+), 53 deletions(-) diff --git a/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_register.go b/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_register.go index 6a0a88016..4c6607f9a 100644 --- a/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_register.go +++ b/cmd/frostfs-adm/internal/modules/morph/initialize/initialize_register.go @@ -100,10 +100,7 @@ func registerCandidates(c *helper.InitializeContext) error { // Register candidates in batches in order to overcome the signers amount limit. // See: https://github.com/nspcc-dev/neo-go/blob/master/pkg/core/transaction/transaction.go#L27 for i := 0; i < need; i += registerBatchSize { - start, end := i, i+registerBatchSize - if end > need { - end = need - } + start, end := i, min(i+registerBatchSize, need) // This check is sound because transactions are accepted/rejected atomically. if have >= end { continue diff --git a/pkg/innerring/processors/netmap/cleanup_table.go b/pkg/innerring/processors/netmap/cleanup_table.go index 705e21d99..c18611569 100644 --- a/pkg/innerring/processors/netmap/cleanup_table.go +++ b/pkg/innerring/processors/netmap/cleanup_table.go @@ -82,10 +82,7 @@ func (c *cleanupTable) touch(keyString string, now uint64, binNodeInfo []byte) b result := !ok || access.removeFlag || !bytes.Equal(access.binNodeInfo, binNodeInfo) access.removeFlag = false // reset remove flag on each touch - if now > access.epoch { - access.epoch = now - } - + access.epoch = max(access.epoch, now) access.binNodeInfo = binNodeInfo // update binary node info c.lastAccess[keyString] = access diff --git a/pkg/local_object_storage/blobovnicza/iterate.go b/pkg/local_object_storage/blobovnicza/iterate.go index 32b0ccea7..e87da751d 100644 --- a/pkg/local_object_storage/blobovnicza/iterate.go +++ b/pkg/local_object_storage/blobovnicza/iterate.go @@ -56,14 +56,6 @@ func (b *Blobovnicza) iterateBounds(useObjLimitBound bool, f func(uint64, uint64 return nil } -func max(a, b uint64) uint64 { - if a > b { - return a - } - - return b -} - // IterationElement represents a unit of elements through which Iterate operation passes. type IterationElement struct { addr oid.Address diff --git a/pkg/local_object_storage/engine/list.go b/pkg/local_object_storage/engine/list.go index f9229a2b1..7245caeeb 100644 --- a/pkg/local_object_storage/engine/list.go +++ b/pkg/local_object_storage/engine/list.go @@ -134,10 +134,7 @@ func (e *StorageEngine) ListWithCursor(ctx context.Context, prm ListWithCursorPr continue } - count := prm.count - uint32(len(result)) - if count > batchSize { - count = batchSize - } + count := min(prm.count-uint32(len(result)), batchSize) var shardPrm shard.ListWithCursorPrm shardPrm.WithCount(count) diff --git a/pkg/local_object_storage/shard/gc.go b/pkg/local_object_storage/shard/gc.go index 83be3259a..ef8e97d34 100644 --- a/pkg/local_object_storage/shard/gc.go +++ b/pkg/local_object_storage/shard/gc.go @@ -343,16 +343,8 @@ func (s *Shard) removeGarbage(pctx context.Context) (result gcRunResult) { } func (s *Shard) getExpiredObjectsParameters() (workerCount, batchSize int) { - workerCount = minExpiredWorkers - batchSize = minExpiredBatchSize - - if s.gc.gcCfg.expiredCollectorBatchSize > batchSize { - batchSize = s.gc.gcCfg.expiredCollectorBatchSize - } - - if s.gc.gcCfg.expiredCollectorWorkerCount > workerCount { - workerCount = s.gc.gcCfg.expiredCollectorWorkerCount - } + workerCount = max(minExpiredWorkers, s.gc.gcCfg.expiredCollectorWorkerCount) + batchSize = max(minExpiredBatchSize, s.gc.gcCfg.expiredCollectorBatchSize) return } diff --git a/pkg/morph/client/notary.go b/pkg/morph/client/notary.go index 5b817a805..febdef4d0 100644 --- a/pkg/morph/client/notary.go +++ b/pkg/morph/client/notary.go @@ -162,11 +162,7 @@ func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uin return util.Uint256{}, fmt.Errorf("can't get previous expiration value: %w", err) } - till := int64(bc + delta) - if till < currentTill { - till = currentTill - } - + till := max(int64(bc+delta), currentTill) return c.depositNotary(amount, till) } diff --git a/pkg/services/object/internal/client/client.go b/pkg/services/object/internal/client/client.go index dd2de0fd1..6477faed1 100644 --- a/pkg/services/object/internal/client/client.go +++ b/pkg/services/object/internal/client/client.go @@ -343,10 +343,7 @@ func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, e return nil, new(apistatus.ObjectOutOfRange) } - ln := prm.ln - if ln > maxInitialBufferSize { - ln = maxInitialBufferSize - } + ln := min(prm.ln, maxInitialBufferSize) w := bytes.NewBuffer(make([]byte, ln)) _, err = io.CopyN(w, rdr, int64(prm.ln)) diff --git a/pkg/services/object/transport_splitter.go b/pkg/services/object/transport_splitter.go index 2d9810cd3..54e49cb12 100644 --- a/pkg/services/object/transport_splitter.go +++ b/pkg/services/object/transport_splitter.go @@ -164,10 +164,7 @@ func (s *searchStreamMsgSizeCtrl) Send(resp *object.SearchResponse) error { newResp.SetBody(body) } - cut := s.addrAmount - if cut > ln { - cut = ln - } + cut := min(s.addrAmount, ln) body.SetIDList(ids[:cut]) newResp.SetMetaHeader(resp.GetMetaHeader()) diff --git a/pkg/services/policer/policer_test.go b/pkg/services/policer/policer_test.go index 50f206fd9..be0974c39 100644 --- a/pkg/services/policer/policer_test.go +++ b/pkg/services/policer/policer_test.go @@ -388,10 +388,7 @@ func (it *sliceKeySpaceIterator) Next(_ context.Context, size uint32) ([]objectc if it.cur >= len(it.objs) { return nil, engine.ErrEndOfListing } - end := it.cur + int(size) - if end > len(it.objs) { - end = len(it.objs) - } + end := min(it.cur+int(size), len(it.objs)) ret := it.objs[it.cur:end] it.cur = end return ret, nil diff --git a/pkg/services/tree/sync.go b/pkg/services/tree/sync.go index 2a19ae18a..1fa9a2b6c 100644 --- a/pkg/services/tree/sync.go +++ b/pkg/services/tree/sync.go @@ -167,9 +167,7 @@ func mergeOperationStreams(streams []chan *pilorama.Move, merged chan<- *piloram merged <- ms[minTimeMoveIndex] height := ms[minTimeMoveIndex].Time if ms[minTimeMoveIndex] = <-streams[minTimeMoveIndex]; ms[minTimeMoveIndex] == nil { - if minStreamedLastHeight > height { - minStreamedLastHeight = height - } + minStreamedLastHeight = min(minStreamedLastHeight, height) } } @@ -203,9 +201,7 @@ func (s *Service) applyOperationStream(ctx context.Context, cid cid.ID, treeID s errGroup.Go(func() error { if err := s.forest.TreeApply(ctx, cid, treeID, m, true); err != nil { heightMtx.Lock() - if m.Time < unappliedOperationHeight { - unappliedOperationHeight = m.Time - } + unappliedOperationHeight = min(unappliedOperationHeight, m.Time) heightMtx.Unlock() return err }