C# What JIT Generates? - Struct Devirtualization

  Рет қаралды 1,524

LevelUp

LevelUp

Күн бұрын

Пікірлер: 6
@MGSncB
@MGSncB 3 жыл бұрын
About these methods at 8:13 and why they all got optimized... It's probably not a bug, and yes, it is related to tiered compilation. The reason is (I think) due to something called the "JIT interface". In .NET Core and .NET 5, the JIT and the runtime (CLR) communicate via the JIT interface. When the jitter consumes the IL code for the first time, it initializes fields, it may do a ton of extra stuff, and it generates code with very few optimizations, if any (because remember - the jitter has to put very limited time for compilation to good use here). If the code path is hot, the JIT compiler recompiles the code, but before that it will sort of "consult" the runtime via that interface, and it is going to tell the JIT that this Get() method's return value has already been set. "Hey, you don't need to initialize anything, the value is known (it is hot, thus we already called the method), it is ALWAYS 4 in every single scenario!" From the CLR's point of view, that "4" is a hidden static readonly field, even though it is used in an instance method. Discard everything you can, and load-return 4. Voilà.
@KoziLord
@KoziLord Жыл бұрын
(I'm like two years too late but oh well) Actually I'm pretty sure that the JIT can just see from the context that 'a' is ALWAYS of type S because it's initialised in the same scope. There's no other type that it can possibly ever be in that case. If you know that a is of type S, you know which Get() you will end up calling which means no need for virtual dispatch. To me it seems like the case of "1 + a.Get()" not optimising that is just the compiler not being clever enough to see that fact in a more complex scenario.
@driversteve9345
@driversteve9345 3 жыл бұрын
What are you using or how do you set up a way to see the MSIL code immediately after changing your source code? Thanks!
@LevelUppp
@LevelUppp 3 жыл бұрын
SharpLab.io
Inlining heuristics in .NET / C# can be hilarious sometimes.
0:25
C# What JIT Generates? - Bounds Check
14:36
LevelUp
Рет қаралды 801
А что бы ты сделал? @LimbLossBoss
00:17
История одного вокалиста
Рет қаралды 11 МЛН
C# JIT Tier Performance
13:24
LevelUp
Рет қаралды 2,3 М.
C# What JIT Generates? - Loop Cloning
14:25
LevelUp
Рет қаралды 1,2 М.
C# JIT Decompilation Tips using WinDBG
13:55
LevelUp
Рет қаралды 1,6 М.
WHY did this C++ code FAIL?
38:10
The Cherno
Рет қаралды 272 М.
Data Structures: Introduction to Bit Sets
4:05
LevelUp
Рет қаралды 12 М.
C# If Else Internals
22:42
LevelUp
Рет қаралды 3,7 М.
UI Performance Improvement I WPF, SQL Server and C#
22:05
Michael Gautier
Рет қаралды 820
C# What JIT Generates? - Folds
10:36
LevelUp
Рет қаралды 1,5 М.
Pointers and their Discontents - Part 2
57:03
Niklas Borson
Рет қаралды 90