-
Notifications
You must be signed in to change notification settings - Fork 4
Replace unbounded index data array with fixed-size ring buffer #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/reimplement-issue-55-on-latest-main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
38de112
Initial plan
Copilot 6810a12
Implement ring buffer for index memory management (fixes #73)
Copilot 3971ca9
Extract ring buffer logic into RingBuffer.js class with clean cache API
Copilot c2a083c
Address review comments: rename variables and add RingBuffer.slice()
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /** | ||
| * A fixed-capacity ring buffer used as an index entry cache. | ||
| * | ||
| * Only the most-recent `capacity` entries are kept in memory. The buffer also | ||
| * tracks the total number of entries ever added (`length`) so callers can tell | ||
| * whether a slot is within the live in-memory window. | ||
| * | ||
| * API contract | ||
| * ------------ | ||
| * - `get(index)` — cached item at 0-based `index`, or `null` when the | ||
| * index is outside the in-memory window or the slot has | ||
| * not been written yet. | ||
| * - `set(index, item)` — stores `item` at 0-based `index` if it falls inside | ||
| * the current window; silently ignores out-of-window | ||
| * writes. | ||
| * - `add(item)` — appends `item` at position `length`, advances | ||
| * `length`, and returns the new length. | ||
| * - `truncate(newLength)` — discards entries from `newLength` onwards (nulls | ||
| * their slots) and sets `length = newLength`. Safe to | ||
| * call with `newLength >= length` (grow-only update). | ||
| * - `reset()` — clears all slots and resets `length` to 0. | ||
| */ | ||
| class RingBuffer { | ||
|
|
||
| /** | ||
| * @param {number} capacity Maximum number of entries held in memory. | ||
| */ | ||
| constructor(capacity) { | ||
| this._capacity = Math.max(1, capacity >>> 0); // jshint ignore:line | ||
| this._buffer = new Array(this._capacity); | ||
| this._length = 0; | ||
| } | ||
|
|
||
| /** | ||
| * Total number of items ever appended (not capped at capacity). | ||
| * @type {number} | ||
| */ | ||
| get length() { | ||
| return this._length; | ||
| } | ||
|
|
||
| /** | ||
| * Maximum number of items kept in memory. | ||
| * @type {number} | ||
| */ | ||
| get capacity() { | ||
| return this._capacity; | ||
| } | ||
|
|
||
| /** | ||
| * The smallest 0-based index that is currently inside the in-memory window. | ||
| * Indices below this value are not cached and require a disk read. | ||
| * @type {number} | ||
| */ | ||
| get windowStart() { | ||
| return Math.max(0, this._length - this._capacity); | ||
| } | ||
|
|
||
| /** | ||
| * Return the cached item at the given 0-based index, or `null` if the | ||
| * index is outside the in-memory window or the slot has not been populated. | ||
| * | ||
| * @param {number} index 0-based position. | ||
| * @returns {*|null} | ||
| */ | ||
| get(index) { | ||
| if (index < this.windowStart) { | ||
| return null; | ||
| } | ||
| const item = this._buffer[index % this._capacity]; | ||
| return item !== undefined ? item : null; | ||
| } | ||
|
|
||
| /** | ||
| * Store `item` at the given 0-based `index`. | ||
| * Writes outside the current in-memory window are silently ignored. | ||
| * | ||
| * @param {number} index 0-based position. | ||
| * @param {*} item | ||
| */ | ||
| set(index, item) { | ||
| if (index < this.windowStart) { | ||
| return; | ||
| } | ||
| this._buffer[index % this._capacity] = item; | ||
| } | ||
|
|
||
| /** | ||
| * Append `item` at position `length` and advance `length`. | ||
| * | ||
| * @param {*} item | ||
| * @returns {number} The new length (1-based position of the appended item). | ||
| */ | ||
| add(item) { | ||
| this._buffer[this._length % this._capacity] = item; | ||
| this._length++; | ||
| return this._length; | ||
| } | ||
|
|
||
| /** | ||
| * Discard entries from `newLength` onwards by nulling their cache slots, | ||
| * then set `length = newLength`. | ||
| * | ||
| * When `newLength >= length` no eviction is performed and only `length` is | ||
| * updated (useful when the underlying file has grown and the caller just | ||
| * needs to advance the length counter without populating new slots). | ||
| * | ||
| * @param {number} newLength The new total length. | ||
| */ | ||
| truncate(newLength) { | ||
| if (newLength < this._length) { | ||
| const cacheStart = this.windowStart; | ||
| for (let i = Math.max(cacheStart, newLength); i < this._length; i++) { | ||
| this._buffer[i % this._capacity] = null; | ||
| } | ||
| } | ||
| this._length = newLength; | ||
| } | ||
|
|
||
| /** | ||
| * Clear all cached slots and reset `length` to 0. | ||
| */ | ||
| reset() { | ||
| this._buffer.fill(null); | ||
| this._length = 0; | ||
| } | ||
| } | ||
|
|
||
| module.exports = RingBuffer; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.