You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Treat readonly fields as JIT time constants. #4287
If understand it right mutating readonly fields outside of constructors is forbidden at CLR level.
In certain conditions this could allow JIT to treat static fields of classes as JIT time constants.
And it opens interesting opportunities.
For example, there is already a JIT times constant - IntPtr.Size. In the following code JIT will get rid not only of the wrong branch but the comparison itself:
if(IntPtr.Size==32){Console.WriteLine("It is an x86 process");}else{Console.WriteLine("It is an x64 process");}
JIT could do the same with "constant" static readonly fields.
Imagine such field:
"For example, there is already a JIT times constant - IntPtr.Size. In the following code JIT will get rid not only of the wrong branch but the comparison itself"
As far as I can tell RyuJIT already does that:
if(IntPtr.Size==8)Console.WriteLine(IntPtr.Size);
generates this
mov ecx,8
call 00007FFC36ADC1B0
If understand it right mutating readonly fields outside of constructors is forbidden at CLR level.
For scalar types such as int or floats the JIT compilers have optimized this kinds of readonly fields since CLR 3.5 at least. If an Object based readonly static field is initialized then it is not possible to optimize them as their values depend upon the address assigned at runtime.
Activity
mikedn commentedon Jun 2, 2015
"For example, there is already a JIT times constant - IntPtr.Size. In the following code JIT will get rid not only of the wrong branch but the comparison itself"
As far as I can tell RyuJIT already does that:
generates this
Yet the following code runs and prints 42:
omariom commentedon Jun 2, 2015
IntPtr.Size is an old trick. The idea is to extend it to static readonly fields.
yeah.. that's not complete readonliness 😞
GeirGrusom commentedon Jun 2, 2015
What about static readonly delegates? Could they be inlined or lifted?
mikedn commentedon Jun 2, 2015
Ah, I see. It does that too:
generates
Nope. Delegates are never inlined by current .NET JIT compilers.
omariom commentedon Jun 2, 2015
WHAAAT!?? It is already there!?
Huge thanks guys! I dreamed of this feature :)
🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉
omariom commentedon Jun 2, 2015
I checked it with ProcessorCount (just to be sure)
if is completely eliminated. As well as WriteLine if procCount != 2.
I think this RuyJIT ability deserves a separate post in JIT team blog. People should know about it.
briansull commentedon Jun 3, 2015
For scalar types such as int or floats the JIT compilers have optimized this kinds of readonly fields since CLR 3.5 at least. If an Object based readonly static field is initialized then it is not possible to optimize them as their values depend upon the address assigned at runtime.
omariom commentedon Jun 4, 2015
I am happily closing this issue.
TypeHelper<T>.Size must be static readonly to enable JIT branch elimi…