Skip to content

Commit

Permalink
perf: Improve startup time on bigger workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Jan 23, 2022
1 parent a25d965 commit e62935d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/utils/replaceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
escapeForRegExp,
} from './utils';
import { isInCodeSpan, isInFencedCodeBlock } from './externalUtils';
import { search } from './searchUtils';
import { search, refsToSearchRegExpStr } from './searchUtils';

type RenamedFile = {
readonly oldUri: Uri;
Expand Down Expand Up @@ -117,7 +117,10 @@ export const resolveRefsReplaceMap = async (
return {};
}

const fsPaths = await search([`[[${oldShortRef}]]`, `[[${oldLongRef}]]`], workspaceFolder);
const fsPaths = await search(
refsToSearchRegExpStr([`[[${oldShortRef}]]`, `[[${oldLongRef}]]`]),
workspaceFolder,
);

const searchUris = fsPaths.length
? newUris.filter(({ fsPath }) => fsPaths.includes(fsPath))
Expand Down
17 changes: 10 additions & 7 deletions src/utils/searchUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { search } from './searchUtils';
import { search, refsToSearchRegExpStr } from './searchUtils';
import {
closeEditorsAndCleanWorkspace,
createFile,
Expand All @@ -16,7 +16,7 @@ describe('search()', () => {

const uri = await createFile(`${name}.md`, '[[ref]]');

const [path] = await search(['[[ref]]'], getWorkspaceFolder());
const [path] = await search(refsToSearchRegExpStr(['[[ref]]']), getWorkspaceFolder());

expect(uri?.fsPath).toBe(path);
});
Expand All @@ -26,7 +26,7 @@ describe('search()', () => {

const uri = await createFile(`${name}.md`, '[[image.png]]');

const [path] = await search(['[[image.png]]'], getWorkspaceFolder());
const [path] = await search(refsToSearchRegExpStr(['[[image.png]]']), getWorkspaceFolder());

expect(uri?.fsPath).toBe(path);
});
Expand All @@ -36,7 +36,7 @@ describe('search()', () => {

const uri = await createFile(`${name}.md`, '![[image.png]]');

const [path] = await search(['[[image.png]]'], getWorkspaceFolder());
const [path] = await search(refsToSearchRegExpStr(['[[image.png]]']), getWorkspaceFolder());

expect(uri?.fsPath).toBe(path);
});
Expand All @@ -56,7 +56,7 @@ describe('search()', () => {

await createFile(`${name1}.md`);

const paths = await search(['[[ref]]'], getWorkspaceFolder());
const paths = await search(refsToSearchRegExpStr(['[[ref]]']), getWorkspaceFolder());

expect(paths.length).toBe(1);
expect(uri?.fsPath).toBe(paths[0]);
Expand All @@ -67,7 +67,7 @@ describe('search()', () => {

await createFile(`${name}.txt`, '[[ref]]');

const paths = await search(['[[ref]]'], getWorkspaceFolder());
const paths = await search(refsToSearchRegExpStr(['[[ref]]']), getWorkspaceFolder());

expect(paths.length).toBe(0);
});
Expand All @@ -78,7 +78,10 @@ describe('search()', () => {
await createFile(`${name}-1.md`, '[[ref0]]');
await createFile(`${name}-2.md`, '[[ref1]]');

const paths = await search(['[[ref0]]', '[[ref1]]'], getWorkspaceFolder());
const paths = await search(
refsToSearchRegExpStr(['[[ref0]]', '[[ref1]]']),
getWorkspaceFolder(),
);

expect(paths.length).toBe(2);
});
Expand Down
8 changes: 5 additions & 3 deletions src/utils/searchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ const findRipgrepPath = async (): Promise<string | undefined> => {
return;
};

export const search = async (refs: string[], dirPath: string = '.'): Promise<string[]> => {
export const refsToSearchRegExpStr = (refs: string[]) => `(${refs.map(escapeForRegExp).join('|')})`;

export const search = async (regExpStr: string, dirPath: string = '.'): Promise<string[]> => {
const ripgrepPath = await findRipgrepPath();

if (!ripgrepPath || !refs.length) {
if (!ripgrepPath || !regExpStr) {
return [];
}

Expand All @@ -60,7 +62,7 @@ export const search = async (refs: string[], dirPath: string = '.'): Promise<str
'never',
'-g',
`*.md`,
`(${refs.map(escapeForRegExp).join('|')})`,
regExpStr,
dirPath,
]);

Expand Down
12 changes: 9 additions & 3 deletions src/workspace/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ const utils = () => require('../../utils');
// private methods (not exported via workspace/index.ts and should not be used outside of workspace folder)

export const cacheRefs = async () => {
workspaceCache.danglingRefsByFsPath = await utils().findDanglingRefsByFsPath(
workspaceCache.markdownUris,
);
const { search, getWorkspaceFolder } = utils();

const fsPaths = await search('\\[\\[([^\\[\\]]+?)\\]\\]', getWorkspaceFolder()!);

const searchUris = fsPaths.length
? workspaceCache.markdownUris.filter(({ fsPath }) => fsPaths.includes(fsPath))
: workspaceCache.markdownUris;

workspaceCache.danglingRefsByFsPath = await utils().findDanglingRefsByFsPath(searchUris);
workspaceCache.danglingRefs = sortPaths(
Array.from(new Set(Object.values(workspaceCache.danglingRefsByFsPath).flatMap((refs) => refs))),
{ shallowFirst: true },
Expand Down

0 comments on commit e62935d

Please # to comment.