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

Memoize InnerGetTypeName() #318

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 11 additions & 7 deletions Orm/Xtensive.Orm/Reflection/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public int GetHashCode((Type, Type[]) obj)
private static readonly Func<(Type genericDefinition, Type typeArgument1, Type typeArgument2), Type> GenericTypeFactory2 = key =>
key.genericDefinition.MakeGenericType(key.typeArgument1, key.typeArgument2);

private static readonly ConcurrentDictionary<(Type Type, bool UseShortForm), string> MemoizedTypeNames = new();

private static int createDummyTypeNumber = 0;
private static AssemblyBuilder assemblyBuilder;
private static ModuleBuilder moduleBuilder;
Expand Down Expand Up @@ -822,13 +824,15 @@ public static string GetShortName(this Type type)
return $"{declaringType.GetShortName()}+{type.InnerGetTypeName(useShortForm: true)}";
}

private static string InnerGetTypeName(this Type type, bool useShortForm)
{
var result = useShortForm
private static string InnerGetTypeName(this Type type, bool useShortForm) =>
MemoizedTypeNames.GetOrAdd((type, useShortForm), TypeNameFactory);

private static readonly Func<(Type Type, bool UseShortForm), string> TypeNameFactory = t => {
var (type, useShortForm) = t;

var result = useShortForm || type.DeclaringType != null // Is nested
? type.Name
: type.DeclaringType != null // Is nested
? type.Name
: type.Namespace + "." + type.Name;
: $"{type.Namespace}.{type.Name}";

var arrayBracketPosition = result.IndexOf('[');
if (arrayBracketPosition > 0) {
Expand Down Expand Up @@ -876,7 +880,7 @@ private static string InnerGetTypeName(this Type type, bool useShortForm)
result = sb.ToString();
}
return result;
}
};

/// <summary>
/// Indicates whether <paramref name="type"/> is a <see cref="Nullable{T}"/> type.
Expand Down