Skip to content

Latest commit

 

History

History
61 lines (48 loc) · 1.95 KB

File metadata and controls

61 lines (48 loc) · 1.95 KB

Research: Physical CPU Core Detection in .NET 8

Current Approach

  • Environment.ProcessorCount / 2 - assumes hyper-threading and divides by 2
  • Comment explains: "A way to prevent wasting resources due to Hyper-Threading"

Problem

  • Environment.ProcessorCount returns logical processors, not physical cores
  • Dividing by 2 is an assumption that may not always be correct:
    • Some CPUs don't support hyper-threading
    • Some systems have hyper-threading disabled
    • Modern CPUs may have different hyper-threading ratios

Possible Solutions

Option 1: Keep current approach (simplest)

Pros:

  • Cross-platform compatible
  • Simple and fast
  • Works reasonably well for most cases
  • No additional dependencies

Cons:

  • Not accurate on all systems
  • Makes assumptions about hyper-threading

Option 2: Platform-specific detection

Pros:

  • More accurate
  • Can handle different CPU architectures

Cons:

  • Complex implementation
  • Platform-specific code
  • Requires additional dependencies (WMI on Windows)
  • May not work in all environments (containers, etc.)

Option 3: Hybrid approach with fallback

Pros:

  • More accurate when possible
  • Falls back to current approach
  • Gradual improvement

Cons:

  • More complex
  • Still requires platform-specific code

Analysis of Current Usage

The code is used for memory zeroing operations where:

  • Memory bandwidth is the bottleneck (not CPU)
  • Too many threads can actually hurt performance
  • The goal is to avoid over-subscribing memory channels

Recommendation

For this specific use case (memory operations), the current approach might actually be better than true physical core detection because:

  1. Memory operations are bandwidth-limited, not compute-limited
  2. The comment mentions "two-channel memory architecture is the most available type"
  3. The code hardcodes to 2 threads anyway: threads = 2;

The real question is whether we need physical core detection or if the current approach is actually optimal for memory operations.