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

Cache StringBuilder per thread and remove Lazy from GetRuntimeClassName. #1381

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 13 additions & 3 deletions src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using WinRT.Interop;

#if NET
Expand Down Expand Up @@ -871,14 +872,23 @@ private static ComInterfaceEntry ProvideIReferenceArray(Type arrayType)

internal sealed class InspectableInfo
{
private readonly Lazy<string> runtimeClassName;
private readonly Type _type;

public Guid[] IIDs { get; }
public string RuntimeClassName => runtimeClassName.Value;

private volatile string _runtimeClassName;

private string MakeRuntimeClassName()
{
global::System.Threading.Interlocked.CompareExchange(ref _runtimeClassName, TypeNameSupport.GetNameForType(_type, TypeNameGenerationFlags.GenerateBoxedName | TypeNameGenerationFlags.ForGetRuntimeClassName), null);
return _runtimeClassName;
}

public string RuntimeClassName => _runtimeClassName ?? MakeRuntimeClassName();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can simplify this by just moving the code for MakeRuntimeClassName in the getter (using a body), rather than needing the separate method there, which is only ever used here anyway?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the pattern followed elsewhere so sticking with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's... Not a great pattern? And it's also wasteful (more IL size and extra JIT work for no reason), not to mention unnecessarily verbose. We should try to improve the codebase as we find things, rather than just sticking to existing code patterns that are not good in the first place. Of course, this can be a follow up 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying that we shouldn't change it but I'd rather change it everywhere at the same time :).


internal InspectableInfo(Type type, Guid[] iids)
{
runtimeClassName = new Lazy<string>(() => TypeNameSupport.GetNameForType(type, TypeNameGenerationFlags.GenerateBoxedName | TypeNameGenerationFlags.ForGetRuntimeClassName));
_type = type;
IIDs = iids;
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/WinRT.Runtime/TypeNameSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,29 @@ struct VisitedType
/// Tracker for visited types when determining a WinRT interface to use as the type name.
/// Only used when GetNameForType is called with <see cref="TypeNameGenerationFlags.ForGetRuntimeClassName"/>.
/// </summary>
private static readonly ThreadLocal<Stack<VisitedType>> VisitedTypes = new ThreadLocal<Stack<VisitedType>>(() => new Stack<VisitedType>());
private static readonly ThreadLocal<Stack<VisitedType>> VisitedTypes = new ThreadLocal<Stack<VisitedType>>(() => new Stack<VisitedType>());

[ThreadStatic]
private static StringBuilder? nameForTypeBuilderInstance;

public static string GetNameForType(Type type, TypeNameGenerationFlags flags)
{
if (type is null)
{
return string.Empty;
}
StringBuilder nameBuilder = new StringBuilder();

// Get instance for this thread
StringBuilder? nameBuilder = nameForTypeBuilderInstance ?? new StringBuilder();
jlaanstra marked this conversation as resolved.
Show resolved Hide resolved
nameBuilder.Clear();
if (TryAppendTypeName(type, nameBuilder, flags))
{
return nameBuilder.ToString();
}
var name = nameBuilder.ToString();
nameForTypeBuilderInstance = nameBuilder;
return name;
}

nameForTypeBuilderInstance = nameBuilder;
return string.Empty;
}

Expand Down