Skip to content

Commit

Permalink
replace cmp.Min/Max with built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed May 28, 2024
1 parent 9ca7e0f commit 502329d
Show file tree
Hide file tree
Showing 29 changed files with 92 additions and 129 deletions.
13 changes: 7 additions & 6 deletions cmd/integration/commands/refetence_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import (
"sync/atomic"
"time"

"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"

common2 "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/backup"
mdbx2 "github.com/ledgerwatch/erigon-lib/kv/mdbx"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/turbo/debug"
"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
)

var stateBuckets = []string{
Expand Down Expand Up @@ -262,7 +263,7 @@ func mdbxTopDup(ctx context.Context, chaindata string, bucket string, logger log

var _max int
for _, i := range cnt {
_max = cmp.Max(i, _max)
_max = max(i, _max)
}
for k, i := range cnt {
if i > _max-10 {
Expand Down
6 changes: 2 additions & 4 deletions cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import (
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/secp256k1"
"github.com/spf13/cobra"
"golang.org/x/sync/semaphore"

"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"

chain2 "github.com/ledgerwatch/erigon-lib/chain"
common2 "github.com/ledgerwatch/erigon-lib/common"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/config3"
Expand Down Expand Up @@ -866,7 +864,7 @@ func stageHeaders(db kv.RwDB, ctx context.Context, logger log.Logger) error {
if unwind > progress {
unwindTo = 1 // keep genesis
} else {
unwindTo = uint64(cmp.Max(1, int(progress)-int(unwind)))
unwindTo = uint64(max(1, int(progress)-int(unwind)))
}

if err = stages.SaveStageProgress(tx, stages.Headers, unwindTo); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/ledgerwatch/erigon-lib/chain/networkname"
"github.com/ledgerwatch/erigon-lib/chain/snapcfg"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/metrics"
libkzg "github.com/ledgerwatch/erigon-lib/crypto/kzg"
Expand Down Expand Up @@ -408,7 +407,7 @@ var (
DBReadConcurrencyFlag = cli.IntFlag{
Name: "db.read.concurrency",
Usage: "Does limit amount of parallel db reads. Default: equal to GOMAXPROCS (or number of CPU)",
Value: cmp.Min(cmp.Max(10, runtime.GOMAXPROCS(-1)*64), 9_000),
Value: min(max(10, runtime.GOMAXPROCS(-1)*64), 9_000),
}
RpcAccessListFlag = cli.StringFlag{
Name: "rpc.accessList",
Expand Down
6 changes: 2 additions & 4 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ import (
"math/big"
"time"

"github.com/ledgerwatch/erigon-lib/kv/dbutils"

"github.com/gballet/go-verkle"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/dbutils"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"

"github.com/ledgerwatch/erigon/core/types"
Expand Down Expand Up @@ -1037,7 +1035,7 @@ func PruneBlocks(tx kv.RwTx, blockTo uint64, blocksDeleteLimit int) error {
return err
}
blockFrom := binary.BigEndian.Uint64(firstK)
stopAtBlock := cmp.Min(blockTo, blockFrom+uint64(blocksDeleteLimit))
stopAtBlock := min(blockTo, blockFrom+uint64(blocksDeleteLimit))

var b *types.BodyForStorage

Expand Down
14 changes: 0 additions & 14 deletions erigon-lib/common/cmp/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ func InRange[T constraints.Ordered](min, max, val T) T {
return val
}

func Min[T constraints.Ordered](a, b T) T {
if a <= b {
return a
}
return b
}

func Max[T constraints.Ordered](a, b T) T {
if a >= b {
return a
}
return b
}

func Compare[T constraints.Ordered](a, b T) int {
switch {
case a < b:
Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/downloader/downloadercfg/downloadercfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Default() *torrent.ClientConfig {
// better don't increase because erigon periodically producing "new seedable files" - and adding them to downloader.
// it must not impact chain tip sync - so, limit resources to minimum by default.
// but when downloader is started as a separated process - rise it to max
//torrentConfig.PieceHashersPerTorrent = cmp.Max(1, runtime.NumCPU()-1)
//torrentConfig.PieceHashersPerTorrent = max(1, runtime.NumCPU()-1)

torrentConfig.MinDialTimeout = 6 * time.Second //default: 3s
torrentConfig.HandshakesTimeout = 8 * time.Second //default: 4s
Expand Down
5 changes: 2 additions & 3 deletions erigon-lib/mmap/total_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package mmap
import (
"runtime/debug"

"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/pbnjay/memory"
)

func TotalMemory() uint64 {
mem := memory.TotalMemory()

if cgroupsMemLimit, err := cgroupsMemoryLimit(); (err == nil) && (cgroupsMemLimit > 0) {
mem = cmp.Min(mem, cgroupsMemLimit)
mem = min(mem, cgroupsMemLimit)
}

if goMemLimit := debug.SetMemoryLimit(-1); goMemLimit > 0 {
mem = cmp.Min(mem, uint64(goMemLimit))
mem = min(mem, uint64(goMemLimit))
}

return mem
Expand Down
3 changes: 1 addition & 2 deletions erigon-lib/seg/compress_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"path/filepath"
"testing"

"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/log/v3"
)

Expand All @@ -43,7 +42,7 @@ func FuzzCompress(f *testing.F) {
if pos[i] == 0 {
continue
}
next := cmp.Min(j+int(pos[i]*10), len(x)-1)
next := min(j+int(pos[i]*10), len(x)-1)
bbb := x[j:next]
a = append(a, bbb)
j = next
Expand Down
3 changes: 1 addition & 2 deletions erigon-lib/seg/decompress_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"testing"

"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/log/v3"
)

Expand All @@ -27,7 +26,7 @@ func FuzzDecompressMatch(f *testing.F) {
if pos[i] == 0 {
continue
}
next := cmp.Min(j+int(pos[i]*10), len(x)-1)
next := min(j+int(pos[i]*10), len(x)-1)
bbb := x[j:next]
a = append(a, bbb)
j = next
Expand Down
4 changes: 1 addition & 3 deletions erigon-lib/seg/silkworm_seg_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ledgerwatch/erigon-lib/common/cmp"
)

func makeSegFilePath(path string, suffix string) string {
Expand Down Expand Up @@ -103,7 +101,7 @@ func NewRandPattern(r *rand.Rand, patternLen int) RandPattern {
}

func (p RandPattern) CopyTo(word []byte, offset int) {
copy(word[offset:cmp.Min(offset+len(p.pattern), len(word))], p.pattern)
copy(word[offset:min(offset+len(p.pattern), len(word))], p.pattern)
}

func generatePatterns(r *rand.Rand) []RandPattern {
Expand Down
5 changes: 2 additions & 3 deletions erigon-lib/state/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ import (
"strconv"
"time"

"github.com/ledgerwatch/erigon-lib/kv/backup"
btree2 "github.com/tidwall/btree"
"golang.org/x/sync/errgroup"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/background"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/backup"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
"github.com/ledgerwatch/erigon-lib/kv/iter"
"github.com/ledgerwatch/erigon-lib/kv/order"
Expand Down Expand Up @@ -1598,7 +1597,7 @@ func (ht *HistoryRoTx) iterateChangedFrozen(fromTxNum, toTxNum int, asc order.By

hi := &HistoryChangesIterFiles{
hc: ht,
startTxNum: cmp.Max(0, uint64(fromTxNum)),
startTxNum: max(0, uint64(fromTxNum)),
endTxNum: toTxNum,
limit: limit,
}
Expand Down
12 changes: 5 additions & 7 deletions erigon-lib/state/inverted_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,26 @@ import (
"sync"
"time"

"github.com/ledgerwatch/erigon-lib/common/assert"

"github.com/RoaringBitmap/roaring/roaring64"
"github.com/ledgerwatch/erigon-lib/kv/backup"
"github.com/ledgerwatch/erigon-lib/seg"
"github.com/ledgerwatch/log/v3"
"github.com/spaolacci/murmur3"
btree2 "github.com/tidwall/btree"
"golang.org/x/sync/errgroup"

"github.com/ledgerwatch/erigon-lib/common/assert"
"github.com/ledgerwatch/erigon-lib/common/background"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/backup"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
"github.com/ledgerwatch/erigon-lib/kv/iter"
"github.com/ledgerwatch/erigon-lib/kv/order"
"github.com/ledgerwatch/erigon-lib/recsplit"
"github.com/ledgerwatch/erigon-lib/recsplit/eliasfano32"
"github.com/ledgerwatch/erigon-lib/seg"
)

type InvertedIndex struct {
Expand Down Expand Up @@ -693,7 +691,7 @@ func (iit *InvertedIndexRoTx) smallestTxNum(tx kv.Tx) uint64 {
fst, _ := kv.FirstKey(tx, iit.ii.indexKeysTable)
if len(fst) > 0 {
fstInDb := binary.BigEndian.Uint64(fst)
return cmp.Min(fstInDb, math.MaxUint64)
return min(fstInDb, math.MaxUint64)
}
return math.MaxUint64
}
Expand All @@ -702,7 +700,7 @@ func (iit *InvertedIndexRoTx) highestTxNum(tx kv.Tx) uint64 {
lst, _ := kv.LastKey(tx, iit.ii.indexKeysTable)
if len(lst) > 0 {
lstInDb := binary.BigEndian.Uint64(lst)
return cmp.Max(lstInDb, 0)
return max(lstInDb, 0)
}
return 0
}
Expand Down
18 changes: 8 additions & 10 deletions erigon-lib/state/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ import (
"path/filepath"
"strings"

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/common/background"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/background"
"github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/recsplit"
"github.com/ledgerwatch/erigon-lib/recsplit/eliasfano32"
"github.com/ledgerwatch/erigon-lib/seg"
Expand Down Expand Up @@ -68,7 +66,7 @@ func (ii *InvertedIndex) endIndexedTxNumMinimax(needFrozen bool) uint64 {
if item.index == nil || (needFrozen && !item.frozen) {
continue
}
_max = cmp.Max(_max, item.endTxNum)
_max = max(_max, item.endTxNum)
}
return true
})
Expand Down Expand Up @@ -98,11 +96,11 @@ func (h *History) endIndexedTxNumMinimax(needFrozen bool) uint64 {
if item.index == nil || (needFrozen && !item.frozen) {
continue
}
_max = cmp.Max(_max, item.endTxNum)
_max = max(_max, item.endTxNum)
}
return true
})
return cmp.Min(_max, h.InvertedIndex.endIndexedTxNumMinimax(needFrozen))
return min(_max, h.InvertedIndex.endIndexedTxNumMinimax(needFrozen))
}

type DomainRanges struct {
Expand Down Expand Up @@ -194,7 +192,7 @@ func (ht *HistoryRoTx) findMergeRange(maxEndTxNum, maxSpan uint64) HistoryRanges
}
endStep := item.endTxNum / ht.h.aggregationStep
spanStep := endStep & -endStep // Extract rightmost bit in the binary representation of endStep, this corresponds to size of maximally possible merge ending at endStep
span := cmp.Min(spanStep*ht.h.aggregationStep, maxSpan)
span := min(spanStep*ht.h.aggregationStep, maxSpan)
start := item.endTxNum - span
foundSuperSet := r.indexStartTxNum == item.startTxNum && item.endTxNum >= r.historyEndTxNum
if foundSuperSet {
Expand Down Expand Up @@ -243,7 +241,7 @@ func (iit *InvertedIndexRoTx) findMergeRange(maxEndTxNum, maxSpan uint64) *Merge
}
endStep := item.endTxNum / iit.ii.aggregationStep
spanStep := endStep & -endStep // Extract rightmost bit in the binary representation of endStep, this corresponds to size of maximally possible merge ending at endStep
span := cmp.Min(spanStep*iit.ii.aggregationStep, maxSpan)
span := min(spanStep*iit.ii.aggregationStep, maxSpan)
start := item.endTxNum - span
foundSuperSet := startTxNum == item.startTxNum && item.endTxNum >= endTxNum
if foundSuperSet {
Expand Down Expand Up @@ -328,7 +326,7 @@ func (ht *HistoryRoTx) maxTxNumInFiles(onlyFrozen bool) uint64 {
} else {
_max = ht.files[len(ht.files)-1].endTxNum
}
return cmp.Min(_max, ht.iit.maxTxNumInFiles(onlyFrozen))
return min(_max, ht.iit.maxTxNumInFiles(onlyFrozen))
}

func (iit *InvertedIndexRoTx) maxTxNumInFiles(onlyFrozen bool) uint64 {
Expand Down
13 changes: 6 additions & 7 deletions erigon-lib/txpool/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"sync"
"time"

"github.com/ledgerwatch/erigon-lib/common/cmp"

"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/direct"
"github.com/ledgerwatch/erigon-lib/gointerfaces/grpcutil"
Expand All @@ -34,9 +36,6 @@ import (
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/rlp"
types2 "github.com/ledgerwatch/erigon-lib/types"
"github.com/ledgerwatch/log/v3"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)

// Fetch connects to sentry and implements eth/66 protocol regarding the transaction
Expand Down Expand Up @@ -285,7 +284,7 @@ func (f *Fetch) handleInboundMessage(ctx context.Context, req *sentry.InboundMes

// limit to max 256 transactions in a reply
const hashSize = 32
hashes = hashes[:cmp.Min(len(hashes), 256*hashSize)]
hashes = hashes[:min(len(hashes), 256*hashSize)]

var txs [][]byte
responseSize := 0
Expand All @@ -298,7 +297,7 @@ func (f *Fetch) handleInboundMessage(ctx context.Context, req *sentry.InboundMes
break
}

txnHash := hashes[i:cmp.Min(i+hashSize, len(hashes))]
txnHash := hashes[i:min(i+hashSize, len(hashes))]
txn, err := f.pool.GetRlp(tx, txnHash)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 502329d

Please sign in to comment.