InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.77k stars 695 forks source link

[koin-core] flatten function #1794

Closed pedrofsn closed 3 months ago

pedrofsn commented 4 months ago

I was analyzing an app initialization and I saw Koin consuming a lot of time in flatten function. This logic could be improved?

Flatten function:

@OptIn(KoinInternalApi::class)
fun flatten(modules: List<Module>): Set<Module> {
    fun flat(modules: List<Module>, newModules: MutableSet<Module>){
        modules.forEach{
            newModules += it
            flat(it.includedModules,newModules)
        }
    }
    return mutableSetOf<Module>().apply { flat(modules,this) }
}

I was thinking in process this in IO instead of Main thread. In this case Koin will need to open this to the client pass the flattened list.

Another idea is try to figure out if it possible to apply other algorithm to reduce the time-space complexity.

pedrofsn commented 4 months ago

Chart generated with macrobenchmark library, with Koin consuming a lot. chart

pedrofsn commented 4 months ago

We did a POC moving our koin to the background, using lazyLoadModules. And it is charming, but don't solve the problem with flatten function. image

pedrofsn commented 4 months ago

Thank you @guilhermemagro for providing this analysis and collecting the charts!

arnaudgiuliani commented 4 months ago

there is a first proposal with #1801 - let's try to benchmark results

arnaudgiuliani commented 3 months ago

see #1801