Skip to content

Process.Linux/OSX: avoid loading full ProcessInfo for ProcessName and ToString.#126449

Open
tmds wants to merge 2 commits intodotnet:mainfrom
tmds:processname_linux
Open

Process.Linux/OSX: avoid loading full ProcessInfo for ProcessName and ToString.#126449
tmds wants to merge 2 commits intodotnet:mainfrom
tmds:processname_linux

Conversation

@tmds
Copy link
Copy Markdown
Member

@tmds tmds commented Apr 2, 2026

This speeds up ProcessName/ToString.

It also enables trimming out ProcessInfo support when no APIs that require ProcessInfo are used.

@tmds tmds requested review from adamsitnik and jkotas April 2, 2026 05:46
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 2, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 2, 2026

Do you have any AOT binary size numbers for how much this saves (e.g. for an all that does just Process.Start("echo").WaitForExit();)

@tmds
Copy link
Copy Markdown
Member Author

tmds commented Apr 2, 2026

This speeds up ProcessName/ToString.

using System.Diagnostics;

var process = Process.GetCurrentProcess();
var sw = Stopwatch.StartNew();
long iterations = 0;

while (sw.ElapsedMilliseconds < 5000)
{
    _ = process.ProcessName;
    process.Refresh();
    iterations++;
}

sw.Stop();

Console.WriteLine($"Iterations: {iterations}");
Console.WriteLine($"Elapsed: {sw.Elapsed}");
Console.WriteLine($"Per call: {sw.Elapsed.TotalNanoseconds / iterations:F1} ns");

Results on Linux:

.NET 10:
Iterations: 103812
Elapsed: 00:00:05.0000314
Per call: 48164.3 ns

.NET 11:
Iterations: 760767
Elapsed: 00:00:05.0000080
Per call: 6572.3 ns

That is about a 7x speedup.

It also enables trimming out ProcessInfo support when no APIs that require ProcessInfo are used.

On Linux, it trims out about 4.5KB by eliminating the ToString dependency on ProcessInfo. A minor improvement.

@tmds
Copy link
Copy Markdown
Member Author

tmds commented Apr 2, 2026

Do you have any AOT binary size numbers for how much this saves (e.g. for an all that does just Process.Start("echo").WaitForExit();)

I'll do an exact measurement.

The speed-up turned out to be the actual improvement.
And with that, moving HaveProcessInfo from EnsureState to a separate method made it trimmable too.

@tmds
Copy link
Copy Markdown
Member Author

tmds commented Apr 2, 2026

Process.Start("echo").WaitForExit(); is 8.2KB smaller.

$ stat -c %s /tmp/nativeaot-out1/nativeaot-test /tmp/nativeaot-out2/nativeaot-test
2163504
2171776

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Process community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants