microsoft / sarif-vscode-extension

SARIF Microsoft Visual Studio Code extension
MIT License
107 stars 48 forks source link

Address CPU usage spike: Perform case-insensitive search on win32 and added workspace.findFiles maxResults value #556

Closed chrishuynhc closed 3 months ago

chrishuynhc commented 4 months ago

Overview

This change addresses the high CPU usage issue caused by the SARIF Viewer mentioned here: https://github.com/microsoft/sarif-vscode-extension/issues/548

For context, the high CPU usage was caused by the spawning of rg.exe (rigrep) processes to perform VSCode's workspace.findFiles searches. This file search is triggered on text document changes and is used for the uri rebaser logic of matching workspace files to SARIF log artifacts.

The Issue/Fix

The issue was due to the vscode.workspace.findFiles method being case-sensitive while the path being searched is normalized to lowercase on Windows. This was triggered on every text document change/keystroke resulting in the numerous rg.exe spawns. So, if the user happens to have a file open that contains capitalization in the file name, such as fooBar.ts, the file search would look for foobar.ts (normalized to lowercase on Windows) and vscode.workspace.findFiles would result in 0 matches. As a result, this file search would repeat as fooBar.ts would never be found and cached resulting in repetitive calls to vscode.workspace.findFiles when trying to resolve artifact matches and fill the distinct files cache.

To address this, we now perform a case-insensitive file search if on a Windows platform, otherwise do the default case-sensitive search when determining if a workspace has a distinct file name. In addition, this issue is compounded on larger workspace which is why a maxLimit of 5000 files is now specified when utilizing vscode.workspace.findFiles. This helps to prevent perf issues when working in large workspaces such as the windows OS repo by stopping the search when the limit is hit.

Note: Tests were also updated to reflect this