Skip to content

Commit

Permalink
reduce usage of getruntimeclassname (#1043)
Browse files Browse the repository at this point in the history
* reduce usage of getruntimeclassname

* Add new cache for types

* Feedback
  • Loading branch information
ujjwalchadha authored Nov 12, 2021
1 parent ec2966b commit 39fbcdf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
20 changes: 20 additions & 0 deletions src/Benchmarks/QueryInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ public bool QuerySDKNonDefaultInterface()
return message.IsSeen;
}

[Benchmark]
public object DefaultObjectParameters()
{
instance.DefaultObjectProperty = new ClassWithMultipleInterfaces();
return instance.DefaultObjectProperty;
}

[Benchmark]
public object DefaultStringParameters()
{
instance.DefaultStringProperty = "Hello";
return instance.DefaultStringProperty;
}

[Benchmark]
public object DynamicCast()
{
return (ClassWithMarshalingRoutines)instance.NewObject();
}

// The following 2 benchmarks try to benchmark the time taken for the first call
// rather than the mean time over several calls. It has the overhead of the object
// construction, but it can be used to track regressions to performance.
Expand Down
48 changes: 27 additions & 21 deletions src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace WinRT
#endif
static partial class ComWrappersSupport
{
private readonly static ConcurrentDictionary<string, Func<IInspectable, object>> TypedObjectFactoryCache = new ConcurrentDictionary<string, Func<IInspectable, object>>(StringComparer.Ordinal);
private readonly static ConcurrentDictionary<string, Func<IInspectable, object>> TypedObjectFactoryCacheForRuntimeClassName = new ConcurrentDictionary<string, Func<IInspectable, object>>(StringComparer.Ordinal);
private readonly static ConcurrentDictionary<Type, Func<IInspectable, object>> TypedObjectFactoryCacheForType = new ConcurrentDictionary<Type, Func<IInspectable, object>>();
private readonly static ConditionalWeakTable<object, object> CCWTable = new ConditionalWeakTable<object, object>();

public static TReturn MarshalDelegateInvoke<TDelegate, TReturn>(IntPtr thisPtr, Func<TDelegate, TReturn> invoke)
Expand Down Expand Up @@ -315,8 +316,8 @@ private static Func<IInspectable, object> CreateReferenceCachingFactory(Func<IIn
_boxedValueReferenceCache.Add(resultingObject, inspectable);
return resultingObject;
};
}

}

private static Func<IInspectable, object> CreateCustomTypeMappingFactory(Type customTypeHelperType)
{
var fromAbiMethod = customTypeHelperType.GetMethod("FromAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
Expand All @@ -330,25 +331,8 @@ private static Func<IInspectable, object> CreateCustomTypeMappingFactory(Type cu
Expression.Call(fromAbiMethod, Expression.Property(parms[0], "ThisPtr")), parms).Compile();
}

internal static Func<IInspectable, object> CreateTypedRcwFactory(string runtimeClassName)
internal static Func<IInspectable, object> CreateTypedRcwFactory(Type implementationType, string runtimeClassName = null)
{
// If runtime class name is empty or "Object", then just use IInspectable.
if (string.IsNullOrEmpty(runtimeClassName) ||
string.CompareOrdinal(runtimeClassName, "Object") == 0)
{
return (IInspectable obj) => obj;
}
// PropertySet and ValueSet can return IReference<String> but Nullable<String> is illegal
if (string.CompareOrdinal(runtimeClassName, "Windows.Foundation.IReference`1<String>") == 0)
{
return CreateReferenceCachingFactory((IInspectable obj) => new ABI.System.Nullable<String>(obj.ObjRef));
}
else if (string.CompareOrdinal(runtimeClassName, "Windows.Foundation.IReference`1<Windows.UI.Xaml.Interop.TypeName>") == 0)
{
return CreateReferenceCachingFactory((IInspectable obj) => new ABI.System.Nullable<Type>(obj.ObjRef));
}

Type implementationType = TypeNameSupport.FindTypeByNameCached(runtimeClassName);
if (implementationType == null)
{
// If we reach here, then we couldn't find a type that matches the runtime class name.
Expand Down Expand Up @@ -386,6 +370,28 @@ internal static Func<IInspectable, object> CreateTypedRcwFactory(string runtimeC
return CreateFactoryForImplementationType(runtimeClassName, implementationType);
}

internal static Func<IInspectable, object> CreateTypedRcwFactory(string runtimeClassName)
{
// If runtime class name is empty or "Object", then just use IInspectable.
if (string.IsNullOrEmpty(runtimeClassName) ||
string.CompareOrdinal(runtimeClassName, "Object") == 0)
{
return (IInspectable obj) => obj;
}
// PropertySet and ValueSet can return IReference<String> but Nullable<String> is illegal
if (string.CompareOrdinal(runtimeClassName, "Windows.Foundation.IReference`1<String>") == 0)
{
return CreateReferenceCachingFactory((IInspectable obj) => new ABI.System.Nullable<String>(obj.ObjRef));
}
else if (string.CompareOrdinal(runtimeClassName, "Windows.Foundation.IReference`1<Windows.UI.Xaml.Interop.TypeName>") == 0)
{
return CreateReferenceCachingFactory((IInspectable obj) => new ABI.System.Nullable<Type>(obj.ObjRef));
}

Type implementationType = TypeNameSupport.FindTypeByNameCached(runtimeClassName);
return CreateTypedRcwFactory(implementationType, runtimeClassName);
}

internal static string GetRuntimeClassForTypeCreation(IInspectable inspectable, Type staticallyDeterminedType)
{
string runtimeClassName = inspectable.GetRuntimeClassName(noThrow: true);
Expand Down
12 changes: 9 additions & 3 deletions src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static IObjectReference CreateCCWForObject(object obj)
{
IntPtr ccw = ComWrappers.GetOrCreateComInterfaceForObject(obj, CreateComInterfaceFlags.TrackerSupport);
return ObjectReference<IUnknownVftbl>.Attach(ref ccw);
}
}

internal static ObjectReference<T> CreateCCWForObject<T>(object obj, Guid iid)
{
Expand Down Expand Up @@ -210,8 +210,8 @@ public static void InitializeComWrappers(ComWrappers wrappers = null)
ComWrappers = wrappers;
}

internal static Func<IInspectable, object> GetTypedRcwFactory(string runtimeClassName) => TypedObjectFactoryCache.GetOrAdd(runtimeClassName, className => CreateTypedRcwFactory(className));

internal static Func<IInspectable, object> GetTypedRcwFactory(string runtimeClassName) => TypedObjectFactoryCacheForRuntimeClassName.GetOrAdd(runtimeClassName, className => CreateTypedRcwFactory(className));
internal static Func<IInspectable, object> GetTypedRcwFactory(Type implementationType) => TypedObjectFactoryCacheForType.GetOrAdd(implementationType, classType => CreateTypedRcwFactory(classType));

private static Func<IInspectable, object> CreateFactoryForImplementationType(string runtimeClassName, Type implementationType)
{
Expand Down Expand Up @@ -471,6 +471,12 @@ private static object CreateObject(IObjectReference objRef)
{
IInspectable inspectable = new IInspectable(inspectableRef);

if (ComWrappersSupport.CreateRCWType != null
&& ComWrappersSupport.CreateRCWType.IsSealed)
{
return ComWrappersSupport.GetTypedRcwFactory(ComWrappersSupport.CreateRCWType)(inspectable);
}

string runtimeClassName = ComWrappersSupport.GetRuntimeClassForTypeCreation(inspectable, ComWrappersSupport.CreateRCWType);
if (string.IsNullOrEmpty(runtimeClassName))
{
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static T CreateRcwForComObject<T>(IntPtr ptr)
{
var inspectable = new IInspectable(identity);
string runtimeClassName = GetRuntimeClassForTypeCreation(inspectable, typeof(T));
runtimeWrapper = string.IsNullOrEmpty(runtimeClassName) ? inspectable : TypedObjectFactoryCache.GetOrAdd(runtimeClassName, className => CreateTypedRcwFactory(className))(inspectable);
runtimeWrapper = string.IsNullOrEmpty(runtimeClassName) ? inspectable : TypedObjectFactoryCacheForRuntimeClassName.GetOrAdd(runtimeClassName, className => CreateTypedRcwFactory(className))(inspectable);
}
else if (identity.TryAs<ABI.WinRT.Interop.IWeakReference.Vftbl>(out var weakRef) == 0)
{
Expand Down

0 comments on commit 39fbcdf

Please sign in to comment.