Hi there. I'm having some serious performance issu...
# koin
s
Hi there. I'm having some serious performance issues with Koin on Android. When I open a new activity the main thread freezes for about 7 seconds while resolving injections. Has anyone seen something similar?
A bit more info. We migrated from Dagger to Koin. We have ~450 definitions total (number extracted from `BeanRegistry.definitions`'s size while debugging).
Looking at systrace I can't see any specific big bottleneck. The long time seems to be the sum of a lot of recursive calls to InstanceRegistry.resolve() for all transitive dependencies. Nothing weird, but looking deeper every dependency is resolved in BeanRegistry.searchByClass(), which takes more than 10ms each. That's a lot!
Funny weird facts: - It only happens on physical devices, emulators seem unaffected. - It only happens on debug buildtype, seems to work fine on release builds but I can't profile it to see what changed.
s
Does it have something like Dagger's Lazy?
s
It does. I'm not using it in this example, but i do in other screens with similar result
a
Even around 450 definitions, resolution should not exceed ~1ms or seomthign like that
do you create other stuffs?
What device do you use?
What version of Koin?
s
If the object creation takes a while, you could inject an observable and offload the construction to a background thread via rxjava
s
We're using 1.0.1
I tried it witg a Samsung Galaxy J5 and a OnePlus 5 with same results
And we're not doing anything special in constructors or factories, just receiving other dependencies
c
It seems what we saw matches with: https://github.com/InsertKoinIO/koin/pull/258
but you said is done in 1.0.2 and it seems 1.0.2 still doing filter…
a
optimization has been pushed out before reintegration / bad perfs behind 😞
A couple of results:
Copy code
=========|=====================
Device:  | OnePlus OnePlus5 v8.1.0
---------|--------------------
Test:    | Koin + Kotlin
Max:     | 30,92 ms
Min:     | 4,16 ms
Average: | 5,30 ms
---------|--------------------
Test:    | Koin + Java
Max:     | 9,15 ms
Min:     | 4,57 ms
Average: | 5,00 ms
=========|=====================

=========|=====================
Device:  | Huawei hwALE-H v6.0
---------|--------------------
Test:    | Koin + Kotlin
Max:     | 292,83 ms
Min:     | 244,74 ms
Average: | 251,38 ms
---------|--------------------
Test:    | Koin + Java
Max:     | 413,13 ms
Min:     | 383,13 ms
Average: | 386,13 ms
=========|=====================
With tweaks similar to the ones in the PR we get
Copy code
=========|=====================
Device:  | Huawei hwALE-H v6.0
---------|--------------------
Test:    | Koin + Kotlin
Max:     | 25,71 ms
Min:     | 14,17 ms
Average: | 15,26 ms
---------|--------------------
Test:    | Koin + Java
Max:     | 155,88 ms
Min:     | 145,08 ms
Average: | 146,83 ms
=========|=====================
a
@sloydev ok thanks
Why your Koin+Java part is so long?
s
We haven't figure that out yet. I plan on diggin on the cpu profiler to see what's going on. Seems weird that creating java classes take longer than creating kotlin ones, but who knows