Skip to content

Commit

Permalink
Merge pull request #66 from arshad-yaseen/use-queue-for-completion-cache
Browse files Browse the repository at this point in the history
Use Queue for Completions Cache Management
  • Loading branch information
arshad-yaseen authored Oct 26, 2024
2 parents 042165c + 591787f commit 3ba056a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
5 changes: 1 addition & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@


## [0.12.7](https://github.com/arshad-yaseen/monacopilot/compare/v0.12.6...v0.12.7) (2024-10-25)


### 📚 Documentation

* make the cross language api handler heading better ([f1190fe](https://github.com/arshad-yaseen/monacopilot/commit/f1190feed0f8097dead2cb2c931cfa26833ae9a4))
- make the cross language api handler heading better ([f1190fe](https://github.com/arshad-yaseen/monacopilot/commit/f1190feed0f8097dead2cb2c931cfa26833ae9a4))

## [0.12.6](https://github.com/arshad-yaseen/monacopilot/compare/v0.12.5...v0.12.6) (2024-10-25)

Expand Down
21 changes: 11 additions & 10 deletions src/classes/completion-cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {CompletionCacheItem, CursorPosition, EditorModel} from '../types';
import {getTextBeforeCursor} from '../utils/editor';
import {Queue} from './queue';

/**
* Manages a cache of code completions with FIFO eviction policy.
* Provides methods to retrieve, add, and clear cached completion items.
*/
export class CompletionCache {
private static readonly MAX_CACHE_SIZE = 10;
private cache: CompletionCacheItem[] = [];
private cache: Queue<CompletionCacheItem>;

constructor() {
this.cache = new Queue<CompletionCacheItem>(CompletionCache.MAX_CACHE_SIZE);
}

/**
* Retrieves cached completion items that are valid based on the current cursor position and editor model.
Expand All @@ -19,9 +24,9 @@ export class CompletionCache {
pos: Readonly<CursorPosition>,
mdl: Readonly<EditorModel>,
): readonly CompletionCacheItem[] {
return this.cache.filter(cacheItem =>
this.isValidCacheItem(cacheItem, pos, mdl),
);
return this.cache
.getAll()
.filter(cacheItem => this.isValidCacheItem(cacheItem, pos, mdl));
}

/**
Expand All @@ -30,18 +35,14 @@ export class CompletionCache {
* @param cacheItem - The completion item to add to the cache.
*/
public add(cacheItem: Readonly<CompletionCacheItem>): void {
const updatedCache = [
...this.cache.slice(-(CompletionCache.MAX_CACHE_SIZE - 1)),
cacheItem,
];
this.cache = updatedCache;
this.cache.enqueue(cacheItem);
}

/**
* Clears all items from the completion cache.
*/
public clear(): void {
this.cache = [];
this.cache.clear();
}

/**
Expand Down
89 changes: 89 additions & 0 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* A generic queue implementation with a maximum size.
* Uses a circular buffer for efficient space utilization.
*/
export class Queue<T> {
private buffer: (T | undefined)[];
private head: number = 0;
private tail: number = 0;
private size: number = 0;

constructor(private readonly capacity: number) {
this.buffer = new Array(capacity);
}

/**
* Adds an item to the queue. If the queue is full, the oldest item is removed.
* @param item - The item to be added to the queue.
* @returns The item that was removed if the queue was full, undefined otherwise.
*/
enqueue(item: T): T | undefined {
let removedItem: T | undefined;

if (this.size === this.capacity) {
removedItem = this.dequeue();
}

this.buffer[this.tail] = item;
this.tail = (this.tail + 1) % this.capacity;
this.size++;

return removedItem;
}

/**
* Removes and returns the oldest item from the queue.
* @returns The removed item, or undefined if the queue is empty.
*/
dequeue(): T | undefined {
if (this.size === 0) return undefined;

const item = this.buffer[this.head];
this.buffer[this.head] = undefined;
this.head = (this.head + 1) % this.capacity;
this.size--;

return item;
}

/**
* Returns all items in the queue without removing them.
* @returns An array of all items in the queue, in order from oldest to newest.
*/
getAll(): T[] {
return this.buffer.filter((item): item is T => item !== undefined);
}

/**
* Clears all items from the queue.
*/
clear(): void {
this.buffer = new Array(this.capacity);
this.head = 0;
this.tail = 0;
this.size = 0;
}

/**
* Returns the current number of items in the queue.
*/
getSize(): number {
return this.size;
}

/**
* Checks if the queue is empty.
* @returns True if the queue is empty, false otherwise.
*/
isEmpty(): boolean {
return this.size === 0;
}

/**
* Checks if the queue is full.
* @returns True if the queue is full, false otherwise.
*/
isFull(): boolean {
return this.size === this.capacity;
}
}

0 comments on commit 3ba056a

Please sign in to comment.