From 584f4691dd5ecfd41081e20da8b091871fd81ef7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 6 May 2026 02:49:06 +0000 Subject: [PATCH 1/4] perf: apply six safe optimizations across core modules - decoder.ts: memoize CP932 TextDecoder at module scope; was re-constructed on every valid multibyte pair, O(n) overhead for CP932 files - buffer.ts: hoist fatalDecoderSupported probe to module-level constant FATAL_DECODER_SUPPORTED; was re-evaluated (including a TextDecoder allocation) on every #searchUtf8 call - context.ts: parallelize bufNr + winId Vim IPC calls with Promise.all; removes one sequential round-trip on every start - context.ts: remove redundant first foldMerge in Custom.get(); was done solely to extract `name`, then the result was discarded - ddx.ts: parallelize fn.expand + fn.getcwd Vim IPC calls with Promise.all in restart() for both primary and anotherPath opens - app.ts: replace slice(-1)[0] with at(-1)! in getDdx; avoids a single-element array allocation on every dispatcher call Agent-Logs-Url: https://github.com/Shougo/ddx.vim/sessions/19fb8487-9d09-400d-9e14-a81b0e5142f6 Co-authored-by: Shougo <41495+Shougo@users.noreply.github.com> --- denops/ddx/app.ts | 2 +- denops/ddx/buffer.ts | 21 ++++++++++++--------- denops/ddx/context.ts | 17 ++++++++++------- denops/ddx/ddx.ts | 16 ++++++++++++---- denops/ddx/decoder.ts | 6 ++++-- 5 files changed, 39 insertions(+), 23 deletions(-) diff --git a/denops/ddx/app.ts b/denops/ddx/app.ts index b71748a..d9f2cea 100644 --- a/denops/ddx/app.ts +++ b/denops/ddx/app.ts @@ -39,7 +39,7 @@ export const main: Entrypoint = (denops: Denops) => { if (ddxs[name].length == 0) { ddxs[name].push(new Ddx(getLoader(name))); } - return ddxs[name].slice(-1)[0]; + return ddxs[name].at(-1)!; }; const getLoader = (name: string) => { if (!loaders[name]) { diff --git a/denops/ddx/buffer.ts b/denops/ddx/buffer.ts index 3e082b0..f3c6569 100644 --- a/denops/ddx/buffer.ts +++ b/denops/ddx/buffer.ts @@ -7,6 +7,17 @@ import { join } from "@std/path/join"; import { resolve } from "@std/path/resolve"; import { isAbsolute } from "@std/path/is-absolute"; +// Probe once at module load time: avoids repeated TextDecoder construction +// inside #searchUtf8 which is called for every string-extraction request. +const FATAL_DECODER_SUPPORTED = (() => { + try { + new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array()); + return true; + } catch { + return false; + } +})(); + type OperationHistory = | ChangeHistory | ChangeBytesHistory @@ -594,14 +605,6 @@ export class DdxBuffer { const isUtf8Start = (b: number) => !(b >= 0x80 && b <= 0xbf); let i = 0; - const fatalDecoderSupported = (() => { - try { - new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array()); - return true; - } catch { - return false; - } - })(); while (i < n) { if (!isUtf8Start(bytes[i])) { @@ -612,7 +615,7 @@ export class DdxBuffer { let lastGood = -1; for (let j = i; j < n; j++) { const slice = bytes.subarray(i, j + 1); - if (fatalDecoderSupported) { + if (FATAL_DECODER_SUPPORTED) { const decoded = this.#tryDecodeFatal(slice, "utf-8"); if (decoded === null) break; lastGood = j; diff --git a/denops/ddx/context.ts b/denops/ddx/context.ts index 07939e6..dfdd74a 100644 --- a/denops/ddx/context.ts +++ b/denops/ddx/context.ts @@ -134,11 +134,9 @@ class Custom { local: Record> = {}; get(userOptions: UserOptions): DdxOptions { - const options = foldMerge(mergeDdxOptions, defaultDdxOptions, [ - this.global, - userOptions, - ]); - const name = options.name; + // Extract the effective name cheaply without a full merge pass. + const name = (userOptions["name"] ?? this.global.name ?? + defaultDdxOptions().name) as string; const local = this.local[name] || {}; return foldMerge(mergeDdxOptions, defaultDdxOptions, [ this.global, @@ -179,11 +177,16 @@ export class ContextBuilder { await this.validate(denops, "options", userOptions, defaultDdxOptions()); + const [bufNr, winId] = await Promise.all([ + fn.bufnr(denops, "%"), + fn.win_getid(denops), + ]); + return [ { ...defaultContext(), - bufNr: await fn.bufnr(denops, "%"), - winId: await fn.win_getid(denops) as number, + bufNr: bufNr as number, + winId: winId as number, }, userOptions, ]; diff --git a/denops/ddx/ddx.ts b/denops/ddx/ddx.ts index a4bb6b1..7bb665d 100644 --- a/denops/ddx/ddx.ts +++ b/denops/ddx/ddx.ts @@ -67,9 +67,13 @@ export class Ddx { } try { + const [expandedPath, cwd] = await Promise.all([ + fn.expand(denops, this.#options.path) as Promise, + fn.getcwd(denops), + ]); await this.#buffer.open( - await fn.expand(denops, this.#options.path) as string, - await fn.getcwd(denops), + expandedPath, + cwd, Number(this.#options.offset), Number(this.#options.length), ); @@ -84,9 +88,13 @@ export class Ddx { if (this.#options.anotherPath.length > 0) { try { + const [expandedAnotherPath, cwd] = await Promise.all([ + fn.expand(denops, this.#options.anotherPath) as Promise, + fn.getcwd(denops), + ]); await this.#anotherBuffer.open( - await fn.expand(denops, this.#options.anotherPath) as string, - await fn.getcwd(denops), + expandedAnotherPath, + cwd, Number(this.#options.offset), Number(this.#options.length), ); diff --git a/denops/ddx/decoder.ts b/denops/ddx/decoder.ts index 6f8ce0e..9c910b0 100644 --- a/denops/ddx/decoder.ts +++ b/denops/ddx/decoder.ts @@ -26,6 +26,9 @@ function getCp932Decoder(): TextDecoder { return new TextDecoder(); } +// Memoized CP932 decoder: constructed once, reused for every multibyte pair. +const CP932_DECODER = getCp932Decoder(); + export function bytesToCP932(buf: Uint8Array): string { const out: string[] = []; let i = 0; @@ -60,8 +63,7 @@ export function bytesToCP932(buf: Uint8Array): string { if (isValidShiftJisMultibyte(byte1, byte2)) { try { - const decoder = getCp932Decoder(); - const decoded = decoder.decode(new Uint8Array([byte1, byte2])); + const decoded = CP932_DECODER.decode(new Uint8Array([byte1, byte2])); // Replace the decoded character if it's a replacement character if (decoded === "�") { From 0d7af953254972d713b58d40146ce6eb80a13bd5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 6 May 2026 02:50:09 +0000 Subject: [PATCH 2/4] perf: address review comments on CP932 decoder and Custom.get name lookup - decoder.ts: add comment explaining TextDecoder.decode() stateless behavior and Deno single-threaded safety for the memoized instance - context.ts: use consistent bracket notation for both userOptions and this.global name access in Custom.get(), with explanatory comment Agent-Logs-Url: https://github.com/Shougo/ddx.vim/sessions/19fb8487-9d09-400d-9e14-a81b0e5142f6 Co-authored-by: Shougo <41495+Shougo@users.noreply.github.com> --- denops/ddx/context.ts | 4 +++- denops/ddx/decoder.ts | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/denops/ddx/context.ts b/denops/ddx/context.ts index dfdd74a..4347126 100644 --- a/denops/ddx/context.ts +++ b/denops/ddx/context.ts @@ -135,7 +135,9 @@ class Custom { get(userOptions: UserOptions): DdxOptions { // Extract the effective name cheaply without a full merge pass. - const name = (userOptions["name"] ?? this.global.name ?? + // userOptions is typed as Record, so bracket notation + // is used to make the dynamic key access explicit. + const name = (userOptions["name"] ?? this.global["name"] ?? defaultDdxOptions().name) as string; const local = this.local[name] || {}; return foldMerge(mergeDdxOptions, defaultDdxOptions, [ diff --git a/denops/ddx/decoder.ts b/denops/ddx/decoder.ts index 9c910b0..e540950 100644 --- a/denops/ddx/decoder.ts +++ b/denops/ddx/decoder.ts @@ -27,6 +27,9 @@ function getCp932Decoder(): TextDecoder { } // Memoized CP932 decoder: constructed once, reused for every multibyte pair. +// TextDecoder.decode() without the `stream` option resets any internal state +// after each call, so reusing the instance is safe in a single-threaded +// JavaScript runtime (Deno does not share module-scope state across Workers). const CP932_DECODER = getCp932Decoder(); export function bytesToCP932(buf: Uint8Array): string { From 572b193935f62fe390b3bb9d2a2e4e08745e36ea Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 6 May 2026 02:51:08 +0000 Subject: [PATCH 3/4] perf: address second round of review comments in Custom.get() - Use dot notation for this.global.name (typed Partial) while keeping bracket notation only for untyped userOptions - Replace defaultDdxOptions().name with the literal "default" to avoid constructing the full defaults object just for one field Agent-Logs-Url: https://github.com/Shougo/ddx.vim/sessions/19fb8487-9d09-400d-9e14-a81b0e5142f6 Co-authored-by: Shougo <41495+Shougo@users.noreply.github.com> --- denops/ddx/context.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/denops/ddx/context.ts b/denops/ddx/context.ts index 4347126..cd98942 100644 --- a/denops/ddx/context.ts +++ b/denops/ddx/context.ts @@ -135,10 +135,9 @@ class Custom { get(userOptions: UserOptions): DdxOptions { // Extract the effective name cheaply without a full merge pass. - // userOptions is typed as Record, so bracket notation - // is used to make the dynamic key access explicit. - const name = (userOptions["name"] ?? this.global["name"] ?? - defaultDdxOptions().name) as string; + // userOptions is Record so bracket notation is used; + // this.global is Partial so dot notation is appropriate there. + const name = (userOptions["name"] ?? this.global.name ?? "default") as string; const local = this.local[name] || {}; return foldMerge(mergeDdxOptions, defaultDdxOptions, [ this.global, From 02d9cb57393c04902ab11296c62e757375a46f94 Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Wed, 6 May 2026 11:57:00 +0900 Subject: [PATCH 4/4] Format --- denops/ddx/context.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/denops/ddx/context.ts b/denops/ddx/context.ts index cd98942..d2765b5 100644 --- a/denops/ddx/context.ts +++ b/denops/ddx/context.ts @@ -137,7 +137,8 @@ class Custom { // Extract the effective name cheaply without a full merge pass. // userOptions is Record so bracket notation is used; // this.global is Partial so dot notation is appropriate there. - const name = (userOptions["name"] ?? this.global.name ?? "default") as string; + const name = + (userOptions["name"] ?? this.global.name ?? "default") as string; const local = this.local[name] || {}; return foldMerge(mergeDdxOptions, defaultDdxOptions, [ this.global,