diff --git a/go.mod b/go.mod index 0299eeca..cce317eb 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/kr/pretty v0.2.0 // indirect github.com/lucasjones/reggen v0.0.0-20180717132126-cdb49ff09d77 github.com/mitchellh/mapstructure v1.3.3 + github.com/neilotoole/errgroup v0.1.5 github.com/pkg/errors v0.9.1 // indirect github.com/segmentio/fasthash v1.0.3 github.com/stretchr/objx v0.1.1 // indirect diff --git a/go.sum b/go.sum index a5b7b9f5..6757e455 100644 --- a/go.sum +++ b/go.sum @@ -193,6 +193,8 @@ github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8 github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= +github.com/neilotoole/errgroup v0.1.5 h1:DxEGoIfFm5ooGicidR+okiHjoOaGRKFaSxDPVZuuu2I= +github.com/neilotoole/errgroup v0.1.5/go.mod h1:Q2nLGf+594h0CLBs/Mbg6qOr7GtqDK7C2S41udRnToE= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= diff --git a/parser/balance_changes.go b/parser/balance_changes.go index 9b6c01bd..69d23c46 100644 --- a/parser/balance_changes.go +++ b/parser/balance_changes.go @@ -128,9 +128,11 @@ func (p *Parser) BalanceChanges( } } - allChanges := []*BalanceChange{} + i := 0 + allChanges := make([]*BalanceChange, len(balanceChanges)) for _, change := range balanceChanges { - allChanges = append(allChanges, change) + allChanges[i] = change + i++ } return allChanges, nil diff --git a/storage/modules/balance_storage.go b/storage/modules/balance_storage.go index c4705451..5aff76bb 100644 --- a/storage/modules/balance_storage.go +++ b/storage/modules/balance_storage.go @@ -22,7 +22,7 @@ import ( "math/big" "sync" - "golang.org/x/sync/errgroup" + "github.com/neilotoole/errgroup" "github.com/coinbase/rosetta-sdk-go/asserter" "github.com/coinbase/rosetta-sdk-go/parser" @@ -194,7 +194,8 @@ func (b *BalanceStorage) AddingBlock( var newAccounts int var newAccountsLock sync.Mutex - g, gctx := errgroup.WithContext(ctx) + // Concurrent execution limited to runtime.NumCPU + g, gctx := errgroup.WithContextN(ctx, 0, 0) for i := range changes { // We need to set variable before calling goroutine // to avoid getting an updated pointer as loop iteration @@ -268,7 +269,8 @@ func (b *BalanceStorage) RemovingBlock( staleAccounts := []*types.AccountCurrency{} var staleAccountsMutex sync.Mutex - g, gctx := errgroup.WithContext(ctx) + // Concurrent execution limited to runtime.NumCPU + g, gctx := errgroup.WithContextN(ctx, 0, 0) for i := range changes { // We need to set variable before calling goroutine // to avoid getting an updated pointer as loop iteration