Uh oh!
There was an error while loading. Please reload this page.
Improve trimability of System.Diagnostics.Process by lazily initializing remote machine support - #126338
Conversation
…ate for remote machine code Use a lazily-initialized delegate (s_getRemoteProcessInfos) for PerformanceCounterLib-based remote process info retrieval. The delegate is only initialized when remote machine support is explicitly requested (via GetProcesses/GetProcessesByName/GetProcessById with a remote name), allowing NativeAOT/trimmer to eliminate PerformanceCounterLib and its dependencies for apps that only use Process.Start and other local-machine operations. Key changes: - Add s_getRemoteProcessInfos delegate and EnsureRemoteMachineFuncs() to ProcessManager.Windows.cs - Refactor IsProcessRunning(int) to not delegate to the 2-arg version (avoids including remote machine code path for local-only operations) - Change GetProcessInfos, GetProcessInfo, GetProcessName, GetProcessIds to use delegate for remote - Refactor GetProcessById(int) in Process.cs to not delegate to GetProcessById(int, string) Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/4e21bece-9420-4574-97e9-54e4e22dc1ec Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/4e21bece-9420-4574-97e9-54e4e22dc1ec Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
jkotas
commented
Mar 31, 2026
@copilot Can you measure how much code more code can be trimmed thanks to this change in a simple |
Per review feedback: calling EnsureRemoteMachineFuncs immediately followed by the s_getRemoteProcessInfos delegate adds overhead without trimability benefit. In GetProcessInfos and GetProcessIds (the remote machine entry points), call NtProcessManager.GetProcessInfos directly while still calling EnsureRemoteMachineFuncs to initialize s_getRemoteProcessInfos for later use by GetProcessInfo/GetProcessName (which benefit from the delegate for trimability since they are always reachable via the Process.ToString() virtual method). Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/ca005b69-3d61-4ca2-9a31-89403b539381 Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
I'm unable to produce actual size numbers from this Linux sandbox — NativeAOT Windows builds require a Windows environment with the ILCompiler toolchain. The change only affects Windows-specific code ( Based on static analysis, the code eliminated from a trimmed In
The entire
Transitively from To get exact byte deltas, you'd need to publish the test app as NativeAOT on Windows (e.g. |
This comment has been minimized.
This comment has been minimized.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…chine parameter Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/85b5145d-83e5-4298-b4c1-025c0dd77d09 Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
…SunOS CreateProcessInfo Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/8621c930-4bf4-4381-bec7-2541991cf40c Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Uh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
Uh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
jkotas
commented
Apr 3, 2026
💡 Minor: Extra blank lines Process.Windows.cs line ~509: double blank line between GetStandardOutputEncoding and StartCore 💡 Minor: Inconsistent null-check style |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/e881e4f5-7fee-4cef-bfa7-561466495b2a Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
... Done in 37d8c56. Removed both extra blank lines (Process.Windows.cs between |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…s/ProcessManager.Windows.cs Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Uh oh!
There was an error while loading. Please reload this page.
🤖 Copilot Code Review — PR #126338Note This review was AI/Copilot-generated. Holistic AssessmentMotivation: Justified. Improving trimability of Approach: Sound. Using a lazily-initialized delegate ( Summary: ✅ LGTM. The refactoring is correct, well-motivated, and preserves existing behavior across all platforms. No blocking issues found. All code paths were verified for correctness, argument validation preservation, and behavioral equivalence. Detailed Findings✅ Delegate safety — |
jkotas
commented
Apr 3, 2026
In ProcessManager.Windows.cs:51-52, the foreach body is a single statement without braces: @copilot Address this feedback |
…ions Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/57e910b4-e9dc-4d58-be98-3307b7bb2fb6 Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
…ach braces; fix double blank lines Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d5325063-0131-4c3c-a241-a0a4e3d178dc Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Tagging subscribers to 'size-reduction': @eerhardt, @SamMonoRT |
A NativeAOT app doing only Process.Start("notepad.exe").WaitForExit() pulled in PerformanceCounterLib → PerformanceMonitor → RegistryKey.OpenRemoteBaseKey due to reachability chains through Process.ToString(), ProcessManager.OpenProcess, GetProcesses(), and GetProcessesByName(). This PR breaks all these chains using the lazily-initialized delegate technique and local-only entry points. It also restores the name-filter efficiency for GetProcessesByName on all platforms.
Win-x64 NAOT app that does Process.Start("notepad.exe").WaitForExit(); goes from 1550kB to 1210kB (-22%, 340kB code size reduction).
Contributes to #126332