Skip to content

Commit

Permalink
Microbenchmark optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
manodasanW committed Oct 6, 2021
1 parent d840c4b commit 14304d9
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ unsafe static bool IsFreeThreaded(IObjectReference unknownRef)
Marshal.Release(agilePtr);
return true;
}
else if (unknownRef.TryAs<ABI.WinRT.Interop.IMarshal.Vftbl>(out var marshalRef) >= 0)
else if (unknownRef.TryAs<ABI.WinRT.Interop.IMarshal.Vftbl>(ABI.WinRT.Interop.IMarshal.IID, out var marshalRef) >= 0)
{
using (marshalRef)
{
Guid iid_IUnknown = typeof(IUnknownVftbl).GUID;
Guid iid_IUnknown = IUnknownVftbl.IID;
Guid iid_unmarshalClass;
Marshal.ThrowExceptionForHR(marshalRef.Vftbl.GetUnmarshalClass_0(
marshalRef.ThisPtr, &iid_IUnknown, IntPtr.Zero, MSHCTX.InProc, IntPtr.Zero, MSHLFLAGS.Normal, &iid_unmarshalClass));
Expand Down
8 changes: 4 additions & 4 deletions src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public unsafe static void Init(
// otherwise the new instance will be used. Since the inner was composed
// it should answer immediately without going through the outer. Either way
// the reference count will go to the new instance.
Guid iid = typeof(IReferenceTrackerVftbl).GUID;
Guid iid = IReferenceTrackerVftbl.IID;
int hr = Marshal.QueryInterface(objRef.ThisPtr, ref iid, out referenceTracker);
if (hr != 0)
{
Expand Down Expand Up @@ -330,7 +330,7 @@ public unsafe static void Init(IObjectReference objRef, bool addRefFromTrackerSo
{
if (objRef.ReferenceTrackerPtr == IntPtr.Zero)
{
Guid iid = typeof(IReferenceTrackerVftbl).GUID;
Guid iid = IReferenceTrackerVftbl.IID;
int hr = Marshal.QueryInterface(objRef.ThisPtr, ref iid, out var referenceTracker);
if (hr == 0)
{
Expand Down Expand Up @@ -434,7 +434,7 @@ private static unsafe bool IsRuntimeImplementedRCW(Type objType)

private static object CreateObject(IObjectReference objRef)
{
if (objRef.TryAs<IInspectable.Vftbl>(out var inspectableRef) == 0)
if (objRef.TryAs<IInspectable.Vftbl>(IInspectable.IID, out var inspectableRef) == 0)
{
IInspectable inspectable = new IInspectable(inspectableRef);

Expand All @@ -448,7 +448,7 @@ private static object CreateObject(IObjectReference objRef)

return ComWrappersSupport.GetTypedRcwFactory(runtimeClassName)(inspectable);
}
else if (objRef.TryAs<ABI.WinRT.Interop.IWeakReference.Vftbl>(out var weakRef) == 0)
else if (objRef.TryAs<ABI.WinRT.Interop.IWeakReference.Vftbl>(ABI.WinRT.Interop.IWeakReference.IID, out var weakRef) == 0)
{
// IWeakReference is IUnknown-based, so implementations of it may not (and likely won't) implement
// IInspectable. As a result, we need to check for them explicitly.
Expand Down
4 changes: 3 additions & 1 deletion src/WinRT.Runtime/IInspectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public enum TrustLevel
[ObjectReferenceWrapper(nameof(_obj))]
[Guid("AF86E2E0-B12D-4c6a-9C5A-D7AA65101E90")]
public partial class IInspectable
{
{
internal static readonly Guid IID = Guid.Parse("AF86E2E0-B12D-4c6a-9C5A-D7AA65101E90");

[Guid("AF86E2E0-B12D-4c6a-9C5A-D7AA65101E90")]
public unsafe struct Vftbl
{
Expand Down
4 changes: 3 additions & 1 deletion src/WinRT.Runtime/Interop/IMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ namespace ABI.WinRT.Interop
{
[Guid("00000003-0000-0000-c000-000000000046")]
internal class IMarshal
{
{
internal static readonly Guid IID = Guid.Parse("00000003-0000-0000-c000-000000000046");

[DllImport("api-ms-win-core-com-l1-1-0.dll")]
private static extern int CoCreateFreeThreadedMarshaler(IntPtr outer, out IntPtr marshalerPtr);

Expand Down
2 changes: 2 additions & 0 deletions src/WinRT.Runtime/Interop/IReferenceTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ internal unsafe struct IReferenceTrackerVftbl
private void* _ReleaseFromTrackerSource_5;
public delegate* unmanaged[Stdcall]<IntPtr, int> ReleaseFromTrackerSource { get => (delegate* unmanaged[Stdcall]<IntPtr, int>)_ReleaseFromTrackerSource_5; set => _ReleaseFromTrackerSource_5 = (void*)value; }
private void* _PegFromTrackerSource_6;

internal static readonly Guid IID = Guid.Parse("11D3B13A-180E-4789-A8BE-7712882893E6");
}
}
4 changes: 3 additions & 1 deletion src/WinRT.Runtime/Interop/IUnknownVftbl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public unsafe struct IUnknownVftbl
public delegate* unmanaged[Stdcall]<IntPtr, uint> Release { get => (delegate* unmanaged[Stdcall]<IntPtr, uint>)_Release; set => _Release = (void*)value; }

public static IUnknownVftbl AbiToProjectionVftbl => ComWrappersSupport.IUnknownVftbl;
public static IntPtr AbiToProjectionVftblPtr => ComWrappersSupport.IUnknownVftblPtr;
public static IntPtr AbiToProjectionVftblPtr => ComWrappersSupport.IUnknownVftblPtr;

internal static readonly Guid IID = Guid.Parse("00000000-0000-0000-C000-000000000046");
}
}
4 changes: 3 additions & 1 deletion src/WinRT.Runtime/Interop/IWeakReferenceSource.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ private static int Do_Abi_GetWeakReference(IntPtr thisPtr, IntPtr* weakReference
[DynamicInterfaceCastableImplementation]
[Guid("00000037-0000-0000-C000-000000000046")]
internal unsafe interface IWeakReference : global::WinRT.Interop.IWeakReference
{
{
internal static readonly Guid IID = Guid.Parse("00000037-0000-0000-C000-000000000046");

[Guid("00000037-0000-0000-C000-000000000046")]
public struct Vftbl
{
Expand Down
6 changes: 3 additions & 3 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ public static IObjectReference CreateMarshaler(T o, bool unwrapObject = true)

if (unwrapObject && ComWrappersSupport.TryUnwrapObject(o, out var objRef))
{
return objRef.As<IInspectable.Vftbl>();
return objRef.As<IInspectable.Vftbl>(IInspectable.IID);
}
var publicType = o.GetType();
Type helperType = Projections.FindCustomHelperTypeMapping(publicType, true);
Expand All @@ -1052,7 +1052,7 @@ public static IObjectReference CreateMarshaler(T o, bool unwrapObject = true)
}
using (var ccw = ComWrappersSupport.CreateCCWForObject(o))
{
return ccw.As<IInspectable.Vftbl>();
return ccw.As<IInspectable.Vftbl>(IInspectable.IID);
}
}

Expand All @@ -1066,7 +1066,7 @@ public static T FromAbi(IntPtr ptr)
return default;
}
using var objRef = ObjectReference<IUnknownVftbl>.FromAbi(ptr);
using var unknownObjRef = objRef.As<IUnknownVftbl>();
using var unknownObjRef = objRef.As<IUnknownVftbl>(IUnknownVftbl.IID);
if (unknownObjRef.IsReferenceToManagedObject)
{
return (T) ComWrappersSupport.FindObject<object>(unknownObjRef.ThisPtr);
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/Projections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ internal static bool TryGetMarshalerTypeForProjectedRuntimeClass<T>(IObjectRefer
Type projectedType = typeof(T);
if (projectedType == typeof(object))
{
if (objectReference.TryAs<IInspectable.Vftbl>(out var inspectablePtr) == 0)
if (objectReference.TryAs<IInspectable.Vftbl>(IInspectable.IID, out var inspectablePtr) == 0)
{
rwlock.EnterReadLock();
try
Expand Down

0 comments on commit 14304d9

Please sign in to comment.