Lazily initialize JCW delegates #9306
Labels
Area: App Runtime
Issues in `libmonodroid.so`.
Area: Performance
Issues with performance.
enhancement
Proposed change to current functionality.
Note: all performance numbers mentioned are run on Android Emulator on a DevBox, so they are somewhat inflated.
Imagine we have a Java type that contains a lot of virtual methods:
Now imagine we bind it, and override all of those methods:
Then we create an instance of this object in C#:
new MyDelegateTester()
.For a
Release
config with marshal methods turned off, this object creation takes ~694ms. (As an aside, this case really shines with marshal methods.)The issue is that we call
mono.android.Runtime.register
from thestatic
constructor of the generated Java peer type, and this calls the C# generatedGet*Handler
immediately for every overridden method (in this case 100) even if they aren't immediately used. If we could make these calls lazy instead it would likely improve app startup and navigating to a new activity, since these are places where objects are often created but are not used until later.Ideally, this could be done in the Java(?) layer as it could then improve all existing bindings. However it could also be done at the binding level.
Instead of:
We could add an extra level of indirection:
Switching to a test case of calling a single method instead of 100, we can see that
CreateDelegate
cost has moved from the constructor to the first invoke of the method:Note: The cost is abnormally high in this example because it is the first call that uses S.R.E, causing the overhead of initializing those APIs.
The text was updated successfully, but these errors were encountered: