Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow delegate creation

I upgraded ReSharper and seeing an error that I was not present previously. I checked around, but found nothing about the error or the underlying issue it is flagging.

** Edit **: as pointed out below, it is actually the 'Heap Allocation Viewer' plugin, not ReSharper itself that is marking it as an error -- though that doesn't change the question itself.

Slow delegate creation: from interface 'IPluginHandler' method

this occurs during the subscription of a plugin handler to events on an event aggregator.

public void Subscribe(IPluginHandler subscriber)
{
  Executing += subscriber.OnExecuting;
  // -- additional subscriptions --
}

in the above code, Executing is an event and subscriber.OnExecuting is an appropriate event handler for the event.

To be clear, this is a ReSharper 'soft error' as the code will still build and run as expected.

So my question is what is the underlying issue the good people at JetBrains are flagging for me and what are the ramifications of it.

Thanks

like image 293
David Culp Avatar asked Oct 15 '14 19:10

David Culp


People also ask

Why delegate is faster?

Delegates are faster because they are only a pointer to a method. Interfaces need to use a v-table to then find a delegate; They are equal, but delegates are easier to use.

Are delegates immutable?

Although the term combining delegates might give the impression that the operand delegates are modified, they are not changed at all. In fact, delegates are immutable. After a delegate object is created, it cannot be changed.

What is a delegate in .NET framework?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.


1 Answers

This JetBrains blog post has the same question in a comment.

The reply there says this:

Hi! This plugin also has one more internal feature: code inspection to show ‘slow’ (>10x slower) delegate instance creations for CLR x86 JIT. You can run this test (it creates delegates from various kinds of methods – virtual/interface/generic/etc) to see the difference in delegate creation performance.

Just as with allocations inspection – you shouldn’t care about this much until some performance snapshot in some hot path of your application shows long invocations of CLR internals. And just like with allocations – this inspections may (and will) produce false positives with new RuyJIT, for example.

Note that the linked test highlights the "slow" delegation creations with an arrow comment: <--.

like image 64
Matthew Strawbridge Avatar answered Sep 29 '22 16:09

Matthew Strawbridge