Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/core/diff/strategies/multi-file-search-replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function getSimilarity(original: string, search: string): number {
/**
* Performs a "middle-out" search of `lines` (between [startIndex, endIndex]) to find
* the slice that is most similar to `searchChunk`. Returns the best score, index, and matched text.
*
* Performance optimization: Returns immediately when a perfect match (similarity === 1) is found,
* avoiding unnecessary Levenshtein distance calculations which are O(m*n) per comparison.
*/
function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, endIndex: number) {
let bestScore = 0
Expand All @@ -54,6 +57,10 @@ function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, e
bestScore = similarity
bestMatchIndex = leftIndex
bestMatchContent = originalChunk
// Early termination: perfect match found, no need to continue searching
if (similarity === 1) {
return { bestScore, bestMatchIndex, bestMatchContent }
}
}
leftIndex--
}
Expand All @@ -66,6 +73,10 @@ function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, e
bestScore = similarity
bestMatchIndex = rightIndex
bestMatchContent = originalChunk
// Early termination: perfect match found, no need to continue searching
if (similarity === 1) {
return { bestScore, bestMatchIndex, bestMatchContent }
}
}
rightIndex++
}
Expand Down
11 changes: 11 additions & 0 deletions src/core/diff/strategies/multi-search-replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function getSimilarity(original: string, search: string): number {
/**
* Performs a "middle-out" search of `lines` (between [startIndex, endIndex]) to find
* the slice that is most similar to `searchChunk`. Returns the best score, index, and matched text.
*
* Performance optimization: Returns immediately when a perfect match (similarity === 1) is found,
* avoiding unnecessary Levenshtein distance calculations which are O(m*n) per comparison.
*/
function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, endIndex: number) {
let bestScore = 0
Expand All @@ -55,6 +58,10 @@ function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, e
bestScore = similarity
bestMatchIndex = leftIndex
bestMatchContent = originalChunk
// Early termination: perfect match found, no need to continue searching
if (similarity === 1) {
return { bestScore, bestMatchIndex, bestMatchContent }
}
}
leftIndex--
}
Expand All @@ -66,6 +73,10 @@ function fuzzySearch(lines: string[], searchChunk: string, startIndex: number, e
bestScore = similarity
bestMatchIndex = rightIndex
bestMatchContent = originalChunk
// Early termination: perfect match found, no need to continue searching
if (similarity === 1) {
return { bestScore, bestMatchIndex, bestMatchContent }
}
}
rightIndex++
}
Expand Down
Loading