Skip to content

Commit 0e9b815

Browse files
author
Andy
authored
Improve performance of duplicate check (#23516)
1 parent b0d6896 commit 0e9b815

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/harness/fourslash.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,14 +2103,11 @@ Actual: ${stringify(fullActual)}`);
21032103
this.raiseError("verifyRangesInImplementationList failed - expected to find at least one implementation location but got 0");
21042104
}
21052105

2106-
for (let i = 0; i < implementations.length; i++) {
2107-
for (let j = 0; j < implementations.length; j++) {
2108-
if (i !== j && implementationsAreEqual(implementations[i], implementations[j])) {
2109-
const { textSpan, fileName } = implementations[i];
2110-
const end = textSpan.start + textSpan.length;
2111-
this.raiseError(`Duplicate implementations returned for range (${textSpan.start}, ${end}) in ${fileName}`);
2112-
}
2113-
}
2106+
const duplicate = findDuplicatedElement(implementations, implementationsAreEqual);
2107+
if (duplicate) {
2108+
const { textSpan, fileName } = duplicate;
2109+
const end = textSpan.start + textSpan.length;
2110+
this.raiseError(`Duplicate implementations returned for range (${textSpan.start}, ${end}) in ${fileName}`);
21142111
}
21152112

21162113
const ranges = this.getRanges();
@@ -3756,6 +3753,16 @@ ${code}
37563753
function stripWhitespace(s: string): string {
37573754
return s.replace(/\s/g, "");
37583755
}
3756+
3757+
function findDuplicatedElement<T>(a: ReadonlyArray<T>, equal: (a: T, b: T) => boolean): T {
3758+
for (let i = 0; i < a.length; i++) {
3759+
for (let j = i + 1; j < a.length; j++) {
3760+
if (equal(a[i], a[j])) {
3761+
return a[i];
3762+
}
3763+
}
3764+
}
3765+
}
37593766
}
37603767

37613768
namespace FourSlashInterface {

0 commit comments

Comments
 (0)