Skip to content

perf(p2p/conn): Remove p2p allocations for wrapping outbound packets - #3423

Merged
melekes merged 4 commits into
mainfrom
dev/remove_tmp2p_alloc
Jul 10, 2024
Merged

melekes merged 4 commits into
mainfrom
dev/remove_tmp2p_alloc

Conversation

@ValarDragon

@ValarDragon ValarDragon commented Jul 4, 2024 •

Copy link
Copy Markdown
Contributor

Closes #2951

Remove p2p allocations for wrapping outbound packets. Every packet send would require 3 new heap objects due to how we used pointers / did this wrapping. This costs us: 13% of all system heap allocations, and 20% of the CPU time of writing packets (under "normal" osmosis load. I don't have big blocks benchmarks after the secret connection improvement got in).

This should be a 25% speedup to channel.writePacketMsgTo, making writing getting closer to the bound of just proto-marshalling a packet.

This solution is simple (just make a buffer in the struct) since these packet sends are all single threaded.


PR checklist

  • Tests written/updated - N/A, existing tests cover safety here
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

@ValarDragon
ValarDragon requested a review from a team as a code owner July 4, 2024 10:08
@ValarDragon
ValarDragon requested a review from a team July 4, 2024 10:08
@ValarDragon

ValarDragon commented Jul 4, 2024 •

Copy link
Copy Markdown
Contributor Author

This did succesfully lower the CPU time as expected, and also appears to have lowered GC by ~10%. (Very interestingly, after this change the largest CPU time segment on Osmosis is no longer GC, now ranking at third, and is now instead the scheduler)

@cason cason left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work.

Left, again, some implementation-related questions.

Comment thread p2p/conn/connection.go Resolved
Comment thread p2p/conn/connection.go Resolved
Comment thread p2p/conn/connection.go Resolved
Comment thread p2p/conn/connection.go Resolved
@melekes
melekes requested a review from cason July 9, 2024 10:18
@melekes melekes added the p2p label Jul 9, 2024

@melekes melekes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ValarDragon ❤️

@cason

cason commented Jul 9, 2024

Copy link
Copy Markdown

Maybe a stupid question, but why do we use protobufs for low-level communication (packets, ping, pong)? Of course we have an overhead here, but what is the advantage of that? Those messages are super simple, size-prefixed messages, ordinary network exchanging []byte s.

@melekes

melekes commented Jul 10, 2024

Copy link
Copy Markdown
Collaborator

Maybe a stupid question, but why do we use protobufs for low-level communication (packets, ping, pong)? Of course we have an overhead here, but what is the advantage of that?

There isn't any advantage imho.

@melekes
melekes added this pull request to the merge queue Jul 10, 2024
Merged via the queue into main with commit 35894b2 Jul 10, 2024
@melekes
melekes deleted the dev/remove_tmp2p_alloc branch July 10, 2024 06:10
mergify Bot pushed a commit that referenced this pull request Jul 10, 2024
…3423)

Closes #2951

Remove p2p allocations for wrapping outbound packets. Every packet send
would require 3 new heap objects due to how we used pointers / did this
wrapping. This costs us: 13% of all system heap allocations, and 20% of
the CPU time of writing packets (under "normal" osmosis load. I don't
have big blocks benchmarks after the secret connection improvement got
in).

This should be a 25% speedup to `channel.writePacketMsgTo`, making
writing getting closer to the bound of just proto-marshalling a packet.

This solution is simple (just make a buffer in the struct) since these
packet sends are all single threaded.

---

#### PR checklist

- [X] Tests written/updated - N/A, existing tests cover safety here
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [X] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [X] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

---------

Co-authored-by: Anton Kaliaev <[email protected]>
(cherry picked from commit 35894b2)
melekes added a commit that referenced this pull request Jul 10, 2024
…(backport #3423) (#3465)

Closes #2951 

Remove p2p allocations for wrapping outbound packets. Every packet send
would require 3 new heap objects due to how we used pointers / did this
wrapping. This costs us: 13% of all system heap allocations, and 20% of
the CPU time of writing packets (under "normal" osmosis load. I don't
have big blocks benchmarks after the secret connection improvement got
in).

This should be a 25% speedup to `channel.writePacketMsgTo`, making
writing getting closer to the bound of just proto-marshalling a packet.

This solution is simple (just make a buffer in the struct) since these
packet sends are all single threaded.

---

#### PR checklist

- [X] Tests written/updated - N/A, existing tests cover safety here
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [X] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [X] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request #3423 done by
[Mergify](https://mergify.com).

Co-authored-by: Dev Ojha <[email protected]>
Co-authored-by: Anton Kaliaev <[email protected]>
mattac21 pushed a commit that referenced this pull request Sep 9, 2025
…3423)

Closes #2951

Remove p2p allocations for wrapping outbound packets. Every packet send
would require 3 new heap objects due to how we used pointers / did this
wrapping. This costs us: 13% of all system heap allocations, and 20% of
the CPU time of writing packets (under "normal" osmosis load. I don't
have big blocks benchmarks after the secret connection improvement got
in).

This should be a 25% speedup to `channel.writePacketMsgTo`, making
writing getting closer to the bound of just proto-marshalling a packet.

This solution is simple (just make a buffer in the struct) since these
packet sends are all single threaded.

---

- [X] Tests written/updated - N/A, existing tests cover safety here
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [X] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [X] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

---------

Co-authored-by: Anton Kaliaev <[email protected]>
mattac21 pushed a commit that referenced this pull request Sep 10, 2025
…3423)

Closes #2951

Remove p2p allocations for wrapping outbound packets. Every packet send
would require 3 new heap objects due to how we used pointers / did this
wrapping. This costs us: 13% of all system heap allocations, and 20% of
the CPU time of writing packets (under "normal" osmosis load. I don't
have big blocks benchmarks after the secret connection improvement got
in).

This should be a 25% speedup to `channel.writePacketMsgTo`, making
writing getting closer to the bound of just proto-marshalling a packet.

This solution is simple (just make a buffer in the struct) since these
packet sends are all single threaded.

---

- [X] Tests written/updated - N/A, existing tests cover safety here
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [X] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [X] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

---------

Co-authored-by: Anton Kaliaev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Speedup CPU time of channel.WritePacketMsgTo

3 participants