Skip to content

RuntimeEnvironmentHelper: remove Lazy<> for basic platform checks#7416

Open
fowl2 wants to merge 1 commit into
NuGet:devfrom
fowl2:runtimeEnvironmentHelper
Open

RuntimeEnvironmentHelper: remove Lazy<> for basic platform checks#7416
fowl2 wants to merge 1 commit into
NuGet:devfrom
fowl2:runtimeEnvironmentHelper

Conversation

@fowl2
Copy link
Copy Markdown

@fowl2 fowl2 commented May 26, 2026

IsOSPlatform() is fast, so remove all the overhead from using a bunch of Lazy<>.

IsLinux also returns true on BSD, not sure if that's documented, but switched to conditional compilation to remove an allocation.

I'm not sure if Mono is still supported - if it's not, ripping all that code out might be a good follow up.

@fowl2 fowl2 requested a review from a team as a code owner May 26, 2026 11:59
@fowl2 fowl2 requested review from martinrrm and nkolev92 May 26, 2026 11:59
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label May 26, 2026
@donnie-msft donnie-msft self-assigned this May 26, 2026
Copy link
Copy Markdown
Member

@nkolev92 nkolev92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the BSD check on framework too.

}
#endif

return false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just add back the FreeBSD detection.

While mono is technically out of support, I don't want to break anything accidentally.

{
return System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
}
=> System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's faster assuming this was called once, but we actually call these methods many many times, for example:

public static bool IsDirectorySeparatorChar(char ch)
{
if (RuntimeEnvironmentHelper.IsWindows)
{
// Windows has both '/' and '\' as valid directory separators.
return (ch == Path.DirectorySeparatorChar ||
ch == Path.AltDirectorySeparatorChar);
}
else
{
return ch == Path.DirectorySeparatorChar;
}
}

I ran the benchmarks:

● Benchmark results (10,000 iterations per call, on Hyper-V):

┌───────────────┬─────────────────┬───────────────────┬─────────────┐
│ Method │ Baseline (Lazy) │ Proposed (direct) │ Δ │
├───────────────┼─────────────────┼───────────────────┼─────────────┤
│ IsWindows │ 8.59 µs │ 6.74 µs │ -22% ✅ │
├───────────────┼─────────────────┼───────────────────┼─────────────┤
│ IsMacOSX │ 6.61 µs │ 8.14 µs │ +23% ❌ │
├───────────────┼─────────────────┼───────────────────┼─────────────┤
│ IsLinux │ 6.56 µs │ 3.93 µs │ -40% ✅ │
├───────────────┼─────────────────┼───────────────────┼─────────────┤
│ IsMono │ 11.53 µs │ 3.91 µs │ -66% ✅ │

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really add a summary here :D

This is me to say the change is good, but I think it would've been great to see this type of benchmark in the PR body to begin with.

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

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants