Skip to content

Commit

Permalink
slices, for loop copy, min/max update (#2006)
Browse files Browse the repository at this point in the history
* Update slices, for loop copy, min/max functions

* Revert append update

* Fix typo

* Fix linter

* Resolve merge conflict
  • Loading branch information
AnkushinDaniil authored Aug 5, 2024
1 parent 63701db commit a30d388
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 22 deletions.
3 changes: 1 addition & 2 deletions core/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ func TestBlockHash(t *testing.T) {
},
}

for _, testcase := range tests {
tc := testcase
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client := feeder.NewTestClient(t, &tc.chain)
Expand Down
5 changes: 2 additions & 3 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"runtime"
"slices"
"sort"

"github.com/NethermindEth/juno/core/crypto"
Expand Down Expand Up @@ -400,9 +401,7 @@ func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt
for key := range diffs {
keys = append(keys, key)
}
sort.SliceStable(keys, func(i, j int) bool {
return len(diffs[keys[i]]) > len(diffs[keys[j]])
})
slices.SortStableFunc(keys, func(a, b felt.Felt) int { return len(diffs[a]) - len(diffs[b]) })

// update per-contract storage Tries concurrently
contractUpdaters := pool.NewWithResults[*bufferedTransactionWithAddress]().WithErrors().WithMaxGoroutines(runtime.GOMAXPROCS(0))
Expand Down
3 changes: 1 addition & 2 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,7 @@ func l1HandlerTransactionHash(l *L1HandlerTransaction, n *utils.Network) (*felt.
}

func deployAccountTransactionHash(d *DeployAccountTransaction, n *utils.Network) (*felt.Felt, error) {
callData := []*felt.Felt{d.ClassHash, d.ContractAddressSalt}
callData = append(callData, d.ConstructorCallData...)
callData := append([]*felt.Felt{d.ClassHash, d.ContractAddressSalt}, d.ConstructorCallData...)
// There is no version 0 for deploy account
switch {
case d.Version.Is(1):
Expand Down
8 changes: 4 additions & 4 deletions db/pebble/iterator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pebble

import (
"slices"

"github.com/NethermindEth/juno/db"
"github.com/cockroachdb/pebble"
)
Expand All @@ -23,8 +25,7 @@ func (i *iterator) Key() []byte {
if key == nil {
return nil
}
buf := make([]byte, len(key))
copy(buf, key)
buf := slices.Clone(key)
return buf
}

Expand All @@ -34,8 +35,7 @@ func (i *iterator) Value() ([]byte, error) {
if err != nil || val == nil {
return nil, err
}
buf := make([]byte, len(val))
copy(buf, val)
buf := slices.Clone(val)
return buf, nil
}

Expand Down
1 change: 0 additions & 1 deletion l1/l1_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ func TestClient(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 0 additions & 2 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestBlockId(t *testing.T) {
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
var blockID rpc.BlockID
Expand Down Expand Up @@ -81,7 +80,6 @@ func TestBlockId(t *testing.T) {
},
}
for name, test := range failingTests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
var blockID rpc.BlockID
Expand Down
6 changes: 1 addition & 5 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,7 @@ func (s *Synchronizer) syncBlocks(syncCtx context.Context) {
}

func maxWorkers() int {
m, mProcs := 16, runtime.GOMAXPROCS(0)
if mProcs > m {
return m
}
return mProcs
return min(16, runtime.GOMAXPROCS(0)) //nolint:mnd
}

func (s *Synchronizer) setupWorkers() (*stream.Stream, *stream.Stream) {
Expand Down
1 change: 0 additions & 1 deletion upgrader/upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func TestUpgrader(t *testing.T) {
}

for description, test := range tests {
test := test
t.Run(description, func(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, paid
}

func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []core.Class) (json.RawMessage, json.RawMessage, error) { //nolint:lll
txnJSONs := []json.RawMessage{}
txnJSONs := make([]json.RawMessage, 0, len(txns))
for _, txn := range txns {
txnJSON, err := marshalTxn(txn)
if err != nil {
Expand All @@ -353,7 +353,7 @@ func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []co
txnJSONs = append(txnJSONs, txnJSON)
}

classJSONs := []json.RawMessage{}
classJSONs := make([]json.RawMessage, 0, len(declaredClasses))
for _, declaredClass := range declaredClasses {
declaredClassJSON, cErr := marshalClassInfo(declaredClass)
if cErr != nil {
Expand Down

0 comments on commit a30d388

Please sign in to comment.