This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
Merge changes from TFS - #1335
Merged
Merged
Conversation
CR: JKotas, DavidWr
The generic ConditionalWeakTable class uses the non-generic DependentHandle struct to store GC handles for keys and values. Runtime type casts from objects back to TKey and TValue types are relatively expensive as they involve extracting the corresponding EETypePtr from the generic dictionary and calling the runtime casting routine. The main change here is skipping certainly safe type casts in ConditionalWeakTable code by using RuntimeHelpers.UncheckedCast intrinsic. One alternative approach would be making the DependentHandle struct generic; however, that would emit extra EETypes into SharedLibrary.dll.
Details:
* Move LockHolder.cs file from TypeLoader to Common and add it to System.Private.CoreLib assembly.
* Monitor.cs: Move ConditionalWeakTable and delegate initialization to the class constructor so that we do not need to check whether they have been initialized on each GetLock or GetCondition call.
* ClassConstructorRunner.cs:
- Use the LockHolder pattern to avoid potential reentrancy issues with Monitor..cctor after the change in Monitor.cs.
- Use raw EETypePtr equality when checking for the Lock type in the GetLock method to improve its performance. This check is temporary and will be removed after we fix Framework code to directly use Lock.Acquire/Release methods instead of the lock statement on Lock objects.
* ConditionalWeakTable.cs: Use the LockHolder pattern for update operations. Replace certainly safe type casts with RuntimeHelpers.UncheckedCast intrinsic calls.
With these changes I am observing ~28% throughput improvement for locking on normal types, and ~21% throughput improvement for locking on the special System.Threading.Lock type for x86ret build with SharedLibrary.dll. These numbers should be taken with a grain of salt as they depend on inlining decisions and binary code layout.
[tfs-changeset: 1608989]
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
No description provided.