Skip to content

refactor: use the built-in max/min to simplify the code #5646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions exp/orderbook/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/stellar/go/price"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/xdr"
)

Expand Down Expand Up @@ -400,7 +399,7 @@ func (state *buyingGraphSearchState) consumeOffers(
) (xdr.Int64, error) {
nextAmount, err := consumeOffersForBuyingAsset(offers, currentAssetAmount)

return ordered.Max(nextAmount, currentBestAmount), err
return max(nextAmount, currentBestAmount), err
}

func (state *buyingGraphSearchState) considerPools() bool {
Expand Down
9 changes: 4 additions & 5 deletions historyarchive/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"strings"

"github.com/stellar/go/support/ordered"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -59,7 +58,7 @@ func (c CheckpointManager) NextCheckpoint(i uint32) uint32 {
v := uint64(i)
n := (((v + freq) / freq) * freq) - 1

return uint32(ordered.Min(n, 0xffffffff))
return uint32(min(n, 0xffffffff))
}

// GetCheckPoint gets the checkpoint containing information about the given ledger sequence
Expand All @@ -82,16 +81,16 @@ func (c CheckpointManager) GetCheckpointRange(i uint32) Range {
}

func (c CheckpointManager) MakeRange(low uint32, high uint32) Range {
high = ordered.Max(high, low)
high = max(high, low)
return Range{
Low: c.PrevCheckpoint(low),
High: c.NextCheckpoint(high),
}
}

func (r Range) clamp(other Range, cManager CheckpointManager) Range {
low := ordered.Max(r.Low, other.Low)
high := ordered.Min(r.High, other.High)
low := max(r.Low, other.Low)
high := min(r.High, other.High)
return cManager.MakeRange(low, high)
}

Expand Down
3 changes: 1 addition & 2 deletions ingest/cdp/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/stellar/go/ingest/ledgerbackend"
"github.com/stellar/go/support/datastore"
"github.com/stellar/go/support/log"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/xdr"
)

Expand Down Expand Up @@ -115,7 +114,7 @@ func ApplyLedgerMetadata(ledgerRange ledgerbackend.Range,
return fmt.Errorf("invalid end value for unbounded range, must be zero")
}

from := ordered.Max(2, ledgerRange.From())
from := max(2, ledgerRange.From())
ledgerBackend.PrepareRange(ctx, ledgerRange)

for ledgerSeq := from; ledgerSeq <= ledgerRange.To() || !ledgerRange.Bounded(); ledgerSeq++ {
Expand Down
4 changes: 1 addition & 3 deletions ingest/ledgerbackend/ledger_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/stellar/go/support/collections/heap"
"github.com/stellar/go/support/compressxdr"
"github.com/stellar/go/support/datastore"
"github.com/stellar/go/support/ordered"

"github.com/stellar/go/xdr"
)

Expand Down Expand Up @@ -58,7 +56,7 @@ func (bsb *BufferedStorageBackend) newLedgerBuffer(ledgerRange Range) (*ledgerBu
}
// ensure BufferSize does not exceed the total range
if ledgerRange.bounded {
bsb.config.BufferSize = uint32(ordered.Min(int(bsb.config.BufferSize), int(ledgerRange.to-ledgerRange.from)+1))
bsb.config.BufferSize = uint32(min(int(bsb.config.BufferSize), int(ledgerRange.to-ledgerRange.from)+1))
}
pq := heap.New(less, int(bsb.config.BufferSize))

Expand Down
3 changes: 1 addition & 2 deletions price/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"regexp"
"strconv"

"github.com/stellar/go/support/ordered"
"github.com/stellar/go/xdr"
)

Expand Down Expand Up @@ -164,7 +163,7 @@ func ConvertToBuyingUnits(sellingOfferAmount int64, sellingUnitsNeeded int64, pr
}

// pathPaymentAmountBought
result = ordered.Min(result, sellingUnitsNeeded)
result = min(result, sellingUnitsNeeded)
sellingUnitsExtracted := result

// pathPaymentAmountSold
Expand Down
3 changes: 1 addition & 2 deletions services/galexie/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/stellar/go/support/datastore"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/support/storage"
)

Expand Down Expand Up @@ -282,7 +281,7 @@ func (config *Config) adjustLedgerRange() {
config.StartLedger = config.DataStoreConfig.Schema.GetSequenceNumberStartBoundary(config.StartLedger)

// Ensure that the adjusted start ledger is at least 2.
config.StartLedger = ordered.Max(2, config.StartLedger)
config.StartLedger = max(2, config.StartLedger)

// Align the end ledger (for bounded cases) to the nearest "LedgersPerFile" boundary.
if config.EndLedger != 0 {
Expand Down
3 changes: 1 addition & 2 deletions services/horizon/internal/actions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/ledger"
hProblem "github.com/stellar/go/services/horizon/internal/render/problem"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/support/render/problem"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
Expand Down Expand Up @@ -560,7 +559,7 @@ func validateAndAdjustCursor(ledgerState *ledger.State, pq *db2.PageQuery) error
// that are removed as part of reaping to maintain the retention window.
if pq.Cursor == "" || errors.Is(err, &hProblem.BeforeHistory) {
pq.Cursor = toid.AfterLedger(
ordered.Max(0, ledgerState.CurrentStatus().HistoryElder-1),
max(0, ledgerState.CurrentStatus().HistoryElder-1),
).String()
return nil
}
Expand Down
9 changes: 4 additions & 5 deletions services/horizon/internal/db2/history/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)
Expand Down Expand Up @@ -212,7 +211,7 @@ func (q *Q) GetLedgerGapsInRange(ctx context.Context, start, end uint32) ([]Ledg
if start < oldestLedger {
result = append(result, LedgerRange{
StartSequence: start,
EndSequence: ordered.Min(end, oldestLedger-1),
EndSequence: min(end, oldestLedger-1),
})
}
if end <= oldestLedger {
Expand All @@ -232,14 +231,14 @@ func (q *Q) GetLedgerGapsInRange(ctx context.Context, start, end uint32) ([]Ledg
break
}
result = append(result, LedgerRange{
StartSequence: ordered.Max(gap.StartSequence, start),
EndSequence: ordered.Min(gap.EndSequence, end),
StartSequence: max(gap.StartSequence, start),
EndSequence: min(gap.EndSequence, end),
})
}

if latestLedger < end {
result = append(result, LedgerRange{
StartSequence: ordered.Max(latestLedger+1, start),
StartSequence: max(latestLedger+1, start),
EndSequence: end,
})
}
Expand Down
5 changes: 2 additions & 3 deletions services/horizon/internal/ingest/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/errors"
logpkg "github.com/stellar/go/support/log"
"github.com/stellar/go/support/ordered"
)

type rangeError struct {
Expand Down Expand Up @@ -123,12 +122,12 @@ func (ps *ParallelSystems) calculateParallelLedgerBatchSize(rangeSize uint32) ui

// ensure the batch size meets min threshold
if ps.minBatchSize > 0 {
batchSize = ordered.Max(batchSize, uint32(ps.minBatchSize))
batchSize = max(batchSize, uint32(ps.minBatchSize))
}

// ensure the batch size does not exceed max threshold
if ps.maxBatchSize > 0 {
batchSize = ordered.Min(batchSize, uint32(ps.maxBatchSize))
batchSize = min(batchSize, uint32(ps.maxBatchSize))
}

// round down to the nearest multiple of HistoryCheckpointLedgerInterval
Expand Down
16 changes: 0 additions & 16 deletions support/ordered/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ import (
"golang.org/x/exp/constraints"
)

// Min returns the smaller of the given items.
func Min[T constraints.Ordered](a, b T) T {
if a <= b {
return a
}
return b
}

// Min returns the larger of the given items.
func Max[T constraints.Ordered](a, b T) T {
if a > b {
return a
}
return b
}

// MinSlice returns the smallest element in a slice-like container.
func MinSlice[T constraints.Ordered](slice []T) T {
var smallest T
Expand Down
27 changes: 0 additions & 27 deletions support/ordered/math_test.go

This file was deleted.