Detecting if JIT is available
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.
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.
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.
Why not test the performance of the two callbacks at runtime?
If yours is slower than the platform's, use the platform's.
If the platform's is slower than yours, use yours.
上一篇: WordNet 3.1和WordNet 3.0有什么区别?
下一篇: 检测JIT是否可用