Are deterministically fatal actions and Funcs Inlined by JIT?

The name asks everything. We make good use of actions, action expressions, and callbacks in today's code.

Can JIT optimize these calls by inserting them into them? This will be a huge increase in productivity, given that inverse forms in one form or another are used today in absurd amounts.

JIT cannot optimize an action that will never change. For example, I see no reason why an action or Func marked with the readonly attribute should not be optimized.

eg:

readonly Action a; readonly Action a1; readonly Action a2; a = () => {}; a1 = () => { a() }; a2 = () => { a1() }; 

IS a2 ever optimized? I see no reason why this should not be.

+6
source share
2 answers

Not optimization itself is due to the fact that this is not the function itself, but the code that they execute. You assign a const reference to an action, but there is no guarantee that the code they execute is not modified and / or cannot produce constant behavior. If you cannot guarantee or predict some kind of persistent behavior, nesting is usually not a good idea, since you can have an unpredictable flow of execution, hence behavior that the .net framework is not aimed at.

In your specific example, where we have simple "dummy" functions that can lead to some embedding, it is possible, but it looks like a very special case, and something is unlikely to happen in someone else's code, so the introduction of optimization in .net framework will have little benefit for everyone, therefore, I think it is not implemented in the CLR .

EDIT

As @Sriram Sakthivel pointed out: having a reflection in .net runtime , such a possible and unpredictable change in function at runtime is probably one of the main factors of concern to allow the structure to be either this function is a good candidate for overlay or not.

In addition, the @hvd comment in @Sriram Sakthivel's answer points out why the readonly keyword specifically simply cannot guarantee deterministic immutability.

+1
source

No, delegates cannot be optimized for the same reason why virtual participants cannot be .

  • Virtual calls: we do not connect virtual calls. The reason for this is not that we do not know the ultimate goal of the challenge. We can potentially do it better (for example, if 99% of the calls end for the same purpose, you can generate a code that checks the method table of the object that the virtual call will be made for, if this is not a 99% case, you make a call otherwise you just execute inlined code), but unlike the J-language, most calls to the main languages ​​we support are not virtual, so we are not forced to be so aggressive in optimizing this case.

Delegates can point to something at runtime, so nesting is not so simple. I know where you came from with readonly , remember that it is not a silver bullet. Remember that reflection is more powerful than readonly, you can rewrite the readonly field with reflection. If the method was inlined, it will do something else as indicated by the delegate.

So it’s clear that JIT is not going to embed your delegate calls.

Update: I found an interesting article on this. Can I enable delegate calling?

+2
source

Source: https://habr.com/ru/post/980127/


All Articles