-
Notifications
You must be signed in to change notification settings - Fork 1
⚡ Bolt: Reuse Uint8Array and fast O(K) reset for visited tracker #414
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
Closed
AhmmedSamier
wants to merge
1
commit into
master
from
bolt-fast-uint8array-reset-3281649880735625941
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #!/bin/bash | ||
| patch -p1 << 'PATCH' | ||
| --- a/src/core/search-engine.ts | ||
| +++ b/src/core/search-engine.ts | ||
| @@ -183,6 +183,9 @@ | ||
| topIndices: number[]; | ||
| } | null = null; | ||
|
|
||
| + private visitedBuffer: Uint8Array = new Uint8Array(0); | ||
| + private visitedTracker: number[] = []; | ||
| + | ||
| public itemsMap: Map<string, SearchableItem> = new Map(); | ||
| private readonly fileItemByNormalizedPath: Map<string, SearchableItem> = new Map(); | ||
| private activityWeight: number = 0.3; | ||
| @@ -1631,16 +1634,29 @@ | ||
| const searchContext = this.prepareSearchContext(query, scope); | ||
| // ⚡ Bolt: Fast integer ID tracking using Uint8Array instead of Set | ||
| const preferredIndices = this.getPreferredIndicesForQuery(scope, query, indices); | ||
| - const visited = preferredIndices.length > 0 ? new Uint8Array(this.items.length) : undefined; | ||
| - | ||
| - if (preferredIndices.length > 0) { | ||
| - this.searchWithIndices(preferredIndices, searchContext, heap, token, visited); | ||
| + | ||
| + const useVisited = preferredIndices.length > 0; | ||
| + if (useVisited && this.visitedBuffer.length < this.items.length) { | ||
| + this.visitedBuffer = new Uint8Array(this.items.length); | ||
| } | ||
|
|
||
| - if (indices) { | ||
| - this.searchWithIndices(indices, searchContext, heap, token, visited); | ||
| - } else { | ||
| - this.searchAllItems(searchContext, heap, token, visited); | ||
| + try { | ||
| + if (preferredIndices.length > 0) { | ||
| + this.searchWithIndices(preferredIndices, searchContext, heap, token, useVisited); | ||
| + } | ||
| + | ||
| + if (indices) { | ||
| + this.searchWithIndices(indices, searchContext, heap, token, useVisited); | ||
| + } else { | ||
| + this.searchAllItems(searchContext, heap, token, useVisited); | ||
| + } | ||
| + } finally { | ||
| + if (useVisited) { | ||
| + for (let i = 0; i < this.visitedTracker.length; i++) { | ||
| + this.visitedBuffer[this.visitedTracker[i]] = 0; | ||
| + } | ||
| + this.visitedTracker.length = 0; | ||
| + } | ||
| } | ||
|
|
||
| const results = heap.getSorted(); | ||
| @@ -1776,16 +1792,17 @@ | ||
| context: ReturnType<typeof this.prepareSearchContext>, | ||
| heap: MinHeap<SearchResult>, | ||
| token?: CancellationToken, | ||
| - visited?: Uint8Array, | ||
| + useVisited: boolean = false, | ||
| ): void { | ||
| for (let j = 0; j < indices.length; j++) { | ||
| if (j % 500 === 0 && token?.isCancellationRequested) break; | ||
| const i = indices[j]; | ||
| - if (visited) { | ||
| - if (visited[i] === 1) { | ||
| + if (useVisited) { | ||
| + if (this.visitedBuffer[i] === 1) { | ||
| continue; | ||
| } | ||
| - visited[i] = 1; | ||
| + this.visitedBuffer[i] = 1; | ||
| + this.visitedTracker.push(i); | ||
| } | ||
| this.processItemForSearch(i, context, heap); | ||
| } | ||
| @@ -1796,12 +1813,12 @@ | ||
| context: ReturnType<typeof this.prepareSearchContext>, | ||
| heap: MinHeap<SearchResult>, | ||
| token?: CancellationToken, | ||
| - visited?: Uint8Array, | ||
| + useVisited: boolean = false, | ||
| ): void { | ||
| const count = context.items.length; | ||
| for (let i = 0; i < count; i++) { | ||
| if (i % 500 === 0 && token?.isCancellationRequested) break; | ||
| - if (visited && visited[i] === 1) { | ||
| + if (useVisited && this.visitedBuffer[i] === 1) { | ||
| continue; | ||
| } | ||
| this.processItemForSearch(i, context, heap); | ||
| } | ||
| PATCH |
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,29 @@ | ||
| [ | ||
| { | ||
| "name": "Burst Search \"App\" (Matches found)", | ||
| "avgMs": 0.022927290000000086, | ||
| "totalMs": 2.2927290000000085, | ||
| "minMs": 0.005182999999988169, | ||
| "maxMs": 0.22028199999999742, | ||
| "p95Ms": 0.07572100000004411, | ||
| "stdDevMs": 0.03326143676686062 | ||
| }, | ||
| { | ||
| "name": "Burst Search \"Zzz\" (No match)", | ||
| "avgMs": 25.31047841, | ||
| "totalMs": 2531.047841, | ||
| "minMs": 21.281904999999824, | ||
| "maxMs": 58.99728899999991, | ||
| "p95Ms": 50.09768800000029, | ||
| "stdDevMs": 7.7481483316893245 | ||
| }, | ||
| { | ||
| "name": "Burst Search \"S\" (Many matches)", | ||
| "avgMs": 0.6536615299999994, | ||
| "totalMs": 65.36615299999994, | ||
| "minMs": 0.5750079999997979, | ||
| "maxMs": 0.8889819999999418, | ||
| "p95Ms": 0.7484010000002854, | ||
| "stdDevMs": 0.05215705758937176 | ||
| } | ||
| ] |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset reusable visited state when clearing engine state.
clear()(Line 756 onward) doesn’t reset the newvisitedBuffer/visitedTracker, so a previously largeUint8Arraycan stay retained after a full clear. Please clear these alongside other caches.💡 Proposed fix
clear(): void { this.items = []; @@ this.inactiveFileItems = []; + this.visitedBuffer = new Uint8Array(0); + this.visitedTracker = []; this.invalidateDerivedCaches(); }🤖 Prompt for AI Agents