C#, Environment.PhysicalCoreCount

Open
#111,173 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start by reviewing the existing Environment.ProcessorCount API and the parallel-loop behavior described in the issue. Define the cross-platform physical-core API and determine how heterogeneous P/E cores should affect scheduling; done requires an agreed scope and validation across the processor configurations named here.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Threading

This issue has been moved from a ticket on Developer Community.


Currently, Environment.ProcessorCount provides the total number of logical threads, which has been useful in processors leveraging technologies like SMT (Simultaneous Multithreading). However, this approach has limitations because, in SMT configurations, only one thread per core is truly executed simultaneously, while the second thread is only used when the first thread is idle. This can lead to worse performance in intensive parallel loops (such as those using native memory and pointers) when attempting to use all logical threads without effectively leveraging hardware resources.

Until now, dividing Environment.ProcessorCount by 2 was a common practice to estimate the number of physical cores. However, this solution is no longer valid with newer processors that do not implement SMT, such as some recent Intel and Apple models. In these cases, continuing to divide by 2 would result in a significant waste of resources by ignoring half of the available physical cores.

For this reason, it is crucial for C# to offer a native API to directly retrieve the number of physical cores, something like Environment.PhysicalCoreCount. This would avoid the current limitations and ensure a reliable, cross-platform, and native way to access this information without relying on operating system calls.

Additionally, it is important to revisit how C# parallel loops handle scenarios on heterogeneous processors, i.e., those combining high-performance cores (P-cores) and high-efficiency cores (E-cores), as seen in modern hybrid architectures. In such cases, the performance of parallel loops can degrade significantly if the unequal performance of the cores is not accounted for. A potential solution could involve dynamically adjusting the workload based on the relative performance of each core.

These improvements would not only benefit high-performance scenarios but also simplify the development of applications that need to optimally utilize underlying hardware in modern and heterogeneous configurations.


Original Comments
Feedback Bot on 12/2/2024, 09:22 PM:

Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. You will hear from us in about a week on our next steps.

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

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.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.