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

Skip QI and 'IObjectReference' allocation in IDIC fallback #1833

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/WinRT.Runtime/IWinRTObject.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,21 @@ internal sealed bool IsInterfaceImplementedFallback(RuntimeTypeHandle interfaceT
AdditionalTypeData.GetOrAdd(projectIEnum, (_) => new ABI.System.Collections.IEnumerable.AdaptiveFromAbiHelper(type, this));
}

using (objRef)
bool hasMovedObjRefOwnership = false;

try
{
var vftblType = helperType.FindVftblType();

// If there is no nested vftbl type, we want to add the object reference with the IID from the helper type
// to the cache. Rather than doing 'QueryInterface' again with the same IID, on the object reference we
// already have, we can just store that same instance in the cache, and suppress the 'objRef.Dispose()'
// call for it. This avoids that extra call, plus the overhead of allocating a new object reference.
// For all other cases, we dispose the object reference as usual at the end of this scope.
if (vftblType is null)
{
var qiObjRef = objRef.As<IUnknownVftbl>(GuidGenerator.GetIID(helperType));
if (!QueryInterfaceCache.TryAdd(interfaceType, qiObjRef))
{
qiObjRef.Dispose();
}
hasMovedObjRefOwnership = QueryInterfaceCache.TryAdd(interfaceType, objRef);

return true;
}

Expand Down Expand Up @@ -230,6 +234,13 @@ static IObjectReference GetObjectReferenceViaVftbl(IObjectReference objRef, Type

return true;
}
finally
{
if (!hasMovedObjRefOwnership)
{
objRef.Dispose();
}
}
}

#if NET8_0_OR_GREATER
Expand Down