/**
 * Larger bucket sizes provide better cache locality
 * Larger bucket sizes also cause more hanging GC references, as if one matrix element is still held - ENTIRE bucket is still referenced
 * @type {number}
 */
const ALLOCATOR_BUCKET_CAPACITY = 42;

/**
 *
 * @type {ArrayBuffer|null}
 */
let bucket = null;

/**
 *
 * @type {number}
 */
let bucket_cursor = ALLOCATOR_BUCKET_CAPACITY;

const ELEMENT_BYTE_SIZE = 4 * 3;

/**
 * custom Float32Array allocator, allocated memory in continuous chunks
 * @returns {Float32Array}
 */
export function v3_allocate() {
    if (bucket_cursor >= ALLOCATOR_BUCKET_CAPACITY) {
        bucket = new ArrayBuffer(ALLOCATOR_BUCKET_CAPACITY * ELEMENT_BYTE_SIZE);
        bucket_cursor = 0;
    }

    const result = new Float32Array(bucket, bucket_cursor * ELEMENT_BYTE_SIZE, 3);

    bucket_cursor++;

    return result;
}
