Three ways to batch messages
Linked list of individual messages
- Requires at least one pointer per message => not suitable for very small messages (memory overhead)
- Very flexible, messages can be linked into and be moved between arbitrary existing lists without additional memory management (use intrusive linking).
- No restrictions on size of queue (fully dynamic)
Linked list of chunks of messages
- Compared to above approach, memory overhead is reduced by putting messages in chunks of messages (fixed number - 16, 32 or more messages?)
- Processing speed might also benefit
- Memory management is much more complicated. Chunks need to be managed separately from actual messages.
- Finding a suitable chunk capacity is not trivial. If a chunk is not full but the messages that are already in it should be shipped, then the remaining memory goes unused. This can potentially stall the producer with very few messages actually being in transit.
- Simpler memory management. Only one "global" allocation to keep track of.
- Queues are more static - normally, the size is set at initialization and never changed.
- Code to handle the wraparound is surprisingly fiddly.
- Suitable as a multithreaded data structure. Lockless implementation possible.