Download src/ink/line-width-cache.ts from chenbhao/codev: direct link, hf CLI and curl.
- Browser
- Download file 734 Bytes
-
https://huggingface.co/chenbhao/codev/resolve/afbc7edaae4e6bb0102ba024b49ce8be89d2264c/src/ink/line-width-cache.ts
- Command line
-
hf download hf://chenbhao/codev@afbc7edaae4e6bb0102ba024b49ce8be89d2264c/src/ink/line-width-cache.ts
-
curl -L -o line-width-cache.ts https://huggingface.co/chenbhao/codev/resolve/afbc7edaae4e6bb0102ba024b49ce8be89d2264c/src/ink/line-width-cache.ts
734 Bytes
| import { stringWidth } from './stringWidth.js' | |
| // During streaming, text grows but completed lines are immutable. | |
| // Caching stringWidth per-line avoids re-measuring hundreds of | |
| // unchanged lines on every token (~50x reduction in stringWidth calls). | |
| const cache = new Map<string, number>() | |
| const MAX_CACHE_SIZE = 4096 | |
| export function lineWidth(line: string): number { | |
| const cached = cache.get(line) | |
| if (cached !== undefined) return cached | |
| const width = stringWidth(line) | |
| // Evict when cache grows too large (e.g. after many different responses). | |
| // Simple full-clear is fine — the cache repopulates in one frame. | |
| if (cache.size >= MAX_CACHE_SIZE) { | |
| cache.clear() | |
| } | |
| cache.set(line, width) | |
| return width | |
| } | |