Skip to content

Commit f498fcf

Browse files
authored
[Java.Interop] Avoid some method group conversions (#1050)
Context: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#improved-method-group-conversion-to-delegate Context: #1034 Context: dotnet/roslyn#62832 C#11 introduced a "slight semantic" change with "Improved method group conversion to delegate": > The C# 11 compiler caches the delegate object created from a method > group conversion and reuses that single delegate object. The result of optimization is that for current `generator`-emitted code such as: static Delegate GetFooHandler () { if (cb_foo == null) cb_foo = JNINativeWrapper.CreateDelegate ((_JniMarshal_PP_V) n_Foo); return cb_foo; } The `(_JniMarshal_PP_V) n_Foo` expression is a "method group conversion", and under C#11 the generated IL is *larger*, as the delegate instance is *cached* in case it is needed again. However in *our* case we *know* the delegate instance won't be needed again, not in this scope, so all this "optimization" does *for us* is increase the size of our binding assemblies when built under C#11. Review `src/Java.Interop` for use of `new JniNativeMethodRegistration` and replace "cast-style" method group conversions `(D) M` to "new-style" delegate conversions `new D(M)`. This explicitly "opts-out" of the C#11 optimization.
1 parent 16e1ecd commit f498fcf

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/Java.Interop/Java.Interop/JavaProxyObject.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ sealed class JavaProxyObject : JavaObject, IEquatable<JavaProxyObject>
1717
[JniAddNativeMethodRegistrationAttribute]
1818
static void RegisterNativeMembers (JniNativeMethodRegistrationArguments args)
1919
{
20-
args.Registrations.Add (new JniNativeMethodRegistration ("equals", "(Ljava/lang/Object;)Z", (EqualsMarshalMethod)Equals));
21-
args.Registrations.Add (new JniNativeMethodRegistration ("hashCode", "()I", (GetHashCodeMarshalMethod)GetHashCode));
22-
args.Registrations.Add (new JniNativeMethodRegistration ("toString", "()Ljava/lang/String;", (ToStringMarshalMethod)ToString));
20+
args.Registrations.Add (new JniNativeMethodRegistration ("equals", "(Ljava/lang/Object;)Z", new EqualsMarshalMethod (Equals)));
21+
args.Registrations.Add (new JniNativeMethodRegistration ("hashCode", "()I", new GetHashCodeMarshalMethod (GetHashCode)));
22+
args.Registrations.Add (new JniNativeMethodRegistration ("toString", "()Ljava/lang/String;", new ToStringMarshalMethod (ToString)));
2323
}
2424

2525
public override JniPeerMembers JniPeerMembers {

src/Java.Interop/Java.Interop/ManagedPeer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ static ManagedPeer ()
2525
new JniNativeMethodRegistration (
2626
"construct",
2727
ConstructSignature,
28-
(ConstructMarshalMethod) Construct),
28+
new ConstructMarshalMethod (Construct)),
2929
new JniNativeMethodRegistration (
3030
"registerNativeMembers",
3131
RegisterNativeMembersSignature,
32-
(RegisterMarshalMethod) RegisterNativeMembers)
32+
new RegisterMarshalMethod (RegisterNativeMembers))
3333
);
3434
}
3535

0 commit comments

Comments
 (0)