Skip to content

Commit

Permalink
small optimisations (cosmos#261)
Browse files Browse the repository at this point in the history
* mutex don't cover GetSigners

don't clone header in context

* Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

* update comment

---------

Signed-off-by: yihuang <[email protected]>
  • Loading branch information
yihuang authored Apr 8, 2024
1 parent 0bb12ec commit 56c1655
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#248](https://github.com/crypto-org-chain/cosmos-sdk/pull/248) Init btree store lazily to save allocations when no content insert into it.
* [#252](https://github.com/crypto-org-chain/cosmos-sdk/pull/252) Add `BlockGasWanted` to `Context` to support feemarket module.

### Improvements

* [#261](https://github.com/crypto-org-chain/cosmos-sdk/pull/261) `ctx.BlockHeader` don't do protobuf deep copy, shallow copy seems enough, reduce scope of mutex in `PriorityNonceMempool.Remove`.

## [Unreleased-Upstream]

### Bug Fixes
Expand Down
6 changes: 2 additions & 4 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/comet"
"cosmossdk.io/core/header"
Expand Down Expand Up @@ -109,10 +108,9 @@ func (c Context) TxCount() int { return c.txCou
func (c Context) BlockGasUsed() uint64 { return c.blockGasUsed }
func (c Context) BlockGasWanted() uint64 { return c.blockGasWanted }

// clone the header before returning
// BlockHeader returns the header by value (shallow copy).
func (c Context) BlockHeader() cmtproto.Header {
msg := proto.Clone(&c.header).(*cmtproto.Header)
return *msg
return c.header
}

// HeaderHash returns a copy of the header hash obtained during abci.RequestBeginBlock
Expand Down
5 changes: 3 additions & 2 deletions types/mempool/priority_nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,6 @@ func (mp *PriorityNonceMempool[C]) CountTx() int {
// Remove removes a transaction from the mempool in O(log n) time, returning an
// error if unsuccessful.
func (mp *PriorityNonceMempool[C]) Remove(tx sdk.Tx) error {
mp.mtx.Lock()
defer mp.mtx.Unlock()
sigs, err := mp.cfg.SignerExtractor.GetSigners(tx)
if err != nil {
return err
Expand All @@ -443,6 +441,9 @@ func (mp *PriorityNonceMempool[C]) Remove(tx sdk.Tx) error {
sender := sig.Signer.String()
nonce := sig.Sequence

mp.mtx.Lock()
defer mp.mtx.Unlock()

scoreKey := txMeta[C]{nonce: nonce, sender: sender}
score, ok := mp.scores[scoreKey]
if !ok {
Expand Down

0 comments on commit 56c1655

Please sign in to comment.