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..d2765b5 100644 --- a/denops/ddx/context.ts +++ b/denops/ddx/context.ts @@ -134,11 +134,11 @@ 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. + // 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, @@ -179,11 +179,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..e540950 100644 --- a/denops/ddx/decoder.ts +++ b/denops/ddx/decoder.ts @@ -26,6 +26,12 @@ function getCp932Decoder(): TextDecoder { return new 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 { const out: string[] = []; let i = 0; @@ -60,8 +66,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 === "�") {