Skip to content

Replace ComWrappers-based IWeakReference with a thin native object - #2668

Merged
Sergio Pedri (Sergio0694) merged 13 commits into
staging/3.0from
sergio0694-custom-weak-reference-no-comwrappers
Nov 25, 2026
Merged

Sergio Pedri (Sergio0694) merged 13 commits into
staging/3.0from
sergio0694-custom-weak-reference-no-comwrappers

Conversation

@Sergio0694

@Sergio0694 Sergio Pedri (Sergio0694) commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Replace the ComWrappers-based internal ManagedWeakReference / IWeakReference implementation with a thin native object, removing the dependency on StrategyBasedComWrappers for IWeakReferenceSource::GetWeakReference. Behavior is unchanged.

Motivation

ManagedWeakReference was a [GeneratedComClass], so every GetWeakReference call on a managed CCW allocated a managed object and marshalled it through StrategyBasedComWrappers. 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 as DispatcherQueueProxyHandler) is enough, and is much cheaper to create.

Changes

  • src/WinRT.Runtime/InteropServices/WeakReferences/ManagedWeakReference.cs: now a native struct (vtable pointer, raw WeakGCHandle<object> as nint, reference count) allocated with NativeMemory, with QueryInterface (IUnknown and IWeakReference only, as before), AddRef, Release and Resolve.
  • src/WinRT.Runtime/InteropServices/ProjectionImpls/IWeakReferenceImpl.cs: new IWeakReferenceImpl with the static vtable and [UnmanagedCallersOnly] thunks.
  • src/WinRT.Runtime/InteropServices/ProjectionImpls/IWeakReferenceSourceImpl.cs: GetWeakReference creates the native object directly instead of going through ComInterfaceMarshaller<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, Resolve for 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 ReflectionPerf benchmarks (GetWeakReferenceOfManagedObject and GetAndResolveWeakReferenceOfManagedObject) already exercise this path, so they can be used to track it going forward:

Scenario CoreCLR Native AOT
Create weak reference 2,591 ns, 526 B to 66 ns, 0 B 7,472 ns, 2,135 B to 106 ns, 0 B
Create, resolve, release 2,600 ns to 84 ns 7,554 ns to 140 ns
Resolve (target alive) 24.4 ns to 23.4 ns 34.0 ns to 31.8 ns
Resolve (target collected) 14.7 ns to 11.3 ns 21.4 ns to 19.6 ns
QueryInterface 13.3 ns to 27.1 ns 38.2 ns to 32.5 ns
AddRef + Release 8.2 ns to 25.0 ns 21.8 ns to 28.3 ns

Creation is about 40x (CoreCLR) and 70x (Native AOT) faster and allocation-free. AddRef, Release and QueryInterface are slower on CoreCLR, because the old CCW used the runtime's native IUnknown while these are now managed UnmanagedCallersOnly thunks. They run a few times per weak reference, so the creation savings outweigh this.

Notes

  • The new Resolve returns E_POINTER for a null output pointer, where the old code would have failed inside its generated stub.
  • Unit tests were not run locally (the environment couldn't build that project); the runtime builds in Debug, Release and as a reference assembly.

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>
@azure-pipelines

Copy link
Copy Markdown
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>
@Sergio0694
Sergio Pedri (Sergio0694) marked this pull request as ready for review November 23, 2026 21:38
@Sergio0694

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The public API is essentially GetOrCreateComInterfaceForObject.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not sure I understand what do you mean here, should I change anything? 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Sergio0694
Sergio Pedri (Sergio0694) merged commit 22bee87 into staging/3.0 Nov 25, 2026
20 checks passed
@Sergio0694
Sergio Pedri (Sergio0694) deleted the sergio0694-custom-weak-reference-no-comwrappers branch November 25, 2026 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CsWinRT 3.0 performance Related to performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants