Skip to content

Remove Unsafe.As from ThreadInt64PersistentCounter by using strongly-typed ThreadLocalNode - #121434

Merged
agocke merged 4 commits into
mainfrom
copilot/remove-unsafe-uses-in-thread-counter
Nov 18, 2025
Merged

agocke merged 4 commits into
mainfrom
copilot/remove-unsafe-uses-in-thread-counter

Conversation

Copilot AI commented Nov 7, 2025 •

Copy link
Copy Markdown
Contributor

The goal here is to remove some uses of Unsafe.As in ThreadInt64PersistentCounter. All of these uses were due to the type of the ThreadLocalNode being private and the return type being object. By making the type internal we can remove the need for casts entirely.

…alNode

Replace object-based API with strongly-typed ThreadLocalNode in ThreadInt64PersistentCounter, removing all Unsafe.As casts. This change propagates through all ThreadPool implementations (Windows, Unix, Portable, Browser, WASI).

Changes:
- Make ThreadLocalNode internal sealed (was private sealed)
- Update Increment/Decrement/Add methods to accept ThreadLocalNode directly
- Change CreateThreadLocalCountObject() return type to ThreadLocalNode
- Update all call sites to use typed node instead of object
- Remove Debug.Assert and Unsafe.As usage from static methods

This improves code clarity, type safety, and should provide minor performance improvements by eliminating type checks and casts.

Co-authored-by: agocke <[email protected]>
Copilot AI changed the title [WIP] Remove unnecessary uses of Unsafe.As in ThreadInt64PersistentCounter Remove Unsafe.As from ThreadInt64PersistentCounter by using strongly-typed ThreadLocalNode Nov 7, 2025
Copilot AI requested a review from agocke November 7, 2025 03:57
Remove Increment/Decrement/Add static methods and call instance methods directly at all call sites. This simplifies the API since the static methods were just pass-through wrappers.

Co-authored-by: agocke <[email protected]>
Copilot AI requested a review from agocke November 7, 2025 04:28
@agocke
agocke marked this pull request as ready for review November 7, 2025 05:26
Copilot AI review requested due to automatic review settings November 7, 2025 05:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the thread pool's completion counter implementation to use strongly-typed ThreadInt64PersistentCounter.ThreadLocalNode references instead of weakly-typed object? references, eliminating unnecessary boxing and unsafe casts.

  • Removed static helper methods (Increment, Decrement, Add) from ThreadInt64PersistentCounter that performed unsafe casts
  • Changed ThreadLocalNode visibility from private to internal to enable direct usage
  • Updated all thread pool implementations across platforms to use strongly-typed references

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadInt64PersistentCounter.cs Removed static helper methods that took object parameters and unsafe casts; changed ThreadLocalNode to internal; updated return type of CreateThreadLocalCountObject()
src/libraries/System.Private.CoreLib/src/System/Threading/WindowsThreadPool.cs Updated field, method parameters, and return types to use ThreadLocalNode instead of object?; replaced static helper calls with instance method calls
src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.cs Updated field, method parameters, and return types to use ThreadLocalNode instead of object?; replaced static helper calls with instance method calls
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs Updated local variables, field types, and method calls to use ThreadLocalNode and call instance methods directly
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPool.Windows.cs Updated method signatures and conditional forwarding to use strongly-typed ThreadLocalNode
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPool.Unix.cs Updated method signatures and forwarding to use strongly-typed ThreadLocalNode
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPool.Wasi.cs Updated method signatures in stub implementation to use strongly-typed ThreadLocalNode?
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPool.Browser.cs Updated method signatures in stub implementation to use strongly-typed ThreadLocalNode?

@agocke
agocke merged commit a66cb73 into main Nov 18, 2025
@agocke
agocke deleted the copilot/remove-unsafe-uses-in-thread-counter branch November 18, 2025 18:27
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants