-
Notifications
You must be signed in to change notification settings - Fork 519
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
[Bgen] Simplify the BindAs generated code. #22216
Conversation
When a property that returns an NSNumber array is marked with the BindAs attribute, the bgen code generator uses the following pattern: ```csharp NativeHandle retvalarrtmp; ret = ((retvalarrtmp = global::ObjCRuntime.Messaging.NativeHandle_objc_msgSend (this.Handle, Selector.GetHandle ("sourceSampleDataTrackIDs"))) == IntPtr.Zero ? null! : (NSArray.ArrayFromHandleFunc <int> (retvalarrtmp, ptr => { using (var num = Runtime.GetNSObject<NSNumber> (ptr)!) { return (int) num.Int32Value; } }, false))); ``` This code can be simplified to use a static group method. So that it looks like: ```csharp NativeHandle retvalarrtmp; ret = ((retvalarrtmp = global::ObjCRuntime.Messaging.NativeHandle_objc_msgSend (this.Handle, Selector.GetHandle ("sourceSampleDataTrackIDs"))) == IntPtr.Zero ? null! : (NSArray.ArrayFromHandleFunc <int> (retvalarrtmp, NSNumber.ToInt32, false))); ``` A seasoned C# developer would mention that in C# 10 and under using method groups (specially static ones) alocates more memory than using lambdas (lambdas were cached, static methods were not, see dotnet/roslyn#5835). Alas, this is not longer the case after dotnet/roslyn#58288 which means that the memory usage is the same allowing us to clean our generated code. Using static group methods also opens the door to other possible improvements mentioned in the roslyn PR. PS: This is some extra work we are doing to make the generated code simpler before we move to rgen.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@@ -130,6 +130,96 @@ public static explicit operator bool (NSNumber source) | |||
return source.BoolValue; | |||
} | |||
|
|||
public static bool ToBool (NativeHandle handle) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xml docs would be nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on xml docs
Co-authored-by: Rolf Bjarne Kvinge <[email protected]>
This comment has been minimized.
This comment has been minimized.
1. Add missing xml comments. 2. Remove the ToEnum method which can give casting exceptions and use a lamdba in that case with a cast. For reference, the enum lambda looks like: ```csharp ptr => (global::CoreVideo.CVPixelFormatType) NSNumber.ToUInt32 (ptr) ```
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ [PR Build] Build passed (Build packages) ✅Pipeline on Agent |
✅ [PR Build] Build passed (Detect API changes) ✅Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
💻 [CI Build] Windows Integration Tests passed 💻✅ All Windows Integration Tests passed. Pipeline on Agent |
✅ [PR Build] Build passed (Build macOS tests) ✅Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ API diff for current PR / commit.NET ( No breaking changes )❗ API diff vs stable (Breaking changes).NET ( ❗ Breaking changes ❗ )ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
💻 [PR Build] Tests on macOS X64 - Mac Sonoma (14) passed 💻✅ All tests on macOS X64 - Mac Sonoma (14) passed. Pipeline on Agent |
💻 [PR Build] Tests on macOS M1 - Mac Monterey (12) passed 💻✅ All tests on macOS M1 - Mac Monterey (12) passed. Pipeline on Agent |
💻 [PR Build] Tests on macOS M1 - Mac Ventura (13) passed 💻✅ All tests on macOS M1 - Mac Ventura (13) passed. Pipeline on Agent |
💻 [PR Build] Tests on macOS arm64 - Mac Sequoia (15) passed 💻✅ All tests on macOS arm64 - Mac Sequoia (15) passed. Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
🔥 [CI Build] Test results 🔥Test results❌ Tests failed on VSTS: test results 0 tests crashed, 1 tests failed, 113 tests passed. Failures❌ dotnettests tests (MacCatalyst)
Html Report (VSDrops) Download Successes✅ cecil: All 1 tests passed. Html Report (VSDrops) Download Pipeline on Agent |
This changes does two things: 1. Adds a GetValue internal helper for non value backend smart enums. 2. Uses the GetValue functions as the builder for the NSArray. The new method (1) is only added to those smart enums that use a none value type backing field that looks like the following: ```csharp internal static ASAuthorizationProviderAuthorizationOperation GetValue (NativeHandle handle) { using var str = Runtime.GetNSObject<NSString> (handle); return GetValue (str); } ``` This is later used to retrieve the enum values from the handle in a BindAs decorated method/property. This is very similar to what we did in #22216 and has the same performance considerations (there is no performance change after C# 11 and later).
This changes does two things: 1. Adds a GetValue internal helper for non value backend smart enums. 2. Uses the GetValue functions as the builder for the NSArray. The new method (1) is only added to those smart enums that use a none value type backing field that looks like the following: ```csharp internal static ASAuthorizationProviderAuthorizationOperation GetValue (NativeHandle handle) { using var str = Runtime.GetNSObject<NSString> (handle); return GetValue (str); } ``` This is later used to retrieve the enum values from the handle in a BindAs decorated method/property. This is very similar to what we did in #22216 and has the same performance considerations (there is no performance change after C# 11 and later). --------- Co-authored-by: GitHub Actions Autoformatter <[email protected]>
When a property that returns an NSNumber array is marked with the BindAs attribute, the bgen code generator uses the following pattern:
This code can be simplified to use a static group method. So that it looks like:
A seasoned C# developer would mention that in C# 10 and under using method groups (specially static ones) alocates more memory than using lambdas (lambdas were cached, static methods were not, see dotnet/roslyn#5835). Alas, this is not longer the case after dotnet/roslyn#58288 which means that the memory usage is the same allowing us to clean our generated code.
Using static group methods also opens the door to other possible improvements mentioned in the roslyn PR.
PS: This is some extra work we are doing to make the generated code simpler before we move to rgen.