RuntimeEnvironmentHelper: remove Lazy for basic platform checks#7485
Merged
Conversation
IsOSPlatform() is fast, so remove all the overhead from using Lazy<>.
This change addresses the review comment to keep the BSD check on
framework too - using OSPlatform.Create("FREEBSD") for all target
frameworks instead of conditionally compiling it.
Also added an early IsWindows check in IsMono to avoid the Mono type
check on Windows, improving performance.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
nkolev92
approved these changes
Jun 17, 2026
Nigusu-Allehu
approved these changes
Jun 17, 2026
This was referenced Jun 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Fixes: NuGet/Home#14951
Description
Thanks to @fowl2 for identifying this performance improvement. Given the PR is labeled as stale due to no activity and the performance improvements are good, we decided to make this change to the product.
Copied the below description from the community PR linked above.
Thanks to @nkolev92 for adding benchmarks showing performance improvements in #7416 (comment). I copied the benchmarks below for quick reference. Benchmarking showed measurable overhead in several platform-check scenarios, especially for Linux and Mono detection.
● 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% ✅ │
PR Checklist
[ ] Added tests