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

Emit SkipLocalsInit for .NET 5 #785

Open
jonpryor opened this issue Jan 22, 2021 · 0 comments
Open

Emit SkipLocalsInit for .NET 5 #785

jonpryor opened this issue Jan 22, 2021 · 0 comments
Labels
enhancement Proposed change to current functionality generator Issues binding a Java library (generator, class-parse, etc.)

Comments

@jonpryor
Copy link
Member

C#9 allows specifying the System.Runtime.CompilerServices.SkipLocalsInitAttribute custom attribute on methods (and other things) for performance reasons:

Finally, you can add the System.Runtime.CompilerServices.SkipLocalsInitAttribute to instruct the compiler not to emit the localsinit flag. This flag instructs the CLR to zero-initialize all local variables. The localsinit flag has been the default behavior for C# since 1.0. However, the extra zero-initialization may have measurable performance impact in some scenarios. In particular, when you use stackalloc. In those cases, you can add the SkipLocalsInitAttribute.

Our generated binding code does use stackalloc, e.g. https://github.com/xamarin/java.interop/blob/b0d170c1002e5481564c0afc78afd6dbf3d40e66/tests/generator-Tests/expected.ji/Adapters/Xamarin.Test.AdapterView.cs#L117-L129

We should consider updating generator output to place [SkipLocalsInit] on such methods, e.g.

[Register ("setAdapter", "(Lxamarin/test/Adapter;)V", "GetSetAdapter_Lxamarin_test_Adapter_Handler")]
[SkipLocalsInit]
set {
	const string __id = "setAdapter.(Lxamarin/test/Adapter;)V";
	IntPtr native_value = JNIEnv.ToLocalJniHandle (value);
	try {
		JniArgumentValue* __args = stackalloc JniArgumentValue [1];
		__args [0] = new JniArgumentValue (native_value);
		_members.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
	} finally {
		JNIEnv.DeleteLocalRef (native_value);
		global::System.GC.KeepAlive (value);
	}
}

Problems

The SkipLocalsInitAttribute is added in .NET 5, which places it into a similar "bucket" as SupportedOSPlatformAttribute (da12df4, a33084b), which we had to disable in 00862ad.

We likely can't emit SkipLocalsInitAttribute until we can emit SupportedOSPlatformAttribute.

@jonpryor jonpryor added enhancement Proposed change to current functionality generator Issues binding a Java library (generator, class-parse, etc.) labels Jan 22, 2021
@jonathanpeppers jonathanpeppers added this to the Under consideration milestone Jan 27, 2021
@jpobst jpobst removed their assignment Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Proposed change to current functionality generator Issues binding a Java library (generator, class-parse, etc.)
Projects
None yet
Development

No branches or pull requests

3 participants