Skip to content

Commit 74ec0f5

Browse files
authored
Optimize findClosestMatch complexity (#11263)
1 parent 66456ea commit 74ec0f5

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

packages/pyright-internal/src/readonlyAugmentedFileSystem.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,19 @@ export class ReadOnlyAugmentedFileSystem implements FileSystem {
215215
private _findClosestMatch(uri: Uri, map: UriMap<MappedEntry>): MappedEntry | undefined {
216216
// Search through the map of directories to find the closest match. The
217217
// closest match is the longest path that is a parent of the uri.
218-
let entry = map.get(uri);
219-
if (!entry) {
220-
let foundKey = undefined;
221-
for (const [key, value] of map.entries()) {
222-
if (uri.isChild(key)) {
223-
// Update the found key if it is a better match.
224-
if (!foundKey || foundKey.getPathLength() < key.getPathLength()) {
225-
foundKey = key;
226-
entry = value;
227-
}
228-
}
218+
while (true) {
219+
const entry = map.get(uri);
220+
if (entry) {
221+
return entry;
222+
}
223+
224+
const parent = uri.getDirectory();
225+
if (parent.equals(uri)) {
226+
return undefined;
229227
}
228+
229+
uri = parent;
230230
}
231-
return entry;
232231
}
233232

234233
private _getOriginalEntry(uri: Uri): MappedEntry | undefined {

0 commit comments

Comments
 (0)