Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jnienv-gen] Add possible C#9 function pointer backend #938

Merged
merged 1 commit into from
Jan 5, 2022

Commits on Jan 4, 2022

  1. [jnienv-gen] Add possible C#9 function pointer backend

    Context: 926e4bc
    Context: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/function-pointers
    Context? dotnet#666
    
    Commit 926e4bc allowed `jnienv-gen` to emit multiple different
    JNIEnv invocation strategies at the same time, allowing
    `tests/invocation-overhead` to try them "all at once" for side-by-
    side comparisons.
    
    Add support for JNIEnv invocation strategy which relies on
    C#9 Function Pointers, a'la:
    
    	partial struct JNIEnv {
    	    public  delegate* unmanaged <IntPtr /* env */, jobject> ExceptionOccurred;
    	}
    	partial class JniEnvironment {
    	    partial class Exceptions {
    	        public static unsafe JniObjectReference ExceptionOccurred ()
    	        {
    	            IntPtr __env = JniEnvironment.EnvironmentPointer;
    	            var tmp = (*((JNIEnv**)__env))->ExceptionOccurred (__env);
    	            return new JniObjectReference (tmp, JniObjectReferenceType.Local);
    	        }
    	    }
    	}
    
    This *should* allow for performance better than "JIPinvokeTiming",
    as it avoids P/Invoke overheads to a set of `java_interop_*` C
    functions (926e4bc), while *also* avoiding the overheads involved
    with using `Marshal.GetDelegateForFunctionPointer()` as used by
    "JIIntPtrs".
    
    …but it doesn't:
    
    	$ JI_JVM_PATH=$HOME/android-toolchain/jdk-11/lib/jli/libjli.dylib dotnet tests/invocation-overhead/bin/Debug/net6.0/invocation-overhead.dll
    	# SafeTiming timing: 00:00:04.2123508
    	#	Average Invocation: 0.00042123508ms
    	# XAIntPtrTiming timing: 00:00:02.1625501
    	#	Average Invocation: 0.00021625500999999998ms
    	# JIIntPtrTiming timing: 00:00:02.3620239
    	#	Average Invocation: 0.00023620239ms
    	# JIPinvokeTiming timing: 00:00:01.8993587
    	#	Average Invocation: 0.00018993587ms
    	# JIFunctionPointersTiming timing: 00:00:02.0278083
    	#	Average Invocation: 0.00020278083ms
    
    (Compare and contrast with 926e4bc, circa 2015!)
    
    Of particular note is that the Average Invocation time for
    JIFunctionPointersTiming takes 7% longer than JIPinvokeTiming.
    
    We may not continue investigation of C#9 Function Pointers
    for `JNIEnv` binding purposes, but we can preserve this code for
    future investigation.
    jonpryor committed Jan 4, 2022
    Configuration menu
    Copy the full SHA
    e9fd048 View commit details
    Browse the repository at this point in the history