Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't repeat 'QueryInterface' calls for 'IReferenceTracker' when possible #113632

Conversation

Sergio0694
Copy link
Contributor

@Sergio0694 Sergio0694 commented Mar 18, 2025

This PR optimizes the TrackerObject scenario in ComWrappers on NativeAOT. When we're in an aggregation scenario, we're doing a QI for IReferenceTracker as part of resolving the object identity. Then we immediately discard this reference. However, further down we do another QueryInterface for the same IID on the external object, before creating a native object wrapper.

This PR avoids that, and instead passes down the pointer retrieved when determining the identity, so we can reuse it.

CsWinRT passes this flag for all objects, and in XAML scenarios pretty much all WinRT objects are tracker objects. This change will avoid that repeated QI for all of them. This is part of an ongoing effort in CsWinRT to avoid as many QIs as we possibly can.

Note

I'd recommend reviewing with whitespace off.

@Copilot Copilot bot review requested due to automatic review settings March 18, 2025 00:25
@Sergio0694 Sergio0694 requested a review from jkoritzinsky March 18, 2025 00:25
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 18, 2025
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot Copilot AI left a comment

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 COM interop code to avoid duplicate QueryInterface calls for IReferenceTracker in aggregation scenarios.

  • Updated NativeObjectWrapper.Create and related methods to pass a reusable reference tracker via a new ref parameter.
  • Modified DetermineIdentityAndInner to output the reference tracker pointer for later reuse.
  • Introduced NullableComHolder in TrackerObjectManager.NativeAot.cs to manage the lifetime of the reference tracker pointer.

Reviewed Changes

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

File Description
System/Runtime/InteropServices/ComWrappers.NativeAot.cs Updated method signatures and logic to reuse an existing IReferenceTracker pointer and eliminate redundant QI calls.
System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs Added NullableComHolder struct to support the new reference tracker management design.
Comments suppressed due to low confidence (1)

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs:993

  • The use of 'ref *&referenceTracker' is non-standard and likely erroneous. Please verify if 'ref referenceTracker' is intended.
retValue = RegisterObjectForComInstance(identity, inner, wrapperMaybe, flags, ref *&referenceTracker);

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@IDisposable IDisposable left a comment

Choose a reason for hiding this comment

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

Nice :shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr area-System.Runtime.InteropServices community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants