Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core: What does MethodImplOptions.AggressiveOptimization exactly do?

What does MethodImplOptions.AggressiveOptimization exactly do? Microsoft's documentation doesn't not tell me much. In which cases can it be useful?

like image 493
codymanix Avatar asked May 04 '20 20:05

codymanix


1 Answers

I would look for more details info (if that's what's you are looking for) not in the documentation but on .net core github or in release notes.

Let's start by looking at the latter. For .net core 3.0 we can find the following entry:

The fully optimizing JIT produces higher-quality (or more optimized) code more slowly. For methods where Quick JIT would not be used (for example, if the method is attributed with MethodImplOptions.AggressiveOptimization), the fully optimizing JIT is used.

So for one, we know that if a method is marked with such attribute, it should be JITed with fully optimizing JIT, which might produce better, more optimized code - but would take more time to compile.

Now let's focus on github and see what we can find there.

The discussion about this is done in this ticket, and it gives a bit more details into the topic.

The flag could be used in a MethodImplAttribute to indicate that the method contains hot code:

  • For tiering, it could cause tier 1 JIT to be used right away for the method[...]
  • It could allow the JIT to spend more JIT time to generate better code, such as inlining more aggressively into the function

From this we can get an answer to, in which cases it could be used but also what it is doing underneath.

In cases we deal with hot path code - this attribute is useful to JIT it to produce faster, more optimized code instead of doing tiered compilation. Using more time at the beginning saves time later if runtime detects that it's, in fact, on the hot path.

There's also an interesting discussion about other usages of this flag which you can read.

But the ultimate truth (I hope) and commits are linked to this discussion so we can look at them. And from those commits and commit messages we can get that this is in fact what's happening with tiered compilation and JIT (at least this is what I can see).

like image 183
Paweł Łukasik Avatar answered Nov 12 '22 17:11

Paweł Łukasik