Skip to content

perf(editor): skip the fence regex when an inline reply cannot match it - #3964

Open
Raing5Days wants to merge 1 commit into
openBitFun:mainfrom
Raing5Days:perf/inline-ai-markdown-fence
Open

Raing5Days wants to merge 1 commit into
openBitFun:mainfrom
Raing5Days:perf/inline-ai-markdown-fence

Conversation

@Raing5Days

Copy link
Copy Markdown
Contributor

What

sanitizeInlineAiMarkdownResponse entered the regex engine for every inline reply. The fence pattern is anchored at both ends (/^```(?:markdown|md)?\s*([\s\S]*?)\s*```$/i), so when the reply does not start with a fence the engine still starts a match attempt at every position of the whole string, and then fails. The literal was also built at the call site on every call.

Both are module constants now, and a reply that does not open with a backtick returns the trimmed text directly, without entering the engine.

Behaviour

Unchanged output for every shape the function handles:

  • a fenced reply, with or without a markdown/md info string, still unwraps to the inner text;
  • a reply that opens with a backtick but is not a single complete fence still returns the trimmed text;
  • a reply with no fence returns the trimmed text (the common case, and now the cheapest one).

No other code path changes: the function is called once per inline AI response before insertion, so this is a pure, local guard.

Evidence

Local OpCase benchmark for this module (200,000 repetitions per cycle, 5 baseline samples then 3 verification samples, per-call median): 0.078 → 0.040 µs/call, worst call 0.044 µs, against a 0.0548 µs/call budget (70% of the pre-change median). The number is module cost for one call, not a frame time.

Testing

  • node scripts/run-vitest.mjs run src/tools/editor/meditor/utils/inlineAi.test.ts → 3 tests pass.
  • pnpm run lint:web, pnpm run type-check:web pass with this change checked out.
  • node scripts/check-git-object-sizes.mjs --base origin/main --head HEAD passes.
  • Remote scenarios: none exercised. No IPC, persistence, protocol, remote-surface or permission path is touched.

This change was previously part of #3960 as a second hunk; that PR now carries only the global-search cache, and this file is submitted here on its own.

AI-assisted: yes, written with an agent. Testing level: fully tested for this module (unit + benchmark), no release build or GUI run.

sanitizeInlineAiMarkdownResponse entered the regex engine for every inline
reply. The pattern is anchored at both ends, so the engine starts a match
attempt at every position even though only a leading fence can ever match, and
the pattern literal was rebuilt at the call site on every call.

The pattern and its only possible first character are module constants now, and
a reply that does not open with a backtick returns the trimmed text directly.
Behaviour is unchanged: a fenced reply with or without a language tag, and a
reply with no fence, all produce the same string as before.

Measured with the local OpCase benchmark for this module over 200,000
repetitions per cycle: 0.078 us/call -> 0.040 us/call (worst call 0.044 us)
against a 0.0548 us/call budget, which is 70% of the pre-change median. The
number is module cost, not a frame time.

Testing: src/tools/editor/meditor/utils/inlineAi.test.ts (3 passed).
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.

1 participant