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

Improve handling of Exception<T> #272

Merged

Conversation

elachlan
Copy link
Contributor

@elachlan elachlan commented May 4, 2022

Copy link
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?

test/Exceptionless.Tests/Plugins/PluginTestBase.cs Outdated Show resolved Hide resolved
src/Exceptionless/Extensions/TypeExtensions.cs Outdated Show resolved Hide resolved
@elachlan
Copy link
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
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
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
Contributor Author

elachlan commented May 6, 2022

@ejsmith any chance of a quick review?

Copy link
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!

src/Exceptionless/Demystifier/TypeNameHelper.cs Outdated Show resolved Hide resolved
src/Exceptionless/Extensions/ToErrorModelExtensions.cs Outdated Show resolved Hide resolved
src/Exceptionless/Extensions/TypeExtensions.cs Outdated Show resolved Hide resolved
@elachlan elachlan requested a review from niemyjski May 9, 2022 12:08
@elachlan
Copy link
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
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
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
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
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
Contributor Author

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

@niemyjski
Copy link
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