Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 14, 2025

Summary

This PR addresses issue #35: "Do we need to access number of physical CPU cores here?" by implementing proper physical CPU core detection for memory operations in the MemoryBlock class.

Key Changes

  • Added PhysicalCoreCount property: New public property that provides accurate physical CPU core detection across platforms
  • Platform-specific detection:
    • Windows: Uses WMI (Win32_Processor) to get actual physical core count
    • Linux: Parses /proc/cpuinfo to detect physical cores accurately
    • macOS: Uses sysctl -n hw.physicalcpu command
    • Fallback: Maintains backward compatibility using Environment.ProcessorCount / 2
  • Improved Zero method logic: Updated to use physical cores instead of the previous assumption-based approach
  • Better thread count calculation: Uses Math.Min(physicalCores, 2) for optimal memory bandwidth utilization
  • Comprehensive testing: Added tests to verify physical core detection works correctly

Why This Matters

The previous implementation used Environment.ProcessorCount / 2 and then hardcoded to 2 threads anyway. This improvement:

  1. More accurate: Gets actual physical cores instead of making hyper-threading assumptions
  2. Cross-platform: Works correctly on Windows, Linux, and macOS
  3. Memory-optimized: Still limits to 2 threads for dual-channel memory architecture optimization
  4. Backward compatible: Falls back to original behavior when platform detection fails

Technical Details

The solution recognizes that memory operations are bandwidth-limited rather than compute-limited. Therefore, the optimal thread count is min(physical_cores, memory_channels). Since most systems have dual-channel memory, we limit to 2 threads to avoid memory bandwidth contention.

Test Plan

  • All existing tests pass
  • New PhysicalCoreCountTest verifies core detection is reasonable
  • Memory zeroing functionality unchanged
  • Cross-platform compatibility maintained
  • Performance characteristics preserved

🤖 Generated with Claude Code


Resolves #35

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #35
@konard konard self-assigned this Sep 14, 2025
- Add PhysicalCoreCount property to MemoryBlock class with platform-specific detection
- Support Windows (WMI), Linux (/proc/cpuinfo), and macOS (sysctl) platforms
- Update Zero method to use physical cores instead of logical processors / 2
- Maintain backward compatibility with fallback to original approach
- Add comprehensive test coverage for physical core detection
- Improve thread count calculation for memory bandwidth optimization

Addresses issue #35: "Do we need to access number of physical CPU cores here?"

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Do we need to access number of physical CPU cores here? Implement physical CPU core detection for memory operations Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do we need to access number of physical CPU cores here?

2 participants