cmpstr
Version:
CmpStr is a lightweight, fast and well performing package for calculating string similarity
64 lines (63 loc) • 2.58 kB
TypeScript
/**
* Pool Utility
* src/utils/Pool.ts
*
* @see https://en.wikipedia.org/wiki/Circular_buffer
*
* The Pool class provides a simple and efficient buffer pool for dynamic programming
* algorithms that require temporary arrays (such as Levenshtein, LCS, etc.).
* By reusing pre-allocated typed arrays, it reduces memory allocations and garbage
* collection overhead, especially for repeated or batch computations.
*
* It supports different types of buffers (Uint16Array, number[], Set, Map) and allows
* for acquiring buffers of specific sizes while managing a maximum pool size.
*
* @module Utils/Pool
* @author Paul Köhler (komed3)
* @license MIT
*/
import type { PoolType } from './Types';
/**
* The Pool class provides a buffer pool for dynamic programming algorithms.
*
* It allows for efficient reuse of buffers (Uint16Array, number[], Set, Map)
* to reduce memory allocations and garbage collection overhead.
*/
export declare class Pool {
private static readonly CONFIG;
private static readonly POOLS;
/**
* Allocates a new buffer of the specified type and size.
*
* @param {PoolType} type - The type of buffer to allocate
* @param {number} size - The size of the buffer to allocate
* @return {any} - The newly allocated buffer
*/
private static allocate;
/**
* Acquires a buffer of the specified type and size from the pool.
* If no suitable buffer is available, it allocates a new one.
*
* @param {PoolType} type - The type of buffer to acquire (e.g., 'uint16', 'number[]', 'set', 'map')
* @param {number} size - The size of the buffer to acquire
* @return {T} - The acquired buffer of the specified type
*/
static acquire<T = any>(type: PoolType, size: number): T;
/**
* Acquires multiple buffers of the specified type and sizes from the pool.
*
* @param {PoolType} type - The type of buffers to acquire
* @param {number[]} sizes - An array of sizes for each buffer to acquire
* @return {T[]} - An array of acquired buffers of the specified type
*/
static acquireMany<T = any>(type: PoolType, sizes: number[]): T[];
/**
* Releases a buffer back to the pool.
* If the size of the buffer is larger than the maximum item size, it will not be released.
*
* @param {PoolType} type - The type of buffer to release
* @param {T} buffer - The buffer to release
* @param {number} size - The size of the buffer
*/
static release<T = any>(type: PoolType, buffer: T, size: number): void;
}