Skip to content

Treat readonly fields as JIT time constants. #4287

Closed

Description

@omariom

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:

private static bool etwLoggingEnabled = ReadValueFromConfig();

And this check:

if (etwLoggingEnabled)
{
    DoEtwLogging();
}

Or it could be a performance counter or other similar thing.

Another case is static readonly array fields. JIT could skip bounds checking as if it were a local variable or a parameter.

There are possibly other optimization opportunities, you guys know your JIT better ;)

Activity

  1. mikedn commented on Jun 2, 2015

    @mikedn
    Contributor

    "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.

    Yet the following code runs and prints 42:

    public class Program {
        public static readonly int x = 1;
    
        static void Main() {
            typeof(Program).GetField("x").SetValue(null, 42);
            Console.WriteLine(typeof(Program).GetField("x").GetValue(null));
        }
    }
  2. omariom commented on Jun 2, 2015

    @omariom
    ContributorAuthor

    As far as I can tell RyuJIT already does thatAs far as I can tell RyuJIT already does that

    IntPtr.Size is an old trick. The idea is to extend it to static readonly fields.

    Yet the following code runs and prints 42:

    yeah.. that's not complete readonliness 😞

  3. GeirGrusom commented on Jun 2, 2015

    @GeirGrusom

    What about static readonly delegates? Could they be inlined or lifted?

  4. mikedn commented on Jun 2, 2015

    @mikedn
    Contributor

    The idea is to extend it to static readonly fields

    Ah, I see. It does that too:

    public class Program {
        public static readonly int x = 1;
        static void Main() {
            if (x == 1)
                Console.WriteLine(x);
        }
    }

    generates

    sub         rsp,28h  
    mov         ecx,1                            ;no if
    call        00007FFC36ADC1B0  
    add         rsp,28h  
    ret  
    

    What about static readonly delegates? Could they be inlined or lifted?

    Nope. Delegates are never inlined by current .NET JIT compilers.

  5. omariom commented on Jun 2, 2015

    @omariom
    ContributorAuthor

    WHAAAT!?? It is already there!?

    Huge thanks guys! I dreamed of this feature :)

    🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉 🎉

  6. omariom commented on Jun 2, 2015

    @omariom
    ContributorAuthor

    I checked it with ProcessorCount (just to be sure)

    class Program
        {
            public static readonly int procCount = Environment.ProcessorCount;
    
            static void Main(string[] args)
            {
                if (procCount  == 2)
                    Console.WriteLine("!");
            }
        }

    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.

  7. briansull commented on Jun 3, 2015

    @briansull
    Contributor

    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.

  8. omariom commented on Jun 4, 2015

    @omariom
    ContributorAuthor

    I am happily closing this issue.

  9. transferred this issue fromdotnet/coreclron Jan 30, 2020
  10. ghost locked as resolved and limited conversation to collaborators on Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions