-
-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1801 from hoc081098/hoc081098-patch-1
flatten: use loop and ArrayDeque instead of recursion
- Loading branch information
Showing
5 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
Benchmark Mode Cnt Score Error Units | ||
BenchmarkClass.emptyStart avgt 10 ≈ 10⁻³ ms/op | ||
BenchmarkClass.start400 avgt 10 0,272 ± 0,045 ms/op | ||
BenchmarkClass.start400AndInject avgt 10 0,265 ± 0,022 ms/op | ||
BenchmarkClass.flattenIterative avgt 10 0.643 ± 0.112 ms/op | ||
BenchmarkClass.flattenRecursive avgt 10 1.297 ± 0.043 ms/op | ||
BenchmarkClass.start400 avgt 10 0.297 ± 0.053 ms/op | ||
BenchmarkClass.start400AndInject avgt 10 0.283 ± 0.007 ms/op |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
examples/jvm-perfs/src/main/kotlin/org/koin/benchmark/nestedModules.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.koin.benchmark | ||
|
||
import org.koin.core.annotation.KoinInternalApi | ||
import org.koin.core.module.Module | ||
import org.koin.dsl.module | ||
|
||
internal fun buildNestedModule( | ||
depth: Int, | ||
width: Int, | ||
): List<Module> { | ||
val matrix = List(depth) { List(width) { module {} } } | ||
|
||
val level0Index = matrix.indices.random() | ||
val level0 = matrix[level0Index] | ||
|
||
matrix.forEachIndexed { r, modules -> | ||
modules.forEachIndexed { c, module -> | ||
// Let each module include the one above it. | ||
matrix | ||
.getOrNull(r + 1) | ||
?.get(c) | ||
?.let { module.includes(it) } | ||
|
||
if (r != level0Index) { | ||
module.includes(level0) | ||
} | ||
} | ||
} | ||
|
||
return matrix.first() | ||
} | ||
|
||
@OptIn(KoinInternalApi::class) | ||
internal fun flattenIterative(modules: List<Module>): Set<Module> { | ||
// This is actually a DFS traversal of the module graph, | ||
// but we're using a stack instead of recursion to avoid stack overflows and performance overhead. | ||
|
||
val flatten = HashSet<Module>() | ||
val stack = ArrayDeque(modules) | ||
|
||
while (stack.isNotEmpty()) { | ||
val current = stack.removeLast() | ||
|
||
// If the module is already in the set, that means we've already visited it, so we can skip it. | ||
if (!flatten.add(current)) { | ||
continue | ||
} | ||
|
||
// Add all the included modules to the stack if they haven't been visited yet. | ||
for (module in current.includedModules) { | ||
if (module !in flatten) { | ||
stack += module | ||
} | ||
} | ||
} | ||
|
||
return flatten | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters