-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unneeded values from state when expected types are specified.
Optimize reduce graph algorithm. Add benchmarks for core functions Small fixes
- Loading branch information
Showing
14 changed files
with
293 additions
and
96 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
12 changes: 6 additions & 6 deletions
12
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/Reduce.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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package flank.exection.parallel | ||
|
||
import flank.exection.parallel.internal.initialValidators | ||
import flank.exection.parallel.internal.contextValidators | ||
import flank.exection.parallel.internal.reduceTo | ||
|
||
/** | ||
* Reduce given [Tasks] by [select] types to remove unneeded tasks from the graph. | ||
* Reduce given [Tasks] by [expected] types to remove unneeded tasks from the graph. | ||
* The returned graph will hold only tasks that are returning selected types, their dependencies and derived dependencies. | ||
* Additionally this is keeping also the validators for initial state. | ||
* | ||
* @return Reduced [Tasks] | ||
*/ | ||
operator fun Tasks.invoke( | ||
select: Set<Parallel.Type<*>> | ||
expected: Set<Parallel.Type<*>> | ||
): Tasks = | ||
reduceTo(select + initialValidators) | ||
reduceTo(expected + contextValidators()) | ||
|
||
/** | ||
* Shortcut for tasks reducing. | ||
*/ | ||
operator fun Tasks.invoke( | ||
vararg returns: Parallel.Type<*> | ||
): Tasks = invoke(returns.toSet()) | ||
vararg expected: Parallel.Type<*> | ||
): Tasks = invoke(expected.toSet()) |
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
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
52 changes: 16 additions & 36 deletions
52
tool/execution/parallel/src/main/kotlin/flank/exection/parallel/internal/Reduce.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 |
---|---|---|
@@ -1,42 +1,22 @@ | ||
package flank.exection.parallel.internal | ||
|
||
import flank.exection.parallel.Parallel | ||
import flank.exection.parallel.Parallel.Task | ||
import flank.exection.parallel.Parallel.Type | ||
import flank.exection.parallel.Tasks | ||
import flank.exection.parallel.internal.graph.findDependenciesIn | ||
|
||
internal infix fun Tasks.reduceTo( | ||
selectedTypes: Set<Parallel.Type<*>> | ||
): Tasks = | ||
filter { task -> task.type in selectedTypes } | ||
.toSet() | ||
.apply { | ||
val notFound = selectedTypes - map(Parallel.Task<*>::type) | ||
if (notFound.isNotEmpty()) throw Exception("Cannot reduce find tasks for the following types: $notFound") | ||
expected: Set<Type<*>> | ||
): Tasks { | ||
val notFound = expected - map(Task<*>::type) | ||
if (notFound.isNotEmpty()) throw Exception("Cannot find tasks for the following types: $notFound") | ||
val graph: Map<Type<*>, Set<Type<*>>> = associate { task -> task.type to task.args } | ||
val dependencies = expected.findDependenciesIn(graph) | ||
return mapNotNull { task -> | ||
when (task.type) { | ||
in expected -> task | ||
in dependencies -> task.copy(expected = false) | ||
else -> null | ||
} | ||
.reduce(this) | ||
|
||
/** | ||
* Reduce [all] steps to given receiver steps and their dependencies. | ||
* | ||
* @receiver The task selector for current reducing step. | ||
* @param all The list of all tasks that are under reducing. | ||
* @param acc Currently accumulated tasks. | ||
* @return Accumulated tasks if selector is empty. | ||
*/ | ||
private tailrec fun Tasks.reduce( | ||
all: Tasks, | ||
acc: Tasks = | ||
if (isEmpty()) all | ||
else emptySet(), | ||
): Tasks = | ||
when { | ||
isEmpty() -> acc | ||
else -> flatMap(Parallel.Task<*>::args) | ||
.mapNotNull(all::findByType) | ||
.toSet() | ||
.reduce(all, acc + this) | ||
} | ||
|
||
private fun Tasks.findByType( | ||
type: Parallel.Type<*> | ||
): Parallel.Task<*>? = | ||
find { task -> task.type == type } | ||
}.toSet() | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...ution/parallel/src/main/kotlin/flank/exection/parallel/internal/graph/FindDependencies.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,22 @@ | ||
package flank.exection.parallel.internal.graph | ||
|
||
/** | ||
* Find dependencies for given nodes in [graph]. | ||
* | ||
* @receiver Expected elements for current iteration. | ||
* @param graph Graph to search. | ||
* @param acc Currently accumulated elements. | ||
* @return Set expected elements along with dependencies. | ||
*/ | ||
internal tailrec fun <T> Set<T>.findDependenciesIn( | ||
graph: Map<T, Set<T>>, | ||
acc: Set<T> = | ||
if (isEmpty()) graph.keys | ||
else emptySet(), | ||
): Set<T> = | ||
when { | ||
isEmpty() -> acc // No more elements, so return all accumulated. | ||
else -> flatMap(graph::getValue).toSet() // Map each expected element to its dependencies. | ||
.minus(acc) // Remove already accumulated elements to optimize calculations. | ||
.findDependenciesIn(graph, acc + this) // Accumulate current dependencies and run next iteration. | ||
} |
Oops, something went wrong.