Skip to content

perf: apply six safe optimizations across core modules - #8

Merged
Shougo merged 4 commits into
mainfrom
copilot/analyze-codebase-for-optimizations
May 6, 2026
Merged

Shougo merged 4 commits into
mainfrom
copilot/analyze-codebase-for-optimizations

Conversation

Copilot AI commented May 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Six safe, zero-behavior-change performance optimizations identified in a prior codebase analysis.


decoder.ts — Memoize getCp932Decoder() at module scope

Previously, getCp932Decoder() was called inside the byte-processing loop, constructing a new TextDecoder (via a label-probe loop that tries up to 5 encoding labels) for every valid CP932 two-byte pair. For a 1 MB CP932 binary, this could mean tens of thousands of unnecessary TextDecoder constructions.

Now a single CP932_DECODER constant is created at module load time and reused across all calls. A comment explains why this is safe: TextDecoder.decode() without the stream option is stateless between calls, and Deno is single-threaded within an isolate.


buffer.ts — Hoist fatalDecoderSupported probe to module level

The capability probe for fatal-mode TextDecoder support was an IIFE re-evaluated on every #searchUtf8 call — i.e., every getStrings request. It allocates a TextDecoder and calls .decode() each time.

Now a module-level constant FATAL_DECODER_SUPPORTED holds the result, computed exactly once on module load.


context.ts — Parallelize bufNr + winId Vim IPC calls

fn.bufnr and fn.win_getid are independent calls. They were awaited sequentially, incurring two round-trips to Vim/Neovim before start could proceed. Changed to Promise.all.


context.ts — Remove redundant first foldMerge in Custom.get()

Custom.get() previously ran a full foldMerge (spread-object allocation over all options fields) solely to read options.name, then discarded the entire result and ran a second foldMerge for the real return value.

Replaced with a direct property lookup chain: userOptions["name"] ?? this.global.name ?? "default". Bracket notation is used for the untyped Record<string, unknown> userOptions; dot notation for the typed Partial<DdxOptions> this.global.


ddx.ts — Parallelize fn.expand + fn.getcwd in restart()

fn.expand (path expansion) and fn.getcwd (working directory) are independent Vim IPC calls, awaited sequentially for both the primary path and anotherPath. Changed to Promise.all for each pair.


app.ts — Replace slice(-1)[0] with at(-1)! in getDdx

Array.prototype.slice(-1) allocates a new single-element array. This is called by virtually every dispatcher function (start, restart, redraw, uiAction, analyze, change, jump, getDiff, getStrings). at(-1) reads the last element directly without allocation.

Copilot AI and others added 3 commits May 6, 2026 02:49
- 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>
…okup

- 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>
- Use dot notation for this.global.name (typed Partial<DdxOptions>)
  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>
Copilot AI requested a review from Shougo May 6, 2026 02:52
Copilot finished work on behalf of Shougo May 6, 2026 02:52
@Shougo
Shougo marked this pull request as ready for review May 6, 2026 02:52
@Shougo
Shougo merged commit 39c1092 into main May 6, 2026
3 checks passed
@Shougo
Shougo deleted the copilot/analyze-codebase-for-optimizations branch May 6, 2026 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants