UNPKG

cmpstr

Version:

CmpStr is a lightweight, fast and well performing package for calculating string similarity

69 lines (68 loc) 2.81 kB
/** * 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 (Int32Array, number[], string[], Set, Map) * and allows for acquiring buffers of specific sizes while managing a max pool size. * * @module Utils * @name 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 (Int32Array, number[], Set, Map) * to reduce memory allocations and garbage collection overhead. */ export declare class Pool { /** Pool Types */ private static readonly CONFIG; /** Pool Rings for each type */ 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., 'int32', 'number[]', 'map') * @param {number} size - The size of the buffer to acquire * @return {T} - The acquired buffer of the specified type * @throws {CmpStrUsageError} - Throws an error if the pool type is unsupported */ 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 * @throws {CmpStrUsageError} - Throws an error if the pool type is unsupported */ static release<T = any>(type: PoolType, buffer: T, size: number): void; }