diff --git a/src/arena.test.ts b/src/arena.test.ts index a33ee57..7639718 100644 --- a/src/arena.test.ts +++ b/src/arena.test.ts @@ -1,4 +1,4 @@ -import { describe, test, expect } from 'vitest' +import { describe, test, expect, afterEach, vi } from 'vitest' import { readFileSync } from 'fs' import { CSSDataArena, @@ -401,6 +401,7 @@ describe('CSSDataArena', () => { expect(arena.get_capacity()).toBe(100) arena.trim() expect(arena.get_capacity()).toBe(arena.get_count()) + expect(((arena as any).buffer as ArrayBuffer).byteLength).toBe(arena.get_count() * 32) }) test('should preserve all node data after trim', () => { @@ -448,3 +449,102 @@ describe('CSSDataArena', () => { }) }) }) + +describe('CSSDataArena buffer growth', () => { + afterEach(() => { + vi.restoreAllMocks() + }) + + function fill_and_check(arena: CSSDataArena, n: number) { + for (let i = 0; i < n; i++) { + const node = arena.create_node(DECLARATION, i, i % 100, i + 1, 7) + arena.set_flag(node, FLAG_IMPORTANT) + } + expect(arena.get_count()).toBe(n + 1) + for (let i = 0; i < n; i++) { + expect(arena.get_start_offset(i + 1)).toBe(i) + expect(arena.get_length(i + 1)).toBe(i % 100) + expect(arena.get_start_line(i + 1)).toBe(i + 1) + expect(arena.has_flag(i + 1, FLAG_IMPORTANT)).toBe(true) + } + } + + test('grows correctly with native resize support', () => { + const arena = new CSSDataArena(2) + fill_and_check(arena, 500) + expect(arena.get_growth_count()).toBeGreaterThan(0) + }) + + test('grows in place without replacing the buffer when resizable', () => { + if (typeof ArrayBuffer.prototype.resize !== 'function') return + const arena = new CSSDataArena(64) + const before = (arena as any).buffer + const view = (arena as any).view + fill_and_check(arena, 100) + expect((arena as any).buffer).toBe(before) + expect((arena as any).view).toBe(view) + }) + + test('works when ArrayBuffer.prototype.resize is unavailable', () => { + const resize = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'resize') + Object.defineProperty(ArrayBuffer.prototype, 'resize', { value: undefined, configurable: true }) + try { + const arena = new CSSDataArena(2) + expect((arena as any).buffer.resizable).toBe(false) + fill_and_check(arena, 500) + } finally { + Object.defineProperty(ArrayBuffer.prototype, 'resize', resize!) + } + }) + + test('falls back when resizable allocation throws', () => { + const Original = globalThis.ArrayBuffer + let calls = 0 + class Throwing extends Original { + constructor(length: number, options?: { maxByteLength?: number }) { + if (options && calls++ >= 0) throw new RangeError('no address space') + super(length) + } + } + vi.stubGlobal('ArrayBuffer', Throwing) + try { + const arena = new CSSDataArena(2) + fill_and_check(arena, 200) + expect(calls).toBeGreaterThan(0) + } finally { + vi.unstubAllGlobals() + } + }) + + test('falls back to copying when resize() throws', () => { + if (typeof ArrayBuffer.prototype.resize !== 'function') return + const arena = new CSSDataArena(2) + const spy = vi.spyOn(ArrayBuffer.prototype, 'resize').mockImplementation(() => { + throw new RangeError('out of memory') + }) + fill_and_check(arena, 200) + expect(spy).toHaveBeenCalled() + }) + + test('falls back to copying when growing past the reserved size', () => { + if (typeof ArrayBuffer.prototype.resize !== 'function') return + const arena = new CSSDataArena(2) + const max = ((arena as any).buffer as ArrayBuffer).maxByteLength + expect(max).toBe(2 * 32 * 4) // reservation scales with initial capacity + const n = max / 32 + 1000 + for (let i = 0; i < n; i++) arena.create_node(DECLARATION, i, 1, 1, 1) + expect(arena.get_start_offset(1)).toBe(0) + expect(arena.get_start_offset(n)).toBe(n - 1) + expect(((arena as any).buffer as ArrayBuffer).byteLength).toBeGreaterThan(max) + }) + + test('trim then grow still works', () => { + const arena = new CSSDataArena(100) + for (let i = 0; i < 10; i++) arena.create_node(DECLARATION, i, 1, 1, 1) + arena.trim() + expect(arena.get_capacity()).toBe(arena.get_count()) + arena.create_node(DECLARATION, 99, 1, 1, 1) + expect(arena.get_start_offset(arena.get_count() - 1)).toBe(99) + expect(arena.get_start_offset(5)).toBe(4) + }) +}) diff --git a/src/arena.ts b/src/arena.ts index 99c400a..5aa269d 100644 --- a/src/arena.ts +++ b/src/arena.ts @@ -137,16 +137,36 @@ export class CSSDataArena { // Combined with the constant above: effective ceiling = 210 × 1.1 = 231 nodes/KB. private static readonly CAPACITY_BUFFER = 1.1 + // A resizable buffer reserves address space up front (not physical memory). The initial capacity + // is already estimated from the source size, so reserve a multiple of it. Growing past the + // reservation falls back to copying into a new buffer. + private static readonly MAX_GROWTH_RATIO = 4 + constructor(initial_capacity: number = 1024) { this.capacity = initial_capacity // Start count at 1 since 0 is reserved for "no node" this.count = 1 this.growth_count = 0 - this.buffer = new ArrayBuffer(initial_capacity * BYTES_PER_NODE) + this.buffer = CSSDataArena.create_buffer(initial_capacity * BYTES_PER_NODE) this.view = new DataView(this.buffer) this.overflow_lengths = new Map() } + // Allocate a resizable buffer (so growing needs no copy) when ArrayBuffer.prototype.resize is + // supported; otherwise, or if the allocation throws, fall back to a plain fixed-size buffer. + private static create_buffer(byte_length: number): ArrayBuffer { + if (typeof ArrayBuffer.prototype.resize === 'function') { + try { + return new ArrayBuffer(byte_length, { + maxByteLength: byte_length * CSSDataArena.MAX_GROWTH_RATIO, + }) + } catch { + // Unsupported options or unable to reserve address space: use a plain buffer + } + } + return new ArrayBuffer(byte_length) + } + // Calculate recommended initial capacity based on CSS source size static capacity_for_source( source_length: number, @@ -329,16 +349,27 @@ export class CSSDataArena { // --- Node Creation --- - // Grow the arena by 1.3x when capacity is exceeded + // Grow the arena by 1.3x when capacity is exceeded. + // Resizes in place when the buffer is resizable; otherwise (or if that fails) copies. private grow(): void { this.growth_count++ const new_capacity = Math.ceil(this.capacity * CSSDataArena.GROWTH_FACTOR) - const new_buffer = new ArrayBuffer(new_capacity * BYTES_PER_NODE) - - // Copy existing data to new buffer - new Uint8Array(new_buffer).set(new Uint8Array(this.buffer)) + const new_byte_length = new_capacity * BYTES_PER_NODE + const buffer = this.buffer + + if (buffer.resizable && new_byte_length <= buffer.maxByteLength) { + try { + // The length-tracking DataView created with the buffer follows the resize + buffer.resize(new_byte_length) + this.capacity = new_capacity + return + } catch { + // Resize failed (buffer is unchanged): fall back to copying + } + } - // Update arena state + const new_buffer = new ArrayBuffer(new_byte_length) + new Uint8Array(new_buffer).set(new Uint8Array(buffer)) this.buffer = new_buffer this.view = new DataView(new_buffer) this.capacity = new_capacity @@ -391,10 +422,19 @@ export class CSSDataArena { } } - // Shrink the buffer to the live node count, releasing wasted capacity. Call once after parsing; no-op if already tight. + // Shrink the buffer to the live node count (in place when resizable), releasing wasted capacity. Call once after parsing; no-op if already tight. trim(): void { if (this.count === this.capacity) return const byte_count = this.count * BYTES_PER_NODE + if (this.buffer.resizable) { + try { + this.buffer.resize(byte_count) + this.capacity = this.count + return + } catch { + // Fall back to copying + } + } const new_buffer = new ArrayBuffer(byte_count) new Uint8Array(new_buffer).set(new Uint8Array(this.buffer, 0, byte_count)) this.buffer = new_buffer