Skip to content

Improve handling of Exception<T>#272

Merged
niemyjski merged 6 commits intoexceptionless:masterfrom
elachlan:improve-handling-of-ExceptionT
May 9, 2022
Merged

Improve handling of Exception<T>#272
niemyjski merged 6 commits intoexceptionless:masterfrom
elachlan:improve-handling-of-ExceptionT

Conversation

@elachlan
Copy link
Copy Markdown
Contributor

@elachlan elachlan commented May 4, 2022

Copy link
Copy Markdown
Member

@niemyjski niemyjski left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, can we also add test cases specifically addressed in the issue around structs. I also left some feedback about performance concerns

@ejsmith should we be worried about this creating lots of new stacks due to hash changes for type names?

@elachlan
Copy link
Copy Markdown
Contributor Author

elachlan commented May 4, 2022

How about this?

private static readonly ConcurrentDictionary<Type, string> TypeNameCache = new ConcurrentDictionary<Type, string>();
public static string GetRealTypeName(this Type t) {
    if (TypeNameCache.TryGetValue(t, out string name)) {
        return name;
    }
    if (!t.IsGenericType)
        return t.FullName.Replace('+','.');

    StringBuilder sb = new StringBuilder();
    ReadOnlySpan<char> fullName = t.FullName.AsSpan();
    int plusIndex = fullName.IndexOf('+');
    if (plusIndex > 0) {
        sb.Append(fullName.Slice(0, plusIndex).ToArray());
        sb.Append('.');
    }
    int length = fullName.IndexOf('`') - (plusIndex > 0 ? plusIndex + 1 : 0);
    sb.Append(fullName.Slice(plusIndex > 0 ? plusIndex + 1 : 0, length).ToArray());
    sb.Append('<');
    bool appendComma = false;
    foreach (Type arg in t.GetGenericArguments()) {
        if (appendComma) { sb.Append(','); }
        sb.Append(GetRealTypeName(arg));
        appendComma = true;
    }
    sb.Append('>');
    return TypeNameCache.GetOrAdd(t, sb.ToString());
}

@elachlan
Copy link
Copy Markdown
Contributor Author

elachlan commented May 5, 2022

Should I just use the TypeNameHelper?

public static string GetTypeDisplayName(Type type, bool fullName = true, bool includeGenericParameterNames = false)
{
var builder = new StringBuilder();
ProcessType(builder, type, new DisplayNameOptions(fullName, includeGenericParameterNames));
return builder.ToString();
}

Only issue I see is that it includes the +, so instead of:
Exceptionless.Tests.Plugins.PluginTestBase.GenericException<Exceptionless.Tests.Plugins.PluginTestBase.ErrorCategory>
You get:
Exceptionless.Tests.Plugins.PluginTestBase+GenericException<Exceptionless.Tests.Plugins.PluginTestBase+ErrorCategory>

If this is okay, I will use that. I also suggest we add a cache of some kind to TypeNameHelper.

Added TypeNameHelper.GetTypeFullDisplayName and cache
@elachlan
Copy link
Copy Markdown
Contributor Author

elachlan commented May 5, 2022

@niemyjski How is that? Do you want a couple more test cases?

@elachlan elachlan requested a review from niemyjski May 6, 2022 21:27
@elachlan
Copy link
Copy Markdown
Contributor Author

elachlan commented May 6, 2022

@ejsmith any chance of a quick review?

Copy link
Copy Markdown
Member

@niemyjski niemyjski left a comment

Choose a reason for hiding this comment

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

Left some comments, It's nice that we found an existing method that handles this!

@elachlan elachlan requested a review from niemyjski May 9, 2022 12:08
@elachlan
Copy link
Copy Markdown
Contributor Author

elachlan commented May 9, 2022

Left some comments, It's nice that we found an existing method that handles this!

All sorted. Thanks for the review!

Copy link
Copy Markdown
Member

@niemyjski niemyjski left a comment

Choose a reason for hiding this comment

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

Looks good

@niemyjski niemyjski merged commit 87fd963 into exceptionless:master May 9, 2022
@niemyjski
Copy link
Copy Markdown
Member

Thanks for the PR! These are really nice changes @elachlan was there any other changes you wanted to get in prior to a release?

@elachlan
Copy link
Copy Markdown
Contributor Author

elachlan commented May 9, 2022

Thanks for the PR! These are really nice changes @elachlan was there any other changes you wanted to get in prior to a release?

I won't have the time to work on this for the next two weeks.

I was going to look at using spans in place of string operations at some point.

@niemyjski
Copy link
Copy Markdown
Member

I think if anything, we just report this possible optimization to the upstream dependency.

@elachlan elachlan deleted the improve-handling-of-ExceptionT branch May 9, 2022 13:59
@elachlan
Copy link
Copy Markdown
Contributor Author

Is this the upstream?
https://github.com/benaadams/Ben.Demystifier/

@niemyjski
Copy link
Copy Markdown
Member

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Improve handling of Exception<T> where T : struct

2 participants