Commit 3cf86c8
[Java.Interop] remove IEnumerable iterators called during startup (#555)
Context: dotnet/android#4102
When profiling with `adb shell setprop debug.mono.profile log:alloc`
against the [Xamarin.Forms integration project in xamarin-android][0],
I noticed the following allocations:
Allocation summary
Bytes Count Average Type name
84840 505 168 Java.Interop.JniRuntime.JniTypeManager.<CreateGetTypeSignaturesEnumerator>d__11
24096 502 48 Android.Runtime.AndroidTypeManager.<GetSimpleReferences>d__1
100K of memory allocations are a bit much, so seemed like it might
help to reduce these.
To reduce the allocations, update `JniTypeManager.GetTypeSignature()`
to *not* call `JniTypeManager.GetTypeSignatures()` (note: plural) and
instead "duplicate" the `GetTypeSignatures()` logic, optimizing for a
single return value.
`JniTypeManager.GetTypeSignature()` calls the new method
`JniTypeManager.GetSimpleReference()`.
There are thus two methods of consequence derived types can override:
partial class JniTypeManager {
protected virtual string GetSimpleReference (Type type);
protected virtual IEnumerable<string> GetSimpleReferences (Type type);
}
Which one should be overridden?
`GetSimpleReference()` is implemented in terms of
`GetSimpleReferences()`, so the only method that is "required" to be
overridden is `GetSimpleReferences()`.
However, when concerned about performance, *both* methods should be
overridden, taking care to ensure that `GetSimpleReference()` returns
the *first* value that `GetSimpleReferences()` would return.
Other general performance changes:
* Added `[MethodImpl(MethodImplOptions.AggressiveInlining)]` to
`JniTypeManager.AssertValid()`. It is called quite frequently.
* Removed a call to `.GetTypeInfo()`, as this was not needed. It
[wraps `System.Type` in another type][1] that was probably
originally used for netstandard 1.x compatibility.
~~ Results ~~
After [updating xamarin-android][2] to override
`JniTypeManager.GetSimpleReference()`, I was able to see a startup
improvement in a Release build of the Xamarin.Forms integration
project on a Pixel 3 XL:
* Before:
01-08 14:53:34.926 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +779ms
01-08 14:53:38.892 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +764ms
01-08 14:53:42.859 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +753ms
01-08 14:53:46.795 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +748ms
01-08 14:53:50.792 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +764ms
01-08 14:53:54.759 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +756ms
01-08 14:53:58.726 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +764ms
01-08 14:54:02.675 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +749ms
01-08 14:54:06.691 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +759ms
01-08 14:54:10.641 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +749ms
* Average(ms): 758.5
* Std Err(ms): 3.0523215208537
* Std Dev(ms): 9.65228815704684
* After:
01-08 14:55:56.972 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +772ms
01-08 14:56:00.906 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +741ms
01-08 14:56:05.006 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +741ms
01-08 14:56:08.940 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +748ms
01-08 14:56:12.890 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +747ms
01-08 14:56:16.839 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +753ms
01-08 14:56:20.824 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +757ms
01-08 14:56:24.775 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +746ms
01-08 14:56:28.776 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +747ms
01-08 14:56:32.740 1473 1503 I ActivityTaskManager: Displayed Xamarin.Forms_Performance_Integration/xamarin.forms.performance.integration.MainActivity: +748ms
* Average(ms): 750
* Std Err(ms): 2.87904305089189
* Std Dev(ms): 9.10433352249844
It seems like this might save ~8ms on startup?
There is also a good improvement to memory usage/allocations.
Looking at Mono's memory summary:
* Before:
* Working Set : 71008256
* Private Bytes : 601202688
* Virtual Bytes : 1306873856
* After:
* Working Set : 70774784
* Private Bytes : 552910848
* Virtual Bytes : 1302822912
Then the total GC summary:
* Before:
Event: GC allocations : 9340
* After:
Event: GC allocations : 8867
~473 allocations are saved.
[0]: https://github.com/xamarin/xamarin-android/tree/master/tests/Xamarin.Forms-Performance-Integration
[1]: https://github.com/mono/mono/blob/c5b88ec4f323f2bdb7c7d0a595ece28dae66579c/mcs/class/referencesource/mscorlib/system/reflection/introspectionextensions.cs#L24
[2]: dotnet/android#41021 parent f6babec commit 3cf86c8
1 file changed
+83
-29
lines changedLines changed: 83 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
| 44 | + | |
| 45 | + | |
42 | 46 | | |
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
46 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
47 | 96 | | |
48 | 97 | | |
| 98 | + | |
49 | 99 | | |
50 | 100 | | |
51 | 101 | | |
52 | 102 | | |
53 | 103 | | |
54 | 104 | | |
55 | | - | |
56 | | - | |
| 105 | + | |
| 106 | + | |
57 | 107 | | |
58 | | - | |
59 | | - | |
| 108 | + | |
60 | 109 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | 110 | | |
78 | 111 | | |
79 | 112 | | |
| |||
87 | 120 | | |
88 | 121 | | |
89 | 122 | | |
90 | | - | |
91 | 123 | | |
92 | | - | |
| 124 | + | |
93 | 125 | | |
94 | 126 | | |
95 | 127 | | |
96 | 128 | | |
97 | | - | |
98 | | - | |
99 | | - | |
| 129 | + | |
| 130 | + | |
100 | 131 | | |
101 | 132 | | |
102 | | - | |
| 133 | + | |
103 | 134 | | |
104 | 135 | | |
105 | 136 | | |
106 | | - | |
| 137 | + | |
107 | 138 | | |
108 | 139 | | |
109 | 140 | | |
| |||
119 | 150 | | |
120 | 151 | | |
121 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
122 | 176 | | |
123 | 177 | | |
124 | 178 | | |
| |||
0 commit comments