Skip to content

feat: Add BlockAllocator for 1D block memory management#8481

Merged
mvaligursky merged 2 commits intomainfrom
mv-block-allocator
Feb 26, 2026
Merged

feat: Add BlockAllocator for 1D block memory management#8481
mvaligursky merged 2 commits intomainfrom
mv-block-allocator

Conversation

@mvaligursky
Copy link
Contributor

@mvaligursky mvaligursky commented Feb 26, 2026

Summary

  • Adds a general-purpose BlockAllocator class (src/core/block-allocator.js) for managing a 1D linear address space with contiguous block allocations
  • Backed by a doubly-linked list with free-list threading for O(f) first-fit allocation and O(1) free with automatic neighbor merging
  • Supports grow-only expansion, incremental defragmentation (hybrid relocate-last + slide-left), full single-pass compaction, and a batch updateAllocation API
  • MemBlock handles are pooled to minimize GC pressure
  • Includes O(1) fragmentation metric
  • Comprehensive unit tests (35 tests) covering basic ops, merging, defrag, pool reuse, and stress tests with Uint32Array buffer integrity validation

General-purpose block allocator backed by a doubly-linked list with
free-list threading. Supports first-fit allocation, O(1) free with
automatic merging, grow-only expansion, incremental and full
defragmentation, batch updateAllocation, and MemBlock object pooling.

Includes comprehensive unit tests covering basic operations, merging,
defrag, fragmentation metric, pool reuse, and stress tests with
Uint32Array buffer integrity validation.

Made-with: Cursor
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new internal 1D memory/block allocator to src/core and introduces a dedicated unit test suite validating allocation/free/merge behavior, growth, and defragmentation flows.

Changes:

  • Introduces BlockAllocator / MemBlock for contiguous block allocation over a linear address space (with free-list + linked-list bookkeeping).
  • Implements grow-only capacity expansion, incremental defrag, full compaction, and a batch updateAllocation API.
  • Adds a comprehensive Mocha/Chai test suite covering core behaviors and stress scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/core/block-allocator.js New allocator implementation with allocation/free, growth, defrag (incremental/full), and batch update API.
test/core/block-allocator.test.mjs New test suite validating allocator invariants, merging, defrag behavior, pooling, and stress runs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Fix buffer resize bug in updateAllocation stress test (const -> let,
  assign resized buffer back)
- Replace Math.random() with deterministic seeded PRNG (mulberry32) in
  all stress tests for reproducible results
- Fix misleading comment in _defragIncremental phase 2

Made-with: Cursor
@mvaligursky mvaligursky merged commit 388cc45 into main Feb 26, 2026
8 checks passed
@mvaligursky mvaligursky deleted the mv-block-allocator branch February 26, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Request for a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants