From 29d565bb2e1be147d6f895ec2e105e23c5d9671b Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Sun, 27 Aug 2023 23:55:04 +0800 Subject: [PATCH 1/3] perf The module flattening function can reduce GC using MutableSet --- .../kotlin/org/koin/core/module/Module.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt index 9f95ac3d5..35465de4d 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt @@ -236,16 +236,17 @@ operator fun List.plus(module: Module): List = this + listOf(mod * Run through the module list to flatten all modules & submodules */ @OptIn(KoinInternalApi::class) -tailrec fun flatten(modules: List, newModules: Set = emptySet()): Set { - return if (modules.isEmpty()) { - newModules - } else { - val head = modules.first() ?: error("Flatten - No head element in list") +tailrec fun flatten(modules: List, newModules: MutableSet = mutableSetOf()): Set { + if (modules.isNotEmpty()) { + val head = modules.first() val tail = modules.subList(1, modules.size) - if (head.includedModules.isEmpty()) { - flatten(tail, newModules + head) + newModules.add(head) + if (head.importedModules.isEmpty()) { + return flatten(tail, newModules) } else { - flatten(head.includedModules + tail, newModules + head) + return flatten(head.importedModules + tail, newModules) } + } else { + return newModules } } \ No newline at end of file From 50bfa99bf1b66972cb260f5f0c5d06e773ac88c4 Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Mon, 28 Aug 2023 13:55:10 +0800 Subject: [PATCH 2/3] perf The module flattening function can reduce GC using MutableSet --- .../commonMain/kotlin/org/koin/core/module/Module.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt index 35465de4d..422eef27d 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt @@ -237,16 +237,16 @@ operator fun List.plus(module: Module): List = this + listOf(mod */ @OptIn(KoinInternalApi::class) tailrec fun flatten(modules: List, newModules: MutableSet = mutableSetOf()): Set { - if (modules.isNotEmpty()) { + return if (modules.isNotEmpty()) { val head = modules.first() val tail = modules.subList(1, modules.size) newModules.add(head) - if (head.importedModules.isEmpty()) { - return flatten(tail, newModules) + if (head.includedModules.isEmpty()) { + flatten(tail, newModules) } else { - return flatten(head.importedModules + tail, newModules) + flatten(head.includedModules + tail, newModules) } } else { - return newModules + newModules } } \ No newline at end of file From fe49b2780247232d144bf643ec184a2daa2ed3a6 Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Fri, 1 Sep 2023 18:13:24 +0800 Subject: [PATCH 3/3] perf The module flattening function reduce GC by removing 'tailrec' and using 'MutableSet' --- .../kotlin/org/koin/core/module/Module.kt | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt index 422eef27d..2fa53edca 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt @@ -236,17 +236,12 @@ operator fun List.plus(module: Module): List = this + listOf(mod * Run through the module list to flatten all modules & submodules */ @OptIn(KoinInternalApi::class) -tailrec fun flatten(modules: List, newModules: MutableSet = mutableSetOf()): Set { - return if (modules.isNotEmpty()) { - val head = modules.first() - val tail = modules.subList(1, modules.size) - newModules.add(head) - if (head.includedModules.isEmpty()) { - flatten(tail, newModules) - } else { - flatten(head.includedModules + tail, newModules) +fun flatten(modules: List): Set { + fun flat(modules: List, newModules: MutableSet){ + modules.forEach{ + newModules += it + flat(it.includedModules,newModules) } - } else { - newModules } + return mutableSetOf().apply { flat(modules,this) } } \ No newline at end of file