-
-
Notifications
You must be signed in to change notification settings - Fork 719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf The module flattening function can reduce GC using MutableSet #1640
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
When we use the In contrast, using the By using Therefore, using the However, |
We can use queue to traverse modules. At the beginning, we add the modules to the queue. Each time, we take a module out of the queue and add it to the set, while also add the included modules from this module to the queue until the queue is empty. Using queue instead of recursion can reduce GC while not changing the original module set. fun flatten(modules: List<Module>, newModules: Set<Module> = emptySet()): Set<Module> {
val queue: Queue<Module> = LinkedList(modules)
val flattenedModules = newModules.toMutableSet()
while (queue.isNotEmpty()) {
queue.poll().run {
this.includedModules.forEach {
queue.offer(it)
}
flattenedModules += this
}
}
return flattenedModules.toSet()
} |
203 modules with a statistical deep of 10 :
I think without 'tailrec' and using 'MutableSet' is a better choice 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) }
} |
in this case, I would tend to say that tail rec is not mandatory. The call graph is not big enough in memory I guess 🤔 Instead of overriding the current function, we can write a second one and use it for a moment. We need to observe the gain. Some of you could help make test on a beta version of Koin 3.5? |
No problem, Do I need to recommit the 'no tailrec' version of the code? |
yep, update your proposal with best no tailrec approach 👍 |
…nd using 'MutableSet'
ok,is updated |
PR #1643 has been merged. Can you update your PR a last time? |
# Conflicts: # core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt
How should I update PR |
that's ok, I thought there were remaining conflicts 👍 |
Good job, and thanks all! |
The module flattening function can reduce GC using MutableSet
If "tailrec" is not necessary, it can be more concise and efficient
no tailrec + use MutableSet Fun: