Commit a91ae7f
authored
[generator] improve *Implementor constructors (#1105)
Context: dotnet/maui#12130
Context: https://github.com/angelru/CvSlowJittering
Profiling a .NET MAUI customer sample while scrolling on a Pixel 5,
I see ~2.2% of the time spent in the
`IOnFocusChangeListenerImplementor` constructor, due to
[subscription to the `View.FocusChange` event][0]:
(2.2%) mono.android!Android.Views.View.IOnFocusChangeListenerImplementor..ctor()
MAUI subscribes to [`Android.Views.View.FocusChange` event][1] for
every view placed on the screen, which happens while scrolling in
this sample.
Reviewing, the generated code for the
`IOnFocusChangeListenerImplementor` constructor, we see it still uses
outdated `JNIEnv` APIs:
public IOnFocusChangeListenerImplementor ()
: base (global::Android.Runtime.JNIEnv.StartCreateInstance ("mono/android/view/View_OnFocusChangeListenerImplementor", "()V"), JniHandleOwnership.TransferLocalRef)
{
global::Android.Runtime.JNIEnv.FinishCreateInstance (((global::Java.Lang.Object) this).Handle, "()V");
}
Which we can change to use the newer/faster Java.Interop APIs:
public unsafe IOnFocusChangeListenerImplementor ()
: base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
{
const string __id = "()V";
if (((global::Java.Lang.Object) this).Handle != IntPtr.Zero)
return;
var h = JniPeerMembers.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), null);
SetHandle (h.Handle, JniHandleOwnership.TransferLocalRef);
JniPeerMembers.InstanceMethods.FinishCreateInstance (__id, this, null);
}
These are better because the equivalent call to `JNIEnv.FindClass()`
is cached, among other things.
After these changes, I now see:
(0.81%) mono.android!Android.Views.View.IOnFocusChangeListenerImplementor..ctor()
This should improve the performance of all C# events that wrap Java
listeners, including and all .NET MAUI views on Android.
Additionally, stop emitting the `[Register]` attribute for
`*Implementor` classes:
[global::Android.Runtime.Register ("mono/android/view/View_OnFocusChangeListenerImplementor")]
partial class IOnFocusChangeListenerImplementor {/* … */}
The `[Register]` attribute is not needed, because `*Implementor`
classes are generated internal implementation details.
[0]: https://github.com/dotnet/maui/blob/2041476e78452891029f4d2bd806c45be42f4878/src/Core/src/Handlers/View/ViewHandler.Android.cs#L14
[1]: https://learn.microsoft.com/en-us/dotnet/api/android.views.view.focuschange?view=xamarin-android-sdk-131 parent 7d42864 commit a91ae7f
File tree
5 files changed
+38
-18
lines changed- tests/generator-Tests
- Unit-Tests/CodeGeneratorExpectedResults
- Common
- JavaInterop1
- expected.xaji
- Core_Jar2Xml
- GenericArguments
- tools/generator/SourceWriters
5 files changed
+38
-18
lines changedLines changed: 7 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
160 | | - | |
161 | 160 | | |
162 | 161 | | |
163 | 162 | | |
164 | 163 | | |
165 | | - | |
| 164 | + | |
166 | 165 | | |
167 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
168 | 172 | | |
169 | 173 | | |
170 | 174 | | |
| |||
Lines changed: 7 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
55 | | - | |
| 54 | + | |
56 | 55 | | |
57 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
58 | 62 | | |
59 | 63 | | |
60 | 64 | | |
| |||
Lines changed: 7 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
115 | 114 | | |
116 | | - | |
| 115 | + | |
117 | 116 | | |
118 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
119 | 123 | | |
120 | 124 | | |
121 | 125 | | |
| |||
Lines changed: 7 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | | - | |
166 | 165 | | |
167 | 166 | | |
168 | 167 | | |
169 | 168 | | |
170 | | - | |
| 169 | + | |
171 | 170 | | |
172 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
173 | 177 | | |
174 | 178 | | |
175 | 179 | | |
| |||
Lines changed: 10 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | 25 | | |
28 | 26 | | |
29 | 27 | | |
30 | | - | |
| 28 | + | |
31 | 29 | | |
32 | 30 | | |
33 | 31 | | |
34 | | - | |
| 32 | + | |
35 | 33 | | |
36 | 34 | | |
37 | 35 | | |
| |||
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
44 | | - | |
| 42 | + | |
| 43 | + | |
45 | 44 | | |
46 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
| |||
0 commit comments