Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 421d656

Browse files
committed
Put depenencies on a Map<String, List<Dependency>> instead of List
This will save a filter per module, changing the required time from O((number of modules)*(number of dependencies)) to O(number of dependencies). In the case of large projects this could save some seconds. For example, creating a project with 1000 modules and a full topology takes ~193 seconds using a List, while using a Map takes ~189 seconds.
1 parent 8714177 commit 421d656

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

src/main/kotlin/com/google/androidstudiopoet/ModuleBlueprintFactory.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import com.google.androidstudiopoet.models.ModuleBlueprint
77

88
object ModuleBlueprintFactory {
99
fun create(index: Int, config: ConfigPOJO, projectRoot: String): ModuleBlueprint {
10-
val dependencies = config.resolvedDependencies
11-
.filter { it.from == index}
12-
.map { it.to }
10+
val dependencies = config.resolvedDependencies[index.toString()]?.map { it.to } ?: listOf()
1311

1412
val dependenciesNames = dependencies.map { getModuleNameByIndex(it) }
1513
val methodsToCallWithinModule = dependencies.map { getMethodToCallForDependency(it, config, projectRoot) }

src/main/kotlin/com/google/androidstudiopoet/models/ConfigPOJO.kt

+39-17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package com.google.androidstudiopoet.models
1616

1717
import com.google.gson.Gson
1818
import java.security.InvalidParameterException
19+
import java.util.function.BiFunction
20+
import kotlin.system.measureTimeMillis
1921

2022
class ConfigPOJO {
2123

@@ -82,26 +84,46 @@ class ConfigPOJO {
8284
val useKotlin: Boolean
8385
get() = kotlinPackageCount!!.toInt() > 0
8486

85-
val resolvedDependencies: List<Dependency> by lazy {
86-
val allDependencies : MutableSet<Dependency> = mutableSetOf()
87-
88-
// Add dependencies generated by topologies
89-
val givenTopologies = topologies
90-
if (givenTopologies != null) {
91-
for (parameters in givenTopologies) {
92-
val type = parameters.get("type") ?: throw InvalidParameterException("No type specified in topology $parameters")
93-
val topology : Topologies = Topologies.valueOf(type.toUpperCase())
94-
allDependencies.addAll(topology.generateDependencies(parameters, this))
95-
}
96-
}
87+
val resolvedDependencies: Map<String, List<Dependency>> by lazy {
88+
89+
val result : MutableMap<String, List<Dependency>> = mutableMapOf()
9790

98-
// Add explicit dependencies
99-
val explicitDependencies = dependencies
100-
if (explicitDependencies != null) {
101-
allDependencies.addAll(explicitDependencies)
91+
val allDependencies: MutableMap<String, MutableList<Dependency>> = mutableMapOf()
92+
// Add dependencies generated by topologies
93+
val givenTopologies = topologies
94+
if (givenTopologies != null) {
95+
for (parameters in givenTopologies) {
96+
val type = parameters.get("type") ?: throw InvalidParameterException("No type specified in topology $parameters")
97+
val topology: Topologies = Topologies.valueOf(type.toUpperCase())
98+
val currentDependencies = topology.generateDependencies(parameters, this)
99+
addDependencies(allDependencies, currentDependencies)
102100
}
101+
}
102+
103+
// Add explicit dependencies
104+
val explicitDependencies = dependencies
105+
if (explicitDependencies != null) {
106+
addDependencies(allDependencies, explicitDependencies)
107+
}
108+
109+
// Remove duplicates
110+
for (dependency in allDependencies) {
111+
result.put(dependency.key, dependency.value.distinct())
112+
}
113+
114+
result
115+
}
103116

104-
allDependencies.toList()
117+
private fun addDependencies(to: MutableMap<String, MutableList<Dependency>>, from: List<Dependency>) {
118+
for (dependency in from) {
119+
val key = dependency.from.toString()
120+
if ((!to.containsKey(key)) || to[key] == null) {
121+
to[key] = mutableListOf(dependency)
122+
}
123+
else {
124+
to[key]?.add(dependency)
125+
}
105126
}
127+
}
106128
}
107129

0 commit comments

Comments
 (0)