From 82346e098e49702200213929331098cb7718fd82 Mon Sep 17 00:00:00 2001 From: Enguerrand de Ribaucourt Date: Fri, 8 Dec 2023 09:56:58 +0100 Subject: [PATCH] Optim: Find SRC_URI files only once Running findFiles() creates a ripgrep process for each URL which could saturate the CPU for tens of seconds. This patch optimizes the findRelatedFiles() function to only run findFiles() once and then filter the results. --- client/src/documentLinkProvider.ts | 43 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/client/src/documentLinkProvider.ts b/client/src/documentLinkProvider.ts index 2d5a99f1a..83f3de018 100644 --- a/client/src/documentLinkProvider.ts +++ b/client/src/documentLinkProvider.ts @@ -17,9 +17,34 @@ export class BitbakeDocumentLinkProvider implements vscode.DocumentLinkProvider this.client = client } + private async findRelatedFiles (document: vscode.TextDocument, linksData: Array<{ value: string, range: vscode.Range }>, token: vscode.CancellationToken): Promise { + /* Corresponding file:// URI usually point to files in ${PN}/ or files/. Ex: + * recipes-core + * ├── busybox + * │ └── defconfig + * ├── busybox_1.36.1.bb + * ├── busybox.inc + * └── files + * └── syslog-startup.conf + */ + const filenames = linksData.map(link => link.value.split(';')[0]) + const filenamesRegex = '{' + filenames.join(',') + '}' + const parentDir = document.uri.path.split('/').slice(0, -1).join('/') + const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri) + const pnDir = path.join(parentDir, extractRecipeName(document.uri.path) as string) + const pnDirRelative = pnDir.replace(workspaceFolder?.uri.path + '/', '') + const filesDir = path.join(parentDir, 'files') + const filesDirRelative = filesDir.replace(workspaceFolder?.uri.path + '/', '') + return [...(await vscode.workspace.findFiles(pnDirRelative + '/**/' + filenamesRegex, undefined, filenames.length, token)), + ...(await vscode.workspace.findFiles(filesDirRelative + '/**/' + filenamesRegex, undefined, filenames.length, token))] + } + async provideDocumentLinks (document: vscode.TextDocument, token: vscode.CancellationToken): Promise { const linksData = await this.client.sendRequest(RequestMethod.getLinksInDocument, { documentUri: document.uri.toString() }) const documentLinks: vscode.DocumentLink[] = [] + const relatedFiles = await this.findRelatedFiles(document, linksData, token) + logger.debug(`Found ${relatedFiles.length} local SRC_URI files for ${document.uri.toString()}`) + for (let i = 0; i < linksData.length; i++) { const link = linksData[i] const range = new vscode.Range( @@ -29,25 +54,9 @@ export class BitbakeDocumentLinkProvider implements vscode.DocumentLinkProvider link.range.end.character ) try { - /* Corresponding file:// URI usually point to files in ${PN}/ or files/. Ex: - * recipes-core - * ├── busybox - * │ └── defconfig - * ├── busybox_1.36.1.bb - * ├── busybox.inc - * └── files - * └── syslog-startup.conf - */ - const parentDir = document.uri.path.split('/').slice(0, -1).join('/') - const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri) - const pnDir = path.join(parentDir, extractRecipeName(document.uri.path) as string) - const pnDirRelative = pnDir.replace(workspaceFolder?.uri.path + '/', '') - const filesDir = path.join(parentDir, 'files') - const filesDirRelative = filesDir.replace(workspaceFolder?.uri.path + '/', '') const filename = link.value.split(';')[0] - const file = (await vscode.workspace.findFiles(pnDirRelative + '/**/' + filename, undefined, 1))[0] ?? - (await vscode.workspace.findFiles(filesDirRelative + '/**/' + filename, undefined, 1))[0] + const file = relatedFiles.find(file => file.path.endsWith(filename)) if (file !== undefined) { documentLinks.push(new vscode.DocumentLink(range, vscode.Uri.parse(file.fsPath))) }