tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.78k stars 489 forks source link

feat: Make allocations when decoding fallible #974

Open ParkMyCar opened 7 months ago

ParkMyCar commented 7 months ago

This PR makes any allocations that occur in the decode path fallible, without any public API changes.

The changes are specifically scoped to repeated fields, Vec<u8>, and String.

In addition to making allocations fallible, I also changed the merge impl of Vec<u8> to use bytes::merge_one_copy like String does, this should result in strictly less allocations.

Why make this change?

Previously users had no way to guard against OOMs in decoding. You could try to make a guess based on the size of the encoded message, but this fairly inaccurate because it would be very difficult (impossible?) to account for things like the ammortized growth of a Vec. Even if you did guess based on the size of the encoded message, there is no way you could account for multiple messages being decoded in parallel in individual tasks, e.g. handling multiple network requests in parallel.

What about the encoding path?

A similar change is not needed for the encoding path because you can already guard against OOMs by using the encoded_len() to allocate a buffer yourself guarding against OOMs, and then encode into this newly allocated buffer.