Skip to content

Commit

Permalink
perf: cache URI.file, URI.parse results
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 30, 2021
1 parent 0c226e2 commit 83f1447
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions packages/shared/src/uriMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ interface Options<T> {

export function createPathMap<T>(map: Options<T> = new Map<string, T>()) {

const uriToUriKeys: Record<string, string> = {};
const fsPathToUriKeys: Record<string, string> = {};

return {
clear,
values,
Expand All @@ -25,6 +28,17 @@ export function createPathMap<T>(map: Options<T> = new Map<string, T>()) {
fsPathSet,
};

function getUriByUri(uri: string) {
if (uriToUriKeys[uri] === undefined)
uriToUriKeys[uri] = normalizeUri(uri).toLowerCase();
return uriToUriKeys[uri];
}
function getUriByFsPath(fsPath: string) {
if (fsPathToUriKeys[fsPath] === undefined)
fsPathToUriKeys[fsPath] = fsPathToUri(fsPath).toLowerCase();
return fsPathToUriKeys[fsPath];
}

function clear() {
return map.clear();
}
Expand All @@ -33,29 +47,29 @@ export function createPathMap<T>(map: Options<T> = new Map<string, T>()) {
}

function uriDelete(_uri: string) {
return map.delete(normalizeUri(_uri).toLowerCase());
return map.delete(getUriByUri(_uri));
}
function uriGet(_uri: string) {
return map.get(normalizeUri(_uri).toLowerCase());
return map.get(getUriByUri(_uri));
}
function uriHas(_uri: string) {
return map.has(normalizeUri(_uri).toLowerCase());
return map.has(getUriByUri(_uri));
}
function uriSet(_uri: string, item: T) {
return map.set(normalizeUri(_uri).toLowerCase(), item);
return map.set(getUriByUri(_uri), item);
}

function fsPathDelete(_fsPath: string) {
return map.delete(fsPathToUri(_fsPath).toLowerCase());
return map.delete(getUriByFsPath(_fsPath));
}
function fsPathGet(_fsPath: string) {
return map.get(fsPathToUri(_fsPath).toLowerCase());
return map.get(getUriByFsPath(_fsPath));
}
function fsPathHas(_fsPath: string) {
return map.has(fsPathToUri(_fsPath).toLowerCase());
return map.has(getUriByFsPath(_fsPath));
}
function fsPathSet(_fsPath: string, item: T) {
return map.set(fsPathToUri(_fsPath).toLowerCase(), item);
return map.set(getUriByFsPath(_fsPath), item);
}
}

Expand Down

1 comment on commit 83f1447

@IWANABETHATGUY
Copy link
Contributor

Choose a reason for hiding this comment

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

image
found that upath.toUnix is used a lot, but actually in macos and linux there is no need to use such function, maybe we use upath.toUnix on demand.So that
we could perf a bit in *nix os.

Please sign in to comment.