perf(ui): optimize text buffer and highlighting for large inputs (#16782)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
N. Taylor Mullen
2026-01-16 09:33:13 -08:00
committed by GitHub
parent fcd860e1b0
commit be37c26c88
10 changed files with 469 additions and 76 deletions
+70
View File
@@ -0,0 +1,70 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { LruCache } from './LruCache.js';
describe('LruCache', () => {
it('should store and retrieve values', () => {
const cache = new LruCache<string, number>(10);
cache.set('a', 1);
expect(cache.get('a')).toBe(1);
expect(cache.get('b')).toBeUndefined();
});
it('should evict oldest items when limit is reached', () => {
const cache = new LruCache<string, number>(2);
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
expect(cache.get('a')).toBeUndefined();
expect(cache.get('b')).toBe(2);
expect(cache.get('c')).toBe(3);
});
it('should update LRU order on get', () => {
const cache = new LruCache<string, number>(2);
cache.set('a', 1);
cache.set('b', 2);
cache.get('a'); // Mark 'a' as recently used
cache.set('c', 3); // Should evict 'b'
expect(cache.get('b')).toBeUndefined();
expect(cache.get('a')).toBe(1);
expect(cache.get('c')).toBe(3);
});
it('should correctly handle falsy values (truthiness bug fix)', () => {
const cache = new LruCache<string, unknown>(2);
cache.set('zero', 0);
cache.set('empty', '');
// Both should be in cache
expect(cache.get('zero')).toBe(0);
expect(cache.get('empty')).toBe('');
// Access 'zero' to move it to end
cache.get('zero');
// Add new item, should evict 'empty' (because 'zero' was just accessed)
cache.set('new', 'item');
expect(cache.get('empty')).toBeUndefined();
expect(cache.get('zero')).toBe(0);
expect(cache.get('new')).toBe('item');
});
it('should correctly handle undefined as a value', () => {
// NOTE: mnemonist LRUMap returns undefined if not found.
// If we store undefined, we can't distinguish between "not found" and "found undefined".
// But we should at least check its behavior.
const cache = new LruCache<string, unknown>(10);
cache.set('key', undefined);
expect(cache.has('key')).toBe(true);
expect(cache.get('key')).toBeUndefined();
});
});
+9 -19
View File
@@ -4,34 +4,24 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { LRUMap } from 'mnemonist';
export class LruCache<K, V> {
private cache: Map<K, V>;
private maxSize: number;
private cache: LRUMap<K, V>;
constructor(maxSize: number) {
this.cache = new Map<K, V>();
this.maxSize = maxSize;
this.cache = new LRUMap<K, V>(maxSize);
}
get(key: K): V | undefined {
const value = this.cache.get(key);
if (value) {
// Move to end to mark as recently used
this.cache.delete(key);
this.cache.set(key, value);
}
return value;
return this.cache.get(key);
}
has(key: K): boolean {
return this.cache.has(key);
}
set(key: K, value: V): void {
if (this.cache.has(key)) {
this.cache.delete(key);
} else if (this.cache.size >= this.maxSize) {
const firstKey = this.cache.keys().next().value;
if (firstKey !== undefined) {
this.cache.delete(firstKey);
}
}
this.cache.set(key, value);
}