Skip to content

Conversation

@stephentoub
Copy link
Member

Every call to GetOrAddTraceSource was allocating a new delegate instance.

…ource

Every call to GetOrAddTraceSource was allocating a new delegate instance.
@ghost ghost assigned stephentoub Jun 22, 2022
@ghost
Copy link

ghost commented Jun 22, 2022

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

Issue Details

Every call to GetOrAddTraceSource was allocating a new delegate instance.

Author: stephentoub
Assignees: -
Labels:

area-Extensions-Logging

Milestone: -

Copy link
Member

@danmoseley danmoseley left a comment

Choose a reason for hiding this comment

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

Nit, maybe long enough to have some curlies.

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +57 to +58
_sources.TryGetValue(name, out DiagnosticsTraceSource? source) ? source :
_sources.GetOrAdd(name, InitializeTraceSource(name));
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.

@stephentoub stephentoub merged commit de078d1 into dotnet:main Jun 22, 2022
@stephentoub stephentoub deleted the tracedelegate branch June 22, 2022 18:53
@ghost ghost locked as resolved and limited conversation to collaborators Jul 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants