Replace unbounded index data array with fixed-size ring buffer#267
Replace unbounded index data array with fixed-size ring buffer#267
Conversation
Co-authored-by: albe <4259532+albe@users.noreply.github.com> Agent-Logs-Url: https://github.com/albe/node-event-storage/sessions/7e6a5798-f2db-4c7e-a376-9a78894d3bf5
|
@copilot extract all the ringbuffer related logic into a separate class RingBuffer.js and then only call "add()", "get()", etc. from the index. The goal is to have a very clean cache API, that could easily be replaced with a different implementation than a ringbuffer. |
Co-authored-by: albe <4259532+albe@users.noreply.github.com> Agent-Logs-Url: https://github.com/albe/node-event-storage/sessions/9fd21e21-3dde-4c24-90ae-e41f3620da75
Done in 3971ca9. All ring buffer mechanics now live in
|
Co-authored-by: albe <4259532+albe@users.noreply.github.com> Agent-Logs-Url: https://github.com/albe/node-event-storage/sessions/a143d98f-44b3-4cea-bcdc-2a7b9467adbd
The index kept an ever-growing sparse
Array(this.data) sized to the full index length, wasting memory for large indices — especially in write-only scenarios where most cached slots are never read back.Changes
src/Index/RingBuffer.js(new) — self-contained ring buffer class with a clean, implementation-agnostic cache API:add(),get(),set(),truncate(),reset(),slice(), andlength/capacity/windowStartproperties. The implementation can be swapped for a different strategy without touching any index code.src/Index/ReadableIndex.js— replaces inlinethis.cache[]/this._length/this.cacheSizefields with a singlethis.cache(RingBuffer) instance. NewcacheSizeoption (default1024) controls how many most-recent entries are held in memory. Entries outside the window fall back to disk reads on demand. The all-cached path inrange()usesthis.cache.slice()directly to avoid per-element allocation.src/Index/WritableIndex.js—add()andtruncate()delegate tothis.cache.add()/this.cache.truncate().src/Index/ReadOnlyIndex.js—onChange()delegates tothis.cache.truncate()for both grow and shrink cases.test/Index.spec.js— 5 new tests covering: memory bounding after cycling, disk fallback for out-of-window entries, cross-boundary range reads, stale-slot safety after truncate + re-add, and correctness after multiple full ring cycles.test/RingBuffer.spec.js(new) — 24 unit tests for theRingBufferclass directly, covering construction,add/get/set, window eviction,truncate(shrink and grow),reset, andslice(contiguous, wrapping, and single-element ranges).Usage
💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.