Skip to content

Commit 960b562

Browse files
lambdalisueclaude
andcommitted
perf: add byte length caching for input component prefix/suffix
Cache byte length calculations for prefix and suffix strings in InputComponent to avoid repeated getByteLength() calls during rendering. This improves performance when the same prefix/suffix values are used repeatedly. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 63ff5cd commit 960b562

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

denops/fall/component/input.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export class InputComponent extends BaseComponent {
7979
#modifiedWindow = true;
8080
#modifiedContent = true;
8181

82+
// Cache for byte lengths to avoid repeated calculations
83+
#prefixCache?: { value: string; byteLength: number };
84+
#suffixCache?: { value: string; byteLength: number };
85+
8286
constructor(
8387
{
8488
title,
@@ -287,9 +291,25 @@ export class InputComponent extends BaseComponent {
287291
this.#offset,
288292
this.#offset + cmdwidth,
289293
);
290-
const prefixByteLength = getByteLength(prefix);
294+
295+
// Use cached byte lengths when possible
296+
let prefixByteLength: number;
297+
if (this.#prefixCache?.value === prefix) {
298+
prefixByteLength = this.#prefixCache.byteLength;
299+
} else {
300+
prefixByteLength = getByteLength(prefix);
301+
this.#prefixCache = { value: prefix, byteLength: prefixByteLength };
302+
}
303+
291304
const middleByteLength = getByteLength(middle);
292-
const suffixByteLength = getByteLength(suffix);
305+
306+
let suffixByteLength: number;
307+
if (this.#suffixCache?.value === suffix) {
308+
suffixByteLength = this.#suffixCache.byteLength;
309+
} else {
310+
suffixByteLength = getByteLength(suffix);
311+
this.#suffixCache = { value: suffix, byteLength: suffixByteLength };
312+
}
293313

294314
await buffer.replace(denops, bufnr, [prefix + middle + suffix]);
295315
signal?.throwIfAborted();

0 commit comments

Comments
 (0)