Sunny Ahuwanya's Blog

Mostly notes on .NET and C#

Multi-Slab Buffers

A noteworthy feature of the bufferpool project is the ability for an individual buffer to span multiple slabs.

Previously, a buffer was confined to one slab, meaning that the maximum buffer size was the slab size.
It was impossible to send or receive data that was larger than the slab.
This commit changed that. Individual buffers can now span multiple slabs, which allows for what is known as Scatter/Gather or Vectored I/O operations.

Buffer spanning multiple slabs

This means buffers of any arbitrary length can be requested from the pool and all needed slabs will be created and added to the pool, to form the buffer.
Conversely, the slabs are freed and removed from the pool, when the buffer is disposed.

This feature is particularly useful for transmitting or receiving large amount of data. It eliminates the need to allocate a large contiguous memory block to hold the data.
Instead, smaller slabs, which are allocated at different convenient locations in memory, are chained to form the buffer.
This significantly lessens the chance of running out of memory, due to heap fragmentation, during socket operations involving a large amount of data.