Skip to content
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

slices, for loop copy, min/max update #2006

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 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
6 changes: 2 additions & 4 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"errors"
"fmt"
"runtime"
"sort"
"slices"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -400,9 +400,7 @@
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 a.Cmp(&b) })
AnkushinDaniil marked this conversation as resolved.
Show resolved Hide resolved

// update per-contract storage Tries concurrently
contractUpdaters := pool.NewWithResults[*bufferedTransactionWithAddress]().WithErrors().WithMaxGoroutines(runtime.GOMAXPROCS(0))
Expand All @@ -423,7 +421,7 @@
}

// we sort bufferedTxns in ascending contract address order to achieve an additional speedup
sort.Slice(bufferedTxns, func(i, j int) bool {

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.22.2, ubuntu-latest)

undefined: sort

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.22.2, ubuntu-latest)

undefined: sort

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / lint

undefined: sort) (typecheck)

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / lint

undefined: sort) (typecheck)

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.22.2, macos-latest)

undefined: sort

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.22.2, macos-latest)

undefined: sort

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.22.2, ubuntu-arm64-2-core)

undefined: sort

Check failure on line 424 in core/state.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.22.2, ubuntu-arm64-2-core)

undefined: sort
return bufferedTxns[i].addr.Cmp(bufferedTxns[j].addr) < 0
})

Expand Down
6 changes: 2 additions & 4 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 Expand Up @@ -698,9 +697,8 @@ func ParseBlockVersion(protocolVersion string) (*semver.Version, error) {
}

sep := "."
digits := strings.Split(protocolVersion, sep)
// pad with 3 zeros in case version has less than 3 digits
digits = append(digits, []string{"0", "0", "0"}...)
digits := append(strings.Split(protocolVersion, sep), []string{"0", "0", "0"}...)
AnkushinDaniil marked this conversation as resolved.
Show resolved Hide resolved

// get first 3 digits only
return semver.NewVersion(strings.Join(digits[:3], sep))
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
Loading