Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ public ILogger CreateLogger(string name)
return new TraceSourceLogger(GetOrAddTraceSource(name));
}

private DiagnosticsTraceSource GetOrAddTraceSource(string name)
{
return _sources.GetOrAdd(name, InitializeTraceSource);
}
private DiagnosticsTraceSource GetOrAddTraceSource(string name) =>
_sources.TryGetValue(name, out DiagnosticsTraceSource? source) ? source :
_sources.GetOrAdd(name, InitializeTraceSource(name));
Comment on lines +57 to +58
Copy link
Member

Choose a reason for hiding this comment

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

It feels like this is such a perf trap ... 99.9% of devs would write the code as it was originally.

Copy link
Member Author

Choose a reason for hiding this comment

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

If InitializeTraceSource was static, it'd be ok (as of C# 11 and its caching of method group delegates). But as an instance method, the compiler is forced to allocate a new delegate. That's the nature of implicit delegate conversions.

Copy link
Contributor

@sharwell sharwell Jun 22, 2022

Choose a reason for hiding this comment

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

The other option here is:

_sources.GetOrAdd(name, static (name, self) => self.InitializeTraceSource(name), this)

Copy link
Contributor

Choose a reason for hiding this comment

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

It feels like this is such a perf trap

It's been this way for a very, very long time. Since most code won't have an observable perf impact of a delegate allocation in a call like this, it hasn't been too big a deal for most of that time (IMO of course).

Copy link
Member Author

Choose a reason for hiding this comment

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

The other option here is

This library has a netstandard2.0 target.


private DiagnosticsTraceSource InitializeTraceSource(string traceSourceName)
{
Expand Down