Avoid a `tokio::mpsc::Sender` clone for each P2P send operation
authorMatt Corallo <git@bluematt.me>
Sat, 4 Nov 2023 21:21:58 +0000 (21:21 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 9 Nov 2023 22:28:08 +0000 (22:28 +0000)
commit18dc7f248bc5c323477920f3168e606151d867e2
treed628eaaf85da74854a3f4b8a7ca95d054e648742
parent969085bf1e77ebe5b4e7cb0523311e9905fa20f3
Avoid a `tokio::mpsc::Sender` clone for each P2P send operation

Whenever we go to send bytes to a peer, we need to construct a
waker for tokio to call back into if we need to finish sending
later. That waker needs some reference to the peer's read task to
wake it up, hidden behind a single `*const ()`. To do this, we'd
previously simply stored a `Box<tokio::mpsc::Sender>` in that
pointer, which requires a `clone` for each waker construction. This
leads to substantial malloc traffic.

Instead, here, we replace this box with an `Arc`, leaving a single
`tokio::mpsc::Sender` floating around and simply change the
refcounts whenever we construct a new waker, which we can do
without allocations.
lightning-net-tokio/src/lib.rs