Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if JIT is available

Tags:

c#

jit

xamarin

What would be the canonical (if any, otherwise any reliable) way of detecting if the JIT engine is available in a portable way?

For example, Xamarin.iOS does not support JIT since the iOS platform enforces DEP. Mono is quite good at bridging the gap by interpreting most things like lambda expressions these days, but some thing are not (properly) implemented, leading to runtime exceptions that hurt performance badly.

I want to do this detection from a shared Portable Class Library, if possible.

like image 414
Krumelur Avatar asked Sep 08 '15 11:09

Krumelur


People also ask

How do I know if JIT is enabled?

To verify the JIT was enabled, create a page that calls the phpinfo() function, request the page from your browser, and verify opcache. jit_buffer_size shows the value of 100M rather than 0.

Is PHP a JIT?

PHP 8.0 brings support for Just-In-Time Compilation (JIT). Pure interpreted programming languages has no compilation step, and directly executes the code in a virtual machine. Most of the interpreted languages including PHP, in fact, has a light-weight compilation step to improve its performance.


2 Answers

You could try performing an operation that you know will fail on your AoT code but not on JIT (creating a type dynamically, for example) and use a try/catch block to define whether or not JIT is available.

It could be implemented like this (note that this will only fail when the linker is set to "Link All Assemblies"):

private static bool? _isJITAvailable = null;
public static bool IsJITAvailable()
{
    if(_isJITAvailable == null)
    {
        try
        { 
            //This crashes on iPhone
            typeof(GenericClass<>).MakeGenericType(typeof(int));
            _isJITAvailable = true;
        }
        catch(Exception)
        {
            _isJITAvailable = false;
        }
    }

    return _isJITAvailable.Value;
}

I would not do so, though. Do you really need those "better performing callbacks" to begin with? This really sounds like a premature optimization.

like image 187
William Barbosa Avatar answered Sep 25 '22 01:09

William Barbosa


After some digging into the Mono source code, it seems the "magic" build-time property is FULL_AOT_RUNTIME. For example, System.Reflection.Emit.AssemblyBuilder is conditionally compiled based on this property being unset, meaning

var canJit = Type.GetType ("System.Reflection.Emit.AssemblyBuilder") != null;

should get me what I want.

like image 20
Krumelur Avatar answered Sep 25 '22 01:09

Krumelur