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

perf: Micro optimization to save one allocation per packet (backport … #75

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [`protoio`] Remove one allocation and new object call from `ReadMsg`,
leading to a 4% p2p message reading performance gain.
([\#3018](https://github.com/cometbft/cometbft/issues/3018)
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ linters-settings:
suggest-new: true
misspell:
locale: US
ignore-words:
- Cancelled
depguard:
rules:
main:
Expand Down
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

## Unreleased

* [#73](https://github.com/osmosis-labs/cometbft/pull/73) perf(consensus/blockexec): Add simplistic block validation cache
* [#74](https://github.com/osmosis-labs/cometbft/pull/74) perf(consensus): Minor speedup to mark late vote metrics
* [#75](https://github.com/osmosis-labs/cometbft/pull/75) perf(p2p): 4% speedup to readMsg by removing one allocation

## v0.37.4-v25-osmo-3

* [#61](https://github.com/osmosis-labs/cometbft/pull/61) refactor(p2p/connection): Slight refactor to sendManyPackets that helps highlight performance improvements (backport #2953) (#2978)
* [#62](https://github.com/osmosis-labs/cometbft/pull/62) perf(consensus/blockstore): Remove validate basic call from LoadBlock
* [#71](https://github.com/osmosis-labs/cometbft/pull/71) perf(consensus): Make mempool update async from Commit
* [#73](https://github.com/osmosis-labs/cometbft/pull/73) perf(consensus/blockexec): Add simplistic block validation cache
* [#74](https://github.com/osmosis-labs/cometbft/pull/74) perf(consensus): Minor speedup to mark late vote metrics
* [#59](https://github.com/osmosis-labs/cometbft/pull/59) `[blockstore]` Remove a redundant `Header.ValidateBasic` call in `LoadBlockMeta`, 75% reducing this time. ([\#2964](https://github.com/cometbft/cometbft/pull/2964))
* [#59](https://github.com/osmosis-labs/cometbft/pull/59) `[p2p/conn]` Speedup connection.WritePacketMsgTo, by reusing internal buffers rather than re-allocating. ([\#2986](https://github.com/cometbft/cometbft/pull/2986))

Expand Down
4 changes: 4 additions & 0 deletions libs/protoio/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ func (r *byteReader) ReadByte() (byte, error) {
}
return r.buf[0], nil
}

func (r *byteReader) resetBytesRead() {
r.bytesRead = 0
}
23 changes: 12 additions & 11 deletions libs/protoio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,25 @@ func NewDelimitedReader(r io.Reader, maxSize int) ReadCloser {
if c, ok := r.(io.Closer); ok {
closer = c
}
return &varintReader{r, nil, maxSize, closer}
return &varintReader{r, newByteReader(r), nil, maxSize, closer}
}

type varintReader struct {
r io.Reader
buf []byte
maxSize int
closer io.Closer
}

func (r *varintReader) ReadMsg(msg proto.Message) (int, error) {
r io.Reader
// ReadUvarint needs an io.ByteReader, and we also need to keep track of the
// number of bytes read, so we use our own byteReader. This can't be
// buffered, so the caller should pass a buffered io.Reader to avoid poor
// performance.
byteReader := newByteReader(r.r)
l, err := binary.ReadUvarint(byteReader)
n := byteReader.bytesRead
byteReader *byteReader
buf []byte
maxSize int
closer io.Closer
}

func (r *varintReader) ReadMsg(msg proto.Message) (int, error) {
r.byteReader.resetBytesRead()
l, err := binary.ReadUvarint(r.byteReader)
n := r.byteReader.bytesRead
if err != nil {
return n, err
}
Expand Down
2 changes: 0 additions & 2 deletions libs/pubsub/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ func (s *Subscription) Out() <-chan Message {

// Cancelled returns a channel that's closed when the subscription is
// terminated and supposed to be used in a select statement.
//
//nolint:misspell
func (s *Subscription) Cancelled() <-chan struct{} {
return s.canceled
}
Expand Down
2 changes: 1 addition & 1 deletion types/event_bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type EventBusSubscriber interface {

type Subscription interface {
Out() <-chan cmtpubsub.Message
Cancelled() <-chan struct{} //nolint: misspell
Cancelled() <-chan struct{}
Err() error
}

Expand Down
Loading