Skip to content

Commit

Permalink
cache GetHelperType lookup (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottj1s authored May 6, 2021
1 parent 396128c commit 56b520f
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/WinRT.Runtime/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Reflection;
using System.Text;

namespace WinRT
{

public static class TypeExtensions
{
private readonly static ConcurrentDictionary<Type, Type> HelperTypeCache = new ConcurrentDictionary<Type, Type>();

public static Type FindHelperType(this Type type)
{
if (typeof(Exception).IsAssignableFrom(type))
{
type = typeof(Exception);
}
Type customMapping = Projections.FindCustomHelperTypeMapping(type);
if (customMapping is object)
{
return customMapping;
}

string fullTypeName = type.FullName;
string ccwTypePrefix = "ABI.Impl.";
if (fullTypeName.StartsWith(ccwTypePrefix))
{
fullTypeName = fullTypeName.Substring(ccwTypePrefix.Length);
}

var helper = $"ABI.{fullTypeName}";
return Type.GetType(helper) ?? type.Assembly.GetType(helper);
return HelperTypeCache.GetOrAdd(type, (type) =>
{
if (typeof(Exception).IsAssignableFrom(type))
{
type = typeof(Exception);
}
Type customMapping = Projections.FindCustomHelperTypeMapping(type);
if (customMapping is object)
{
return customMapping;
}

string fullTypeName = type.FullName;
string ccwTypePrefix = "ABI.Impl.";
if (fullTypeName.StartsWith(ccwTypePrefix))
{
fullTypeName = fullTypeName.Substring(ccwTypePrefix.Length);
}

var helper = $"ABI.{fullTypeName}";
return Type.GetType(helper) ?? type.Assembly.GetType(helper);
});
}

public static Type GetHelperType(this Type type)
Expand Down

0 comments on commit 56b520f

Please sign in to comment.