Repository navigation
perf(editor): skip the fence regex when an inline reply cannot match it - #3964
Open
Raing5Days wants to merge 1 commit into
Open
Raing5Days wants to merge 1 commit into
Raing5Days wants to merge 1 commit into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
sanitizeInlineAiMarkdownResponseentered 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:
markdown/mdinfo string, still unwraps to the inner text;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:webpass with this change checked out.node scripts/check-git-object-sizes.mjs --base origin/main --head HEADpasses.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.