Skip to content

Commit

Permalink
Optim: Find SRC_URI files only once
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
deribaucourt committed Dec 11, 2023
1 parent 43d6b64 commit 2364e83
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions client/src/documentLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.Uri[]> {
/* 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<vscode.DocumentLink[]> {
const linksData = await this.client.sendRequest<RequestResult['getLinksInDocument']>(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(
Expand All @@ -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)))
}
Expand Down

0 comments on commit 2364e83

Please sign in to comment.