-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat: Enhance autocomplete cache with async initialization and improved eviction logic #8367
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
base: main
Are you sure you want to change the base?
feat: Enhance autocomplete cache with async initialization and improved eviction logic #8367
Conversation
…ed eviction logic
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.
3 issues found across 3 files
Prompt for AI agents (all 3 issues)
Understand the root cause of the following 3 issues and fix them.
<file name="core/autocomplete/util/AutocompleteLruCache.ts">
<violation number="1" location="core/autocomplete/util/AutocompleteLruCache.ts:218">
After close(), we need to clear the singleton so future get() calls reinitialize a fresh cache; otherwise callers receive an instance whose DB connection is already closed and whose flush timer never restarts.</violation>
</file>
<file name="core/autocomplete/util/AutocompleteLruCache.test.ts">
<violation number="1" location="core/autocomplete/util/AutocompleteLruCache.test.ts:4">
Mocking `async-mutex` removes the real Mutex implementation, so `cache.put` fails when it tries to call `this.mutex.acquire()`.</violation>
<violation number="2" location="core/autocomplete/util/AutocompleteLruCache.test.ts:638">
`jest.doMock("sqlite", …)` runs after AutocompleteLruCache imported `open`, leaving `open` as the default auto-mock and causing `AutocompleteLruCache.get()` to crash with an undefined database handle.</violation>
</file>
React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.
| * Gracefully shuts down the cache. | ||
| * Stops the flush timer, persists pending changes, and closes database connection. | ||
| */ | ||
| async close() { |
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.
After close(), we need to clear the singleton so future get() calls reinitialize a fresh cache; otherwise callers receive an instance whose DB connection is already closed and whose flush timer never restarts.
Prompt for AI agents
Address the following comment on core/autocomplete/util/AutocompleteLruCache.ts at line 218:
<comment>After close(), we need to clear the singleton so future get() calls reinitialize a fresh cache; otherwise callers receive an instance whose DB connection is already closed and whose flush timer never restarts.</comment>
<file context>
@@ -1,121 +1,227 @@
+ * Gracefully shuts down the cache.
+ * Stops the flush timer, persists pending changes, and closes database connection.
+ */
+ async close() {
+ if (this.flushTimer) {
+ clearInterval(this.flushTimer);
</file context>
✅ Addressed in 52ebf5d
|
|
||
| it("should return same instance on multiple calls", async () => { | ||
| const mockOpen = jest.fn().mockResolvedValue(mockDb); | ||
| jest.doMock("sqlite", () => ({ open: mockOpen })); |
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.
jest.doMock("sqlite", …) runs after AutocompleteLruCache imported open, leaving open as the default auto-mock and causing AutocompleteLruCache.get() to crash with an undefined database handle.
Prompt for AI agents
Address the following comment on core/autocomplete/util/AutocompleteLruCache.test.ts at line 638:
<comment>`jest.doMock("sqlite", …)` runs after AutocompleteLruCache imported `open`, leaving `open` as the default auto-mock and causing `AutocompleteLruCache.get()` to crash with an undefined database handle.</comment>
<file context>
@@ -0,0 +1,646 @@
+
+ it("should return same instance on multiple calls", async () => {
+ const mockOpen = jest.fn().mockResolvedValue(mockDb);
+ jest.doMock("sqlite", () => ({ open: mockOpen }));
+
+ const instance1 = await AutocompleteLruCache.get();
</file context>
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.
Seems to be working to me correctly
| import AutocompleteLruCache from "./AutocompleteLruCache"; | ||
|
|
||
| // Mock the external dependencies before importing | ||
| jest.mock("async-mutex"); |
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.
Mocking async-mutex removes the real Mutex implementation, so cache.put fails when it tries to call this.mutex.acquire().
Prompt for AI agents
Address the following comment on core/autocomplete/util/AutocompleteLruCache.test.ts at line 4:
<comment>Mocking `async-mutex` removes the real Mutex implementation, so `cache.put` fails when it tries to call `this.mutex.acquire()`.</comment>
<file context>
@@ -0,0 +1,646 @@
+import AutocompleteLruCache from "./AutocompleteLruCache";
+
+// Mock the external dependencies before importing
+jest.mock("async-mutex");
+jest.mock("sqlite");
+jest.mock("sqlite3");
</file context>
✅ Addressed in 52ebf5d
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.
No issues found across 2 files
Based on discussion #8315 Implement asynchronous initialization for the autocomplete cache and refine eviction logic to optimize performance and memory usage. This update ensures a more efficient handling of cached entries with SQLite persistence.
Summary by cubic
Initialize the autocomplete cache asynchronously and move lookups to an in-memory LRU with periodic SQLite persistence. This improves startup, speeds up cache hits, and tightens eviction for better memory usage.