问题
I am using Boost.Asio, I want to improve my system by using Zero-copy sendmsg/Receive. Can I use Zero-copy sendmsg/Receive in Boost.Asio? Could you give me how to use them if I can use them?
回答1:
Short answer, you can only if your in-memory representation is identical to the on-the-wire one.
See the answers here, which do an excellent job of describing how e.g. Cap'n Proto achieves this:
- Do cereal and Boost Serialization use zero-copy?
Also compare:
- TCP Zero copy using boost
And on creating memory-mappable representations of advanced C++ data structures so that they can be mapped on-the-wire:
https://www.boost.org/doc/libs/1_72_0/doc/html/interprocess/managed_memory_segments.html#interprocess.managed_memory_segments.making_ipc_easy.managed_memory_segments_intro, specifically about
basic_managed_heap_memory
andbasic_managed_external_buffer
I have a gazillion examples on this site which show how to use such managed memory segments, but usually focused on
managed_mapped_file
andmanaged_shared_memory
which have the exact same feature set, so you can use these with the previous managed heap segments in the same way: https://stackoverflow.com/search?q=user%3A85371+interprocess+allocator
来源:https://stackoverflow.com/questions/60254180/how-to-use-zero-copy-sendmsg-receive-in-boost-asio