Use LRU cache for keybinding when-expression AST parsing
- add reusable `LruCache` utility with bounded eviction - replace keybinding AST cache clear-all behavior with true LRU lookups - add unit tests for eviction, recency updates, and capacity validation
This commit is contained in:
1 parent
be32ce9792
commit
b81b5616ec
3 files changed
+81
-10
No files matched your search
+10
-10
@@ -1,4 +1,5 @@
|
||||
import type { KeybindingCommand, KeybindingsConfig } from "@t3tools/contracts";
|
||||
import { LruCache } from "./lib/lru";
|
||||
|
||||
export interface ShortcutEventLike {
|
||||
key: string;
|
||||
@@ -44,8 +45,7 @@ type WhenToken =
|
||||
| { type: "lparen" }
|
||||
| { type: "rparen" };
|
||||
|
||||
const WHEN_AST_CACHE = new Map<string, WhenNode | null>();
|
||||
const MAX_WHEN_AST_CACHE_ENTRIES = 512;
|
||||
const WHEN_AST_CACHE = new LruCache<string, WhenNode | null>(512);
|
||||
|
||||
function isMacPlatform(platform: string): boolean {
|
||||
return /mac|iphone|ipad|ipod/i.test(platform);
|
||||
@@ -290,15 +290,15 @@ function matchesWhenExpression(when: string | undefined, context: ShortcutMatchC
|
||||
const normalized = when.trim();
|
||||
if (normalized.length === 0) return true;
|
||||
|
||||
if (!WHEN_AST_CACHE.has(normalized)) {
|
||||
if (WHEN_AST_CACHE.size >= MAX_WHEN_AST_CACHE_ENTRIES) {
|
||||
WHEN_AST_CACHE.clear();
|
||||
}
|
||||
WHEN_AST_CACHE.set(normalized, parseWhenExpression(normalized));
|
||||
const cached = WHEN_AST_CACHE.get(normalized);
|
||||
if (cached === undefined) {
|
||||
const parsed = parseWhenExpression(normalized);
|
||||
WHEN_AST_CACHE.set(normalized, parsed);
|
||||
if (!parsed) return false;
|
||||
return evaluateWhenNode(parsed, context);
|
||||
}
|
||||
const ast = WHEN_AST_CACHE.get(normalized);
|
||||
if (!ast) return false;
|
||||
return evaluateWhenNode(ast, context);
|
||||
if (!cached) return false;
|
||||
return evaluateWhenNode(cached, context);
|
||||
}
|
||||
|
||||
function resolvePlatform(options: ShortcutMatchOptions | undefined): string {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { assert, describe, it } from "vitest";
|
||||
import { LruCache } from "./lru";
|
||||
|
||||
describe("LruCache", () => {
|
||||
it("evicts the least recently used entry", () => {
|
||||
const cache = new LruCache<string, number>(2);
|
||||
cache.set("a", 1);
|
||||
cache.set("b", 2);
|
||||
assert.strictEqual(cache.get("a"), 1);
|
||||
|
||||
cache.set("c", 3);
|
||||
|
||||
assert.strictEqual(cache.get("b"), undefined);
|
||||
assert.strictEqual(cache.get("a"), 1);
|
||||
assert.strictEqual(cache.get("c"), 3);
|
||||
});
|
||||
|
||||
it("refreshes recency on update", () => {
|
||||
const cache = new LruCache<string, number>(2);
|
||||
cache.set("a", 1);
|
||||
cache.set("b", 2);
|
||||
cache.set("a", 10);
|
||||
cache.set("c", 3);
|
||||
|
||||
assert.strictEqual(cache.get("b"), undefined);
|
||||
assert.strictEqual(cache.get("a"), 10);
|
||||
assert.strictEqual(cache.get("c"), 3);
|
||||
});
|
||||
|
||||
it("rejects non-positive capacities", () => {
|
||||
assert.throws(() => new LruCache<string, number>(0), "positive integer");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
export class LruCache<K, V> {
|
||||
private readonly entries = new Map<K, V>();
|
||||
|
||||
constructor(private readonly maxEntries: number) {
|
||||
if (!Number.isInteger(maxEntries) || maxEntries <= 0) {
|
||||
throw new Error("LruCache maxEntries must be a positive integer.");
|
||||
}
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this.entries.size;
|
||||
}
|
||||
|
||||
get(key: K): V | undefined {
|
||||
const value = this.entries.get(key);
|
||||
if (value === undefined && !this.entries.has(key)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Refresh recency on successful lookup.
|
||||
this.entries.delete(key);
|
||||
this.entries.set(key, value as V);
|
||||
return value;
|
||||
}
|
||||
|
||||
set(key: K, value: V): void {
|
||||
if (this.entries.has(key)) {
|
||||
this.entries.delete(key);
|
||||
}
|
||||
this.entries.set(key, value);
|
||||
|
||||
while (this.entries.size > this.maxEntries) {
|
||||
const oldest = this.entries.keys().next().value as K | undefined;
|
||||
if (oldest === undefined) break;
|
||||
this.entries.delete(oldest);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user