Skip to content

Commit c5a7a5d

Browse files
committed
Re-enable parallel map layer loading
Signed-off-by: Kyle Corry <[email protected]>
1 parent ad139e6 commit c5a7a5d

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.kylecorry.trail_sense.shared.andromeda_temp
2+
3+
import kotlinx.coroutines.coroutineScope
4+
import kotlinx.coroutines.launch
5+
import kotlinx.coroutines.sync.Semaphore
6+
import kotlinx.coroutines.sync.withPermit
7+
8+
class ParallelCoroutineRunner2(maxParallel: Int = 8) {
9+
10+
private val semaphore = Semaphore(maxParallel)
11+
12+
suspend fun run(coroutines: List<suspend () -> Any>) = coroutineScope {
13+
for (coroutine in coroutines) {
14+
launch {
15+
semaphore.withPermit { coroutine() }
16+
}
17+
}
18+
}
19+
20+
suspend fun <R> run(items: List<R>, coroutine: suspend (R) -> Unit) {
21+
run(items.map { { coroutine(it) } })
22+
}
23+
24+
suspend fun <T> map(coroutines: List<suspend () -> T>): List<T> {
25+
val items = mutableListOf<Pair<Int, T>>()
26+
val lock = Any()
27+
28+
run(coroutines.mapIndexed { index, coroutine ->
29+
{
30+
val item = coroutine()
31+
synchronized(lock) {
32+
items.add(index to item)
33+
}
34+
}
35+
})
36+
37+
return items.sortedBy { it.first }.map { it.second }
38+
}
39+
40+
suspend fun <T> mapFunctions(functions: List<() -> T>): List<T> {
41+
return map(functions.map { { it() } })
42+
}
43+
44+
suspend fun <R, T> map(items: List<R>, coroutine: suspend (R) -> T): List<T> {
45+
return map(items.map { { coroutine(it) } })
46+
}
47+
48+
}

app/src/main/java/com/kylecorry/trail_sense/shared/map_layers/MapLayerBackgroundTask.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.kylecorry.andromeda.core.cache.AppServiceRegistry
44
import com.kylecorry.luna.coroutines.CoroutineQueueRunner
55
import com.kylecorry.luna.coroutines.ParallelCoroutineRunner
66
import com.kylecorry.sol.science.geology.CoordinateBounds
7+
import com.kylecorry.trail_sense.shared.andromeda_temp.ParallelCoroutineRunner2
78
import com.kylecorry.trail_sense.shared.andromeda_temp.grow
89
import com.kylecorry.trail_sense.shared.device.DeviceSubsystem
910
import com.kylecorry.trail_sense.shared.map_layers.tiles.TileMath
@@ -62,12 +63,8 @@ class MapLayerBackgroundTask {
6263
val taskCopy = synchronized(taskLock) {
6364
tasks.toList()
6465
}
65-
for (task in taskCopy) {
66-
task(bounds, metersPerPixel)
67-
}
68-
// TODO: Parallel execution is not working properly - I think it's cancellation related
69-
// val parallel = ParallelCoroutineRunner()
70-
// parallel.run(taskCopy.map { { it(bounds, metersPerPixel) } })
66+
val parallel = ParallelCoroutineRunner2()
67+
parallel.run(taskCopy.map { { it(bounds, metersPerPixel) } })
7168
}
7269
) {
7370

app/src/main/java/com/kylecorry/trail_sense/shared/map_layers/MapLayerBackgroundTask2.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package com.kylecorry.trail_sense.shared.map_layers
22

33
import com.kylecorry.andromeda.core.cache.AppServiceRegistry
44
import com.kylecorry.luna.coroutines.CoroutineQueueRunner
5+
import com.kylecorry.luna.coroutines.ParallelCoroutineRunner
56
import com.kylecorry.sol.math.geometry.Rectangle
67
import com.kylecorry.sol.science.geology.CoordinateBounds
8+
import com.kylecorry.trail_sense.shared.andromeda_temp.ParallelCoroutineRunner2
79
import com.kylecorry.trail_sense.shared.andromeda_temp.grow
810
import com.kylecorry.trail_sense.shared.device.DeviceSubsystem
911
import com.kylecorry.trail_sense.shared.map_layers.tiles.TileMath
@@ -64,12 +66,8 @@ class MapLayerBackgroundTask2 {
6466
val taskCopy = synchronized(taskLock) {
6567
tasks.toList()
6668
}
67-
for (task in taskCopy) {
68-
task(viewBounds, bounds, projection)
69-
}
70-
// TODO: Parallel execution is not working properly - I think it's cancellation related
71-
// val parallel = ParallelCoroutineRunner()
72-
// parallel.run(taskCopy.map { { it(viewBounds, bounds, projection) } })
69+
val parallel = ParallelCoroutineRunner2()
70+
parallel.run(taskCopy.map { { it(viewBounds, bounds, projection) } })
7371
}
7472
) {
7573

0 commit comments

Comments
 (0)