nicholassm / disruptor-rs

Low latency inter-thread communication library in Rust inspired by the LMAX Disruptor.
MIT License
618 stars 18 forks source link

Does it make sense to add heap stored field in the Event? #11

Closed ghost closed 3 months ago

ghost commented 3 months ago

Hi,

Was wondering if we would not lose the cache-friendly property of the Disruptor by using a heap stored field in the Event placed on the ring buffer, like a Vec for example? Or it has no incidence whatsoever?

Edit:

Actually I think I got my confirmation here:

https://blog.scottlogic.com/2021/12/01/disruptor.html

That being said, we do need to be mindful that nested values on those objects are kept as primitive values to avoid indirectly allocating to the heap.

nicholassm commented 3 months ago

Hi Aasimovv,

You're right in the sense that it depends on the memory allocator. Does it put all the heap allocated objects next to each other in memory and in particular the slots allocated in the Ring Buffer? If yes, then the CPU is likely to pre-fetch the data into the caches as the different thread traverse memory. But that is typically not happening and it's definitely not guaranteed so you could have different latency in different runs of the same code.

I think the biggest latency impact will be if you allocate in e.g. the Vec during the critical path in your code. That could really impact the latency. It's better in that case to pre-allocate everything at startup.

Kind regards, Nicholas