Repository navigation
Replace ComWrappers-based IWeakReference with a thin native object - #2668
Merged
Sergio Pedri (Sergio0694) merged 13 commits intoNov 25, 2026
Merged
Sergio Pedri (Sergio0694) merged 13 commits into
Sergio Pedri (Sergio0694) merged 13 commits into
Conversation
ManagedWeakReference was a [GeneratedComClass], so every IWeakReferenceSource::GetWeakReference call went through StrategyBasedComWrappers. Since it only ever holds a weak handle (no reference cycles, no tracker support needed), replace it with a thin native object (vtable + refcount + raw WeakGCHandle<object>), following the DispatcherQueueProxyHandler pattern. Also add unit tests for the native weak reference and a standalone BenchmarkDotNet project comparing the old and new implementations. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sergio Pedri (Sergio0694)
requested a review
from Manodasan Wignarajah (manodasanW)
November 23, 2026 19:27
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sergio Pedri (Sergio0694)
marked this pull request as ready for review
November 23, 2026 21:38
Member
Author
|
Note: also needed to work correctly with dotnet/runtime#138109. |
| weakReference = null; | ||
| // A CCW existed when this weak reference was created, but it might not exist anymore: native callers can release all their references | ||
| // to it and only keep this weak reference, while the target stays alive from the managed side. In that case a new CCW has to be created. | ||
| // There is no public API to only look up an existing CCW, so we always go through 'ComWrappers', which returns the existing one if any. |
There was a problem hiding this comment.
The public API is essentially GetOrCreateComInterfaceForObject.
Member
Author
There was a problem hiding this comment.
Not sure I understand what do you mean here, should I change anything? 😅
There was a problem hiding this comment.
that was a reference to the comment that there is no public API.
… objects Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The existing ReflectionPerf benchmarks (GetWeakReferenceOfManagedObject and GetAndResolveWeakReferenceOfManagedObject) already exercise this path. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Manodasan Wignarajah (manodasanW)
approved these changes
Nov 25, 2026
Sergio Pedri (Sergio0694)
enabled auto-merge (squash)
November 25, 2026 00:24
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sergio Pedri (Sergio0694)
deleted the
sergio0694-custom-weak-reference-no-comwrappers
branch
November 25, 2026 05:18
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Replace the
ComWrappers-based internalManagedWeakReference/IWeakReferenceimplementation with a thin native object, removing the dependency onStrategyBasedComWrappersforIWeakReferenceSource::GetWeakReference. Behavior is unchanged.Motivation
ManagedWeakReferencewas a[GeneratedComClass], so everyGetWeakReferencecall on a managed CCW allocated a managed object and marshalled it throughStrategyBasedComWrappers. The object only ever holds a weak handle to its target, so it can never be part of a reference cycle and doesn't need tracker support. A plain native object with a small vtable (the same pattern asDispatcherQueueProxyHandler) is enough, and is much cheaper to create.Changes
src/WinRT.Runtime/InteropServices/WeakReferences/ManagedWeakReference.cs: now a native struct (vtable pointer, rawWeakGCHandle<object>asnint, reference count) allocated withNativeMemory, withQueryInterface(IUnknownandIWeakReferenceonly, as before),AddRef,ReleaseandResolve.src/WinRT.Runtime/InteropServices/ProjectionImpls/IWeakReferenceImpl.cs: newIWeakReferenceImplwith the static vtable and[UnmanagedCallersOnly]thunks.src/WinRT.Runtime/InteropServices/ProjectionImpls/IWeakReferenceSourceImpl.cs:GetWeakReferencecreates the native object directly instead of going throughComInterfaceMarshaller<IWeakReference>.src/WinRT.Runtime/InteropServices/WeakReferences/IWeakReference.cs: removed (the[GeneratedComInterface]is no longer needed).src/Tests/UnitTest/WeakReferenceTests.cs: new tests for the native weak reference (QueryInterface, reference counting,Resolvefor live and collected targets).Performance
Measured with a throwaway BenchmarkDotNet microbenchmark comparing the old and new implementations (old to new, lower is better). The existing
ReflectionPerfbenchmarks (GetWeakReferenceOfManagedObjectandGetAndResolveWeakReferenceOfManagedObject) already exercise this path, so they can be used to track it going forward:Resolve(target alive)Resolve(target collected)QueryInterfaceAddRef+ReleaseCreation is about 40x (CoreCLR) and 70x (Native AOT) faster and allocation-free.
AddRef,ReleaseandQueryInterfaceare slower on CoreCLR, because the old CCW used the runtime's nativeIUnknownwhile these are now managedUnmanagedCallersOnlythunks. They run a few times per weak reference, so the creation savings outweigh this.Notes
ResolvereturnsE_POINTERfor a null output pointer, where the old code would have failed inside its generated stub.