microsoft / microsoft/vscode-cpptools

Constant CPU usage from cpptools.exe scanning workspace

Open
#13,546 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Language Service more info needed
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

On Windows, simply having VS Code open on a folder with a C++ project causes the cpptools.exe to almost constantly use some CPU. It doesn't use a full CPU core, but enough to occasionally make my laptop fan spin up.

The offending stack trace starts with

msvc::recursive_directory_iterator_utf8::enumerate_files_non_recursive
msvc::recursive_directory_iterator_utf8::recursive_directory_iterator_utf8
browse_engine::query_include_path_suggestions_impl
browse_engine::parse_file
browse_engine::parse_root
cpptools_context::parse_root
std::_Func_impl_no_alloc<`cpptools_context::cpptools_context'::`1'::<lambda_10_>,void,cpptools_context::browse_engine_update_action_and_task_token &&>::_Do_call
`msvc::deque_thread<cpptools_context::browse_engine_update_action_and_task_token>::defer'::`2'::<lambda_1>::operator()
msvc::thread_manager_t::do_work
msvc::thread_helper_t::thread_entry
thread_start<unsigned int (__cdecl*)(void *),1>

from there, it calls FindNextFileW() to iterate over the contents of the directory, and GetFileAttributesExW() presumably to get the modification time for each file. (Without having access to the cpptools executable I'm having to guess at why these calls are made).

The calls to GetFileAttributesExW end up taking a disproportionate amount of time, and I don't think they're necessary.

The FindNextFile function already returns attributes on each file, so GetFileAttributesEx() seems redundant. Alternatively, this could all be replaced with a call to GetFileInformationByHandleEx(), and both approaches would be over an order of magnitude faster. See the following blog post which documents optimizing pretty much the exact same use case:

https://www.wholetomato.com/blog/2024/11/14/how-to-query-file-attributes-50x-faster-on-windows/

I'd love to submit a PR myself, but again, I don't have access to the cpptools executable's source code.

I hope you can take a look at this. Even if the extension only uses a few percent CPU, doing so constantly on every machine it's running on adds up to a lot of wasted time and energy, if it can be trivially sped up.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation behind msvc::recursive_directory_iterator_utf8 and browse_engine::query_include_path_suggestions_impl, then trace its use of FindNextFileW and GetFileAttributesExW. Compare the existing directory scan with the suggested Windows APIs and confirm that the change reduces repeated file-attribute work and constant CPU usage on a Windows C++ workspace.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, typescript
Domain
performance, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.