Skip to content

Commit

Permalink
optim: shallow copy block header in Context
Browse files Browse the repository at this point in the history
the proto clone introduce significant memory allocations in our
profiling, because the method is called multiple times for each
transaction, and the deep clone seems unnesserary, the header is a plain
struct without pointers inside (unless one that's in time.Time), a shallow value copy seems good enough.
  • Loading branch information
yihuang committed Apr 5, 2024
1 parent 558c950 commit 20ee641
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (types) [#18963](https://github.com/cosmos/cosmos-sdk/pull/18963) Swap out amino json encoding of `ABCIMessageLogs` for std lib json encoding
* (x/auth) [#19651](https://github.com/cosmos/cosmos-sdk/pull/19651) Allow empty public keys in `GetSignBytesAdapter`.
* (x/genutil) [#19735](https://github.com/cosmos/cosmos-sdk/pull/19735) Update genesis api to match new `appmodule.HasGenesis` interface.
* (server) [#]() Return BlockHeader by shallow copy in server Context.

### 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 @@ -6,7 +6,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 @@ -91,10 +90,9 @@ func (c Context) StreamingManager() storetypes.StreamingManager { return c.strea
func (c Context) CometInfo() comet.Info { return c.cometInfo }
func (c Context) HeaderInfo() header.Info { return c.headerInfo }

// clone the header before returning
// BlockHeader returns the header is returned by value.
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

0 comments on commit 20ee641

Please sign in to comment.