vfhe.util.memory_pool

Controls the library’s memory pool: its capacity, wiping, and accounting.

vfhe keeps the large buffers it releases, per thread, and hands them out again for the next request of the same size, instead of returning them to the C library and faulting fresh memory in on the next allocation. What it keeps over all threads is bounded by a capacity:

  • "auto" (the default): at most the most the library ever had in use at once, so a repeating workload is served from the pool and, once its buffers are released, the pool holds no more than the library’s own peak.

  • a number of bytes.

  • 0: keep nothing; every buffer goes back to the C library.

VFHE_MEMPOOL_CAPACITY (bytes) sets the default.

A released buffer keeps whatever it held until it is overwritten. With set_wipe_on_release every buffer is zeroed as it is released, at the cost of a pass over it; off by default (VFHE_MEMPOOL_WIPE_ON_RELEASE=1 turns it on).

>>> from vfhe.util import memory_pool
>>> memory_pool.set_capacity(1 << 30)
>>> memory_pool.statistics().retained_bytes <= 1 << 30
True
class vfhe.util.memory_pool.MemoryPoolStatistics(retained_bytes: int, outstanding_bytes: int, peak_outstanding_bytes: int, retention_limit_bytes: int, hits: int, misses: int)

Holds a snapshot of the pool, over all threads.

retained_bytes: int

Kept by the pool, ready to be handed out.

outstanding_bytes: int

Handed out and not yet released to the pool.

peak_outstanding_bytes: int

The most ever outstanding at once; the "auto" capacity. Exact while one thread uses the pool; with several it may miss a short-lived peak by up to a few MiB per thread, as each thread shares its count in steps.

retention_limit_bytes: int

What the capacity allows the pool to keep right now.

hits: int

Requests served from the pool.

misses: int

Requests passed to the C library.

vfhe.util.memory_pool.capacity() → int | Literal['auto']

Returns the capacity in force (see set_capacity).

vfhe.util.memory_pool.release_all() → None

Returns every buffer the pool keeps, on every thread, to the C library.

This costs speed afterwards: the next requests for those sizes are served by the C library again, and a large buffer it hands out gets fresh pages, faulted in on first use, which costs about as much as one pass over the buffer. Keeping the buffers to avoid that is what the pool is for. Call it after a phase whose buffer sizes will not come back (a large one-off computation, say) when the memory matters more than the speed of what follows; the C library may keep part of the memory for the process.

vfhe.util.memory_pool.set_capacity(n_bytes: int | Literal['auto'] | None = None) → None

Bounds what the pool keeps, over all threads, to n_bytes.

"auto" follows the library’s peak use and 0 turns the pool off; None restores the default (VFHE_MEMPOOL_CAPACITY if set, else "auto"). Lowering the capacity below what the pool keeps releases it all. A capacity below what a workload reuses costs speed on every request the pool no longer serves (see release_all).

vfhe.util.memory_pool.set_wipe_on_release(enabled: bool | None = None) → None

Zeroes every buffer as it is released to the pool, or stops doing so.

None restores the default: off, unless VFHE_MEMPOOL_WIPE_ON_RELEASE=1.

vfhe.util.memory_pool.statistics() → MemoryPoolStatistics

Returns the pool’s current accounting (see MemoryPoolStatistics).

vfhe.util.memory_pool.wipe_on_release() → bool

Returns whether released buffers are zeroed (see set_wipe_on_release).