From 21aa5b80543bef1f5a5ebfb5edc17bed843bf860 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 25 Sep 2024 02:22:58 +0200 Subject: [PATCH] [bgen] Improve performance a little bit when calling functions that return retained objects. Instead of doing this: ```cs var obj = Runtime.GetINativeObject<T>(handle, false); NSObject.ReleaseDangerous (handle); ``` we now do this: ```cs var obj = Runtime.GetINativeObject<T>(handle, true); ``` Less generated code, and better performance too. This was found while investigating https://github.com/xamarin/xamarin-macios/issues/21290. --- src/bgen/Generator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bgen/Generator.cs b/src/bgen/Generator.cs index 6752ee0530a7..0208eba94716 100644 --- a/src/bgen/Generator.cs +++ b/src/bgen/Generator.cs @@ -2918,7 +2918,7 @@ void GetReturnsWrappers (MethodInfo mi, MemberInformation minfo, Type declaringT // protocol support means we can return interfaces and, as far as .NET knows, they might not be NSObject if (IsProtocolInterface (mi.ReturnType)) { cast_a = " Runtime.GetINativeObject<" + TypeManager.FormatType (minfo?.type ?? mi.DeclaringType, mi.ReturnType) + "> ("; - cast_b = ", false)!"; + cast_b = $", {(minfo?.is_return_release == true ? "true" : "false")})!"; } else if (minfo is not null && minfo.is_forced) { cast_a = " Runtime.GetINativeObject<" + TypeManager.FormatType (minfo.type, mi.ReturnType) + "> ("; cast_b = $", true, {minfo.is_forced_owns})!"; @@ -3686,7 +3686,7 @@ public void GenerateMethodBody (MemberInformation minfo, MethodInfo mi, string s if (shouldMarshalNativeExceptions) print ("Runtime.ThrowException (exception_gchandle);"); - if (minfo.is_return_release) { + if (minfo.is_return_release && !IsProtocolInterface (mi.ReturnType)) { // Make sure we generate the required signature in Messaging only if needed // bool_objc_msgSendSuper_IntPtr: for respondsToSelector: