microavia / messgen

MIT License
9 stars 11 forks source link

Introduce payload type to support zero copy serialization in C++ #94

Closed lachem closed 3 months ago

lachem commented 3 months ago

This is a proposal to implement an additional type "payload" (better name ideas are welcome) that would allow to perform zero-copy optimizations in C++. For non C++ generators "payload" is identical to "bytes".

Problem: currently payloads for C++ are generated as std::vector. An std::vector is not great for trailing payloads, because:

Serializing/Deserialising a requires a copy of its contents
Default alignment on our target platform is 8 bytes
The size_type on our target platform is 8 bytes

For low-latency applications all of the above are an issue.

Solution 1: inplace_payload as implemented. The pros are that it offers similar API to std::vector and hence share some of cpp generator logic with bytes. The cons is that using inplace_payload in C++ requires careful manual memory management, which is error prone. Optionally we could make it less error prone, by introducing a _capacity member that would have to be set manually before assigning any data, but that has the downside of using another 4 bytes on our platform.

Solution 2: Use pmr::vector for bytes. The pros are that passing a custom polymorphic allocator is explicit and no changes to messgen except for the stl vector type would need to be made. The cons are that this does not change the alignment or the memory footprint of the vector size member.