Suppose there are two threads, which communicate by asynchronously sending data
ID: 654255 • Letter: S
Question
Suppose there are two threads, which communicate by asynchronously sending data messages to each other. Each thread has some kind of message queue.
My question is very low level: What can be expected to be the most efficient way to manage the memory? I can think of several solutions:
Sender creates the object via new. Receiver calls delete.
Memory pooling (to transfer the memory back to the sender)
Garbage collection (e.g., Boehm GC)
(if the objects are small enough) copy by value to avoid heap allocation completely
1) is the most obvious solution, so I'll use it for a prototype. Chances are that it is already good enough. But independent of my specific problem, I wonder which technique is most promising if you are optimizing for performance.
I would expect pooling to be theoretically the best, especially because you can use extra knowledge about the flow of information between the threads. However, I fear that it is also the most difficult to get right. Lots of tuning... :-(
Garbage collection should be quite easy to add afterwards (after solution 1), and I would expect it to perform very well. So, I guess that it is the most practical solution if 1) turns out to be too inefficient.
If the objects are small and simple, copy by value might be the fastest. However, I fear that it forces unnecessary limitations on the implementation of the supported messages, so I want to avoid it.
Explanation / Answer
It is going to depend on how you implement the queues.
If you go with an array (round robin style) you need to set an upper bound on size for solution 4. If you go with a linked queue, you need allocated objects.
Then, resource pooling can be done easily when you just replace the new and delete with AllocMessage<T> and freeMessage<T>. My suggestion would be to limit the amount of potential sizes T can have and round up when allocating concrete messages.
Straight up garbage collection can work but that might cause long pauses when it needs to collect a large part, and will (I think) perform a bit worse than new/delete.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.