eve.rt.fiber.pool

Fiber Pool — Reusable Fiber Stacks for High-Concurrency Servers

This module provides FiberPool, a class that maintains a pool of reusable fibers. Creating and destroying fibers is expensive because each allocates a stack (typically 64 KB on POSIX). The pool amortises this cost by keeping finished fibers alive and re-dispatching them through a trampoline loop.

Lifecycle of a pooled fiber:

  1. spawn(work) creates a fiber whose trampoline reads

    slot.work, executes it, returns the slot to the free list, and then parks (yield without re-queue).

  2. The next spawn() call takes the slot from the free list,

    sets slot.work, and un-parks the fiber so the trampoline runs again.

Thread affinity: one pool per FiberScheduler, one scheduler per event loop, one event loop per thread.

Example:

auto loop  = EventLoop.create();
auto sched = new FiberScheduler(loop);
sched.attachToLoop();

auto pool = new FiberPool(sched, 16, 1024);
scope (exit) pool.dispose();

server.onAccept = (ref TcpListener s, Handle h) {
   pool.spawn(() @trusted {
      auto sock = FiberSocket.adopt(sched, *sched.eventLoop, h);
      handleRequest(sock);
      sock.dispose();
   });
};

loop.run();

class FiberPool

Types 1

A pool of reusable fibers.

Fibers are created on demand (lazy allocation) and reused via a per-slot trampoline. Each trampoline is an infinite loop that:

  1. Reads and clears its slot's work delegate.
  2. Executes the delegate (if any).
  3. Returns the slot to the free list.
  4. Parks and yields — waits for unpark() from the next

    spawn() call.

The pool integrates with a FiberScheduler for execution but manages fiber lifecycle independently.

Fields
Slot[] slots_
size_t[] freeSlots_
size_t maxSize_
size_t activeCount_
size_t totalCreated_
void delegate(Throwable) errorHandler_
Methods
void delegate() makeTrampoline(size_t idx) @trusted
void createSlot(size_t idx) @trusted
void onError(void delegate(Throwable) handler) @safe nothrowSet the error handler for unhandled exceptions in pooled fibers.
bool spawn(void delegate() work) @trustedSpawn a work item on a fiber from the pool.
size_t activeCount() @property const pure @safe nothrow @nogcNumber of fibers currently executing work.
size_t freeCount() @property const pure @safe nothrow @nogcNumber of fibers available in the free list.
size_t totalCount() @property const pure @safe nothrow @nogcTotal number of fibers created by this pool.
void dispose() @safe nothrowRelease all pooled fibers.
Constructors
this(FiberScheduler sched, size_t initialSize = 0, size_t maxSize = 0)Create a fiber pool.
Nested Templates
Slot