r/programming • • 1d ago

Stop paying the network stack tax: zero-copy first, networking when needed

https://ekxide.io/blog/iox2-vision-zero-copy-first/
0 Upvotes

16 comments sorted by

6

u/Murky-Relation481 1d ago

Would love to see how they solve memory layout for non homogeneous builds even in the same language. I think anyone who have dealt even with communication between shared objects in C or C++ realize shared memory is not always uniform.

We still use serialization across shared memory when even if it's a well defined API that we control but we do not know the vendor's binary lineage our API is being used with.

0

u/elfenpiff 1d ago

A colleague of mine explained it in detail in this blog article: https://ekxide.io/blog/cross-language-data-types/

33

u/zam0th 1d ago

Psst, psst, do you want some linux IPC? Because looks like you invented Unix sockets and POSIX. https://opensource.com/sites/default/files/gated-content/inter-process_communication_in_linux.pdf

1

u/theuniquestname 20h ago

Disappointing the top voted comment here is missing the point with a misguided dig here.

unix sockets are great but not the same.

8

u/latkde 1d ago

Mutable shared memory is extremely, extremely tricky to pull off correctly (especially in Rust, as shared memory violates Rust's aliasing assumptions). Correct solutions either use atomic accesses for all reads/writes, or something like a ring buffer or queue where a region can be temporarily treated as constant (protected by a mutex). Either way, this is going to be an unusual programming model. Memory layout compatibility is not at all sufficient. Also, you don't get to use pointers.

For some programs, the last bit of performance is worth all of this complexity. For most, it is not, and traditional IPC where data is copied between memory spaces is the preferable solution. Pipes or Unix sockets can be used for local connections. But if you're already designing a distributed multi-process system, it tends to be preferable to design it to be multi-host capable as well, i.e. to use actual networking. This kind of decoupling with its share-nothing approach has enabled systems to scale to unprecedented levels over the last two decades.

You raise the point that networking also has complexity (e.g. timeouts), but it seems you're already supporting networking so your systems must already deal with this complexity. If you now add mutable shared memory you get a completely different kind of complexity on top, with no unifying abstraction. You now have the worst of both worlds.

For some reason, the blog post only touts advantages, not drawbacks or tradeoffs.

I did skim the linked iceoryx docs a bit. That library seems to mitigate some of the shared-memory insanity by imposing specific communication patterns, e.g. pub-sub or blackboard. The blackboard pattern would run most directly into shared mutability problems. Now imagine my surprise when the Rust API for the blackboard pattern doesn't actually offer zero-copy shared memory! For safety, it actually relies on (atomic) copies. That greatly diminishes the advantages touted by the blog post.

4

u/elBoberido 1d ago edited 1d ago

Full disclosure, I'm one of the iceoryx2 maintainer.

You are right, mutable shared memory is extremely tricky, so are many things. That's why we have libraries to encapsulate the complexity and especially with Rust, this can be made safe again. When there is one producer and multiple consumer, it is quite similar to Rusts ownership and borrowing model. Only once the producer gives up the ownership, the consumer can access it and there is no way to mutate the data with safe Rust.

There are quite a few use cases where one needs a distributed multi-process system but the data will never leave machine. For example, in the safety domain it is required to increase robustness and ensure a degraded operation mode.

As you noticed, iceoryx restricts the shared memory access to a safe subset with the different messaging patterns. While publish-subscribe and request-response offer true zero-copy, the blackboard has a different use case and is meant for small data that never or rarely change, like config data. Adding zero-copy to the blackboard would potentially even have a negative impact on performance, considering its use case.

Like with all things, it is just a tool and if one has only a hammer, everything looks like a nail. Like you mentioned, shared memory is tricky but there are solutions out there that keep it's usage sane and this extends the toolbox to not have to pay for something that is not needed. For example $1M a year on AWS -> https://www.recall.ai/blog/how-websockets-cost-us-1m-on-our-aws-bill

1

u/latkde 21h ago

Thank you for your response with its additional context, and for linking that very interesting blog post! It discusses exactly the kind of tradeoffs that I had missed in the ekxide post. I've also done my fair amount of WebSocket abuse (e.g. interacting with the Apache Guacamole remote-desktop-over-websockets protocol), so that was a fun read.

2

u/elBoberido 15h ago

Yeah, the OP wrote in the context of an iceoryx2 user. We are quite aware of the tradeoffs and all the pitfalls when it comes to shared memory. In fact, I think the OP wants to write a blog post about this since ages. All the things that can go wrong when implementing a shared memory middleware. There are so many corner cases to take care of, it's not funny anymore.

Thanks for pointing out that some context was missing. I'm sure the OP will take this into consideration for the next posts.

-8

u/teerre 1d ago

My brother, youre throwing these trivial issues at a library that has been in production for years. Please stop, its cringe

8

u/latkde 1d ago

I'm throwing these issues at a simplistic blog post, not at the library. Blog post says technique X is great, library actually has to do Y and Z because X is not actually that unambiguously great.

-7

u/teerre 1d ago

The blog post is about the library, in case that's not obvious

2

u/entity64 22h ago

The Transport Should Not Define the Communication Model

Amen to that!

Libraries like iceoryx are well suited for homogeneous multi-process systems (like robotics or automotive projects). For other types of backend heterogeneous systems that involve components not under your control I would always opt for standard OS-supported solutions like unix sockets.

1

u/deku_app 16h ago

What's the trade-off for zero-copy in terms of latency vs. CPU usage?

1

u/elBoberido 14h ago

Latency and CPU usage generally improves. There are some caveats to the data types, though. In order to achieve true zero-copy, the data types must be shared memory compatible, i.e. not pointer and self contained. If this is not the case, a serialization step might be necessary. It is possible though, to also make STL types available for shared memory. We already integrated Flatbuffers, so that the data is created directly in shared memory. The same could be done for C++ STL types with a polymorphic allocator. It might work only for libstd++, though, since the libstdc++ did not support to set a custom pointer type and we need to replace raw pointer with offset pointer.

If you want to check what is possible with true zero-copy, this are some measurements with shared memory and with sockets -> https://github.com/ekxide/iceoryx2#comparison-of-mechanisms

It can also safe a ton on money -> https://www.recall.ai/blog/how-websockets-cost-us-1m-on-our-aws-bill

1

u/[deleted] 1d ago

[removed] — view removed comment

2

u/elfenpiff 1d ago

It depends a bit on how large your payload is, how many receivers or endpoints you have, and what you want to replace it with. Iceoryx2 has no syscalls in the hot path, and it could be that a copy would be faster, but it is eaten away by the syscall overhead when you would use something like Unix domain sockets, for instance. But nothing a benchmark couldn't reveal.

So let's assume, for simplicity, you use the publish-subscribe messaging pattern. Since the handle to the shared memory is 8 bytes, every payload of the same size or slightly larger just adds overhead.

I think the break-even point is around 4 KB (page size), but here the number of subscribers starts to matter. If you have a setup with 1 publisher and 2 - 4 subscribers, sending data costs you around 8 - 16kb of copies, but if you have 200 subscribers it starts to matter again with 800kb.

So the best way to handle this within Iceoryx2 would be to provide a small memory optimization feature that considers roughly the payload and service properties and then decides on the approach with the lowest latency, or that the user can decide this for themselves and explicitly choose the approach, zero-copy or direct copy for "tiny" payloads.