class

IO::BufferPool

Inherits Reference < Object

A pool of reusable byte buffers for IO::Buffered.

Most connections are idle most of the time. By returning read/write buffers to a pool when they're not in use, memory can be reused across connections instead of each connection holding two buffers forever.

Each thread has its own cache of buffers (a LIFO stack, so the most recently used buffer is reused first), so acquiring and releasing takes no locks. A buffer may be released on another thread than it was acquired on, it then goes into that thread's cache. Each cache keeps at most CACHE_BYTES_PER_THREAD worth of buffers, more are left to the GC.

Constants

CACHE_BYTES_PER_THREAD = (4 * 1024) * 1024

Idle buffers kept per thread and pool: 256 buffers of the default 16 KiB. With hundreds of busy connections, about 350 buffers were in use at once, and 64 buffers made the pool drop and reallocate some of them.

Constructors

new(buffer_size : Int32)
Source

Class methods

for(buffer_size : Int32) : IO::BufferPool

Returns the pool for the given buffer size. Pools for other sizes (from before a config reload) keep serving the connections that use them, their caches are bounded by CACHE_BYTES_PER_THREAD.

Source

Instance methods

acquire

Acquire a buffer from the current thread's cache, or allocate a new one

Source
buffer_size
Source
max_cached

Max number of buffers cached per thread

Source
release(buffer : Pointer(UInt8)) : Nil

Return a buffer to the current thread's cache. If the cache is full, the buffer is left for the GC.

Source
stats
Source