Skip to content

mergeEditor: optimize removeDiffs from O(K*N) to single-pass O(N)#304404

Merged
hediet merged 1 commit intomicrosoft:mainfrom
xingsy97:wt/merge-editor-removediffs-perf
Mar 25, 2026
Merged

mergeEditor: optimize removeDiffs from O(K*N) to single-pass O(N)#304404
hediet merged 1 commit intomicrosoft:mainfrom
xingsy97:wt/merge-editor-removediffs-perf

Conversation

@xingsy97
Copy link
Copy Markdown
Member

Problem

removeDiffs() processes each diff removal with two full-array passes (filter + map), creating 2K temporary arrays for K removals. Complexity is O(K * N) where N is the total diff count.

When accepting/rejecting all changes in a complex merge with 50+ conflicts, this becomes noticeable. Marked // TODO improve performance in the code.

Fix

Replace the per-removal loop with a single-pass algorithm:

  1. Validate all diffs-to-remove exist using a Set for O(1) lookup
  2. Apply all text model edits in reverse document order (same as before)
  3. Single reverse pass through the diffs array: skip removed diffs, accumulate line delta, apply delta to remaining diffs

Complexity drops to O(N + K log K) with one array allocation instead of 2K.

Copilot AI review requested due to automatic review settings March 24, 2026 09:10
@vs-code-engineering vs-code-engineering bot added this to the 1.114.0 milestone Mar 24, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes TextModelDiffs.removeDiffs() in the merge editor model to avoid repeated full-array passes when removing multiple diffs, aiming to improve responsiveness when accepting/rejecting many conflicts.

Changes:

  • Replaces the per-diff filter + map loop with a single-pass rebuild of the diffs array.
  • Adds a pre-validation step to ensure all diffs-to-remove exist before applying edits.
  • Keeps bottom-up edit application to avoid range shifting during pushEditOperations.

@xingsy97 xingsy97 force-pushed the wt/merge-editor-removediffs-perf branch from cd1d14c to ab1d609 Compare March 24, 2026 09:24
@xingsy97 xingsy97 force-pushed the wt/merge-editor-removediffs-perf branch from ab1d609 to 6cfc3d5 Compare March 24, 2026 12:12
@xingsy97 xingsy97 marked this pull request as ready for review March 24, 2026 19:23
@hediet hediet enabled auto-merge (rebase) March 25, 2026 11:40
@hediet hediet merged commit 5096904 into microsoft:main Mar 25, 2026
18 checks passed
@xingsy97 xingsy97 deleted the wt/merge-editor-removediffs-perf branch March 31, 2026 06:19
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.

4 participants