-
Notifications
You must be signed in to change notification settings - Fork 976
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
Reducing allocations for transfer-heavy protocols by using borrowed data in protobuf files #4781
Comments
I started with this an it yields a lot of compile errors (understandably). Those will all need to be fixed. Perhaps in a first iteration by just using The lifetime would have to be To actually make use of borrowed data in gossipsub for example, we need to defer encoding to the protobuf format for as long as possible. Essentially, we need to carry some data type that uses
@joshuef Are you interested in working on this? |
To achieve this, we'd need to do something similar as @mxinden described here. I.e. we need to extend |
Let me check out the scope of this and see how deep things go. If I have time to get in amongst it in the next week I'll definitely do that, or may be able to get someone from the team to have a look if no. I'll report back here when it's a bit clearer for me. 👍 |
I did start looking at this, but won't have time to dig in until next week sometime, I think. |
No worries, it is an internal change so we can ship it at any point, doesn't need to go into the current release :) |
So I've been digging about further on this this morning, but I am blocked by the
I'm not really sure if there's a way around this? Suggestions very welcome (My WIP branch: https://github.com/joshuef/rust-libp2p/tree/GossipBytesPbCow) |
If the data is borrowed, we don't need to Meaning, we can just match on the |
Okay, I'll have a look at implementing in that fashion then, thanks @thomaseizinger ! 🙇 |
We can reduce the number of allocations during the encoding of messages by not depending on the `Encoder` implementation of `unsigned-varint` but doing the encoding ourselves. This allows us to directly plug the `BytesMut` into the `Writer` and directly encode into the target buffer. Previously, we were allocating each message as an additional buffer that got immediately thrown-away again. Related: #4781. Pull-Request: #4782.
Description
This is inspired by PRs from @joshuef (#4751, #4753, #4754).
The idea is to use
Bytes
to avoid potentially costly clones of large chunks of memory as we are processing the messages. For gossipsub, a single message might be sent to multiple clients. For kademlia, we might serve the same record multiple times from the record store. In both cases, there shouldn't be a need for any additional allocations.The main problem though is that the generated protobuf structs use
Vec<u8>
and thus force an allocation onto us every time we want to send one of these. https://github.com/tafia/quick-protobuf does have an option to generateCow
instead ofVec<u8>
and thus allow encoding of borrowed data. In the past, we weren't able to use this becauseasynchronous-codec
couldn't encode borrowed data. But it can now! See mxinden/asynchronous-codec#9.As a result, I think it is worth experimenting whether or not we can now use the generated protobuf structs.
To actually make use of this, we need to:
pb-rs
with the correct options to useCow
instead ofVec
:quick-protobuf-codec
to work with the new protobuf filesMotivation
Less memory allocations.
Current Implementation
We allocate each message again just to serialize it to the wire.
Are you planning to do it yourself in a pull request ?
No
The text was updated successfully, but these errors were encountered: