Skip to content

Commit

Permalink
[1658]: Provide suggested sizes when making maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Oct 11, 2023
1 parent 9a19d4d commit 49c3bc5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions x/exchange/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (g GenesisState) Validate() error {
}
}

marketIDs := make(map[uint32]int)
marketIDs := make(map[uint32]int, len(g.Markets))
for i, market := range g.Markets {
if market.MarketId == 0 {
errs = append(errs, fmt.Errorf("invalid market[%d]: market id cannot be zero", i))
Expand All @@ -39,7 +39,7 @@ func (g GenesisState) Validate() error {
}

maxOrderID := uint64(0)
orderIDs := make(map[uint64]int)
orderIDs := make(map[uint64]int, len(g.Orders))
for i, order := range g.Orders {
if order.OrderId != 0 {
j, seen := orderIDs[order.OrderId]
Expand Down
8 changes: 4 additions & 4 deletions x/exchange/keeper/fulfillment.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func (k Keeper) FillBids(ctx sdk.Context, msg *exchange.MsgFillBidsRequest) erro
var totalSellerFee sdk.Coins
assetOutputs := make([]banktypes.Output, 0, len(msg.BidOrderIds))
priceInputs := make([]banktypes.Input, 0, len(msg.BidOrderIds))
addrIndex := make(map[string]int)
addrIndex := make(map[string]int, len(msg.BidOrderIds))
feeInputs := make([]banktypes.Input, 0, len(msg.BidOrderIds)+1)
feeAddrIndex := make(map[string]int)
feeAddrIndex := make(map[string]int, len(msg.BidOrderIds))
filledOrders := make([]*exchange.FilledOrder, 0, len(msg.BidOrderIds))
for _, order := range orders {
bidOrder := order.GetBidOrder()
Expand Down Expand Up @@ -192,9 +192,9 @@ func (k Keeper) FillAsks(ctx sdk.Context, msg *exchange.MsgFillAsksRequest) erro
var errs []error
assetInputs := make([]banktypes.Input, 0, len(msg.AskOrderIds))
priceOutputs := make([]banktypes.Output, 0, len(msg.AskOrderIds))
addrIndex := make(map[string]int)
addrIndex := make(map[string]int, len(msg.AskOrderIds))
feeInputs := make([]banktypes.Input, 0, len(msg.AskOrderIds)+1)
feeAddrIndex := make(map[string]int)
feeAddrIndex := make(map[string]int, len(msg.AskOrderIds))
filledOrders := make([]*exchange.FilledOrder, 0, len(msg.AskOrderIds))
for _, order := range orders {
askOrder := order.GetAskOrder()
Expand Down
20 changes: 10 additions & 10 deletions x/exchange/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (m Market) Validate() error {
// ValidateFeeOptions returns an error if any of the provide coin values is not a valid fee option.
func ValidateFeeOptions(field string, options []sdk.Coin) error {
var errs []error
denoms := make(map[string]bool)
dups := make(map[string]bool)
denoms := make(map[string]bool, len(options))
dups := make(map[string]bool, len(options))
for _, coin := range options {
if denoms[coin.Denom] {
if !dups[coin.Denom] {
Expand Down Expand Up @@ -115,7 +115,7 @@ func ValidateFeeRatios(sellerRatios, buyerRatios []FeeRatio) error {
}

// For the buyer ones, there can be multiple per price denom.
buyerPriceDenomsMap := make(map[string]bool)
buyerPriceDenomsMap := make(map[string]bool, len(buyerRatios))
buyerPriceDenoms := make([]string, 0)
for _, ratio := range buyerRatios {
if !buyerPriceDenomsMap[ratio.Price.Denom] {
Expand Down Expand Up @@ -145,7 +145,7 @@ func ValidateSellerFeeRatios(ratios []FeeRatio) error {
return nil
}

seen := make(map[string]bool)
seen := make(map[string]bool, len(ratios))
dups := make(map[string]bool)
var errs []error
for _, ratio := range ratios {
Expand Down Expand Up @@ -177,7 +177,7 @@ func ValidateBuyerFeeRatios(ratios []FeeRatio) error {
return nil
}

seen := make(map[string]bool)
seen := make(map[string]bool, len(ratios))
dups := make(map[string]bool)
var errs []error
for _, ratio := range ratios {
Expand Down Expand Up @@ -337,14 +337,14 @@ func ValidateRatioDenoms(sellerRatios, buyerRatios []FeeRatio) []error {
if len(sellerRatios) > 0 && len(buyerRatios) > 0 {
// We only need to check the price denoms if *both* types have an entry.
sellerPriceDenoms := make([]string, len(sellerRatios))
sellerPriceDenomsKnown := make(map[string]bool)
sellerPriceDenomsKnown := make(map[string]bool, len(sellerRatios))
for i, ratio := range sellerRatios {
sellerPriceDenoms[i] = ratio.Price.Denom
sellerPriceDenomsKnown[ratio.Price.Denom] = true
}

buyerPriceDenoms := make([]string, 0, len(sellerRatios))
buyerPriceDenomsKnown := make(map[string]bool)
buyerPriceDenomsKnown := make(map[string]bool, len(sellerRatios))
for _, ratio := range buyerRatios {
if !buyerPriceDenomsKnown[ratio.Price.Denom] {
buyerPriceDenoms = append(buyerPriceDenoms, ratio.Price.Denom)
Expand Down Expand Up @@ -414,7 +414,7 @@ func ValidateAccessGrantsField(field string, accessGrants []AccessGrant) error {
field += " "
}
errs := make([]error, len(accessGrants))
seen := make(map[string]bool)
seen := make(map[string]bool, len(accessGrants))
dups := make(map[string]bool)
for i, ag := range accessGrants {
if seen[ag.Address] && !dups[ag.Address] {
Expand Down Expand Up @@ -446,7 +446,7 @@ func (a AccessGrant) ValidateInField(field string) error {
if len(a.Permissions) == 0 {
return fmt.Errorf("invalid %saccess grant: no permissions provided for %s", field, a.Address)
}
seen := make(map[Permission]bool)
seen := make(map[Permission]bool, len(a.Permissions))
for _, perm := range a.Permissions {
if seen[perm] {
return fmt.Errorf("invalid %saccess grant: %s appears multiple times for %s", field, perm.SimpleString(), a.Address)
Expand Down Expand Up @@ -568,7 +568,7 @@ func ValidateReqAttrsAreNormalized(field string, attrs []string) error {
// ValidateReqAttrs makes sure that each provided attribute is valid and that no duplicate entries are provided.
func ValidateReqAttrs(field string, attrs []string) error {
var errs []error
seen := make(map[string]bool)
seen := make(map[string]bool, len(attrs))
bad := make(map[string]bool)
for _, attr := range attrs {
normalized := nametypes.NormalizeName(attr)
Expand Down
2 changes: 1 addition & 1 deletion x/exchange/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (m MsgMarketManagePermissionsRequest) ValidateBasic() error {
errs = append(errs, err)
}

toRevokeByAddr := make(map[string]AccessGrant)
toRevokeByAddr := make(map[string]AccessGrant, len(m.ToRevoke))
for _, ag := range m.ToRevoke {
if ContainsString(m.RevokeAll, ag.Address) {
errs = append(errs, fmt.Errorf("address %s appears in both the revoke-all and to-revoke fields", ag.Address))
Expand Down
2 changes: 1 addition & 1 deletion x/exchange/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var _ OrderI = (*Order)(nil)
// findDuplicateIds returns all order ids that appear two or more times in the provided slice.
func findDuplicateIds(orderIDs []uint64) []uint64 {
var rv []uint64
seen := make(map[uint64]bool)
seen := make(map[uint64]bool, len(orderIDs))
dups := make(map[uint64]bool)
for _, orderID := range orderIDs {
if seen[orderID] && !dups[orderID] {
Expand Down

0 comments on commit 49c3bc5

Please sign in to comment.