perf: move indexing relays off main thread
This commit is contained in:
@@ -21,11 +21,15 @@ package org.oxycblt.musikr
|
||||
import android.content.Context
|
||||
import android.os.SystemClock
|
||||
import java.util.Collections
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.oxycblt.musikr.fs.File
|
||||
import org.oxycblt.musikr.pipeline.EvaluateStep
|
||||
import org.oxycblt.musikr.pipeline.ExploreStep
|
||||
@@ -116,6 +120,7 @@ sealed interface IndexingProgress {
|
||||
internal class IndexingProgressCoalescer(
|
||||
private val intervalMs: Long = 250,
|
||||
private val clock: () -> Long = SystemClock::elapsedRealtime,
|
||||
private val emissionContext: CoroutineContext = EmptyCoroutineContext,
|
||||
private val onProgress: suspend (IndexingProgress) -> Unit,
|
||||
) {
|
||||
private val mutex = Mutex()
|
||||
@@ -134,7 +139,7 @@ internal class IndexingProgressCoalescer(
|
||||
}
|
||||
IndexingProgress.Indeterminate -> {
|
||||
flushLatest()
|
||||
onProgress(progress)
|
||||
emit(progress)
|
||||
lastEmissionMs = clock()
|
||||
}
|
||||
}
|
||||
@@ -144,10 +149,13 @@ internal class IndexingProgressCoalescer(
|
||||
|
||||
private suspend fun flushLatest() {
|
||||
val progress = latestSongs ?: return
|
||||
onProgress(progress)
|
||||
emit(progress)
|
||||
latestSongs = null
|
||||
lastEmissionMs = clock()
|
||||
}
|
||||
|
||||
private suspend fun emit(progress: IndexingProgress) =
|
||||
withContext(emissionContext) { onProgress(progress) }
|
||||
}
|
||||
|
||||
private class MusikrImpl(
|
||||
@@ -157,19 +165,25 @@ private class MusikrImpl(
|
||||
private val evaluateStep: EvaluateStep,
|
||||
) : Musikr {
|
||||
override suspend fun run(onProgress: suspend (IndexingProgress) -> Unit) = coroutineScope {
|
||||
val progressCoalescer = IndexingProgressCoalescer(onProgress = onProgress)
|
||||
val progressCoalescer =
|
||||
IndexingProgressCoalescer(
|
||||
emissionContext = Dispatchers.Main,
|
||||
onProgress = onProgress,
|
||||
)
|
||||
onProgress(IndexingProgress.Songs(0, 0))
|
||||
var explored = 0
|
||||
var loaded = 0
|
||||
val explored = AtomicInteger()
|
||||
val loaded = AtomicInteger()
|
||||
val exploredChannel = Channel<Explored>(Channel.UNLIMITED)
|
||||
val playlistFiles = Collections.synchronizedList(mutableListOf<File>())
|
||||
val exploredTask = exploreStep.explore(this, exploredChannel, playlistFiles)
|
||||
val trackedExploredChannel = Channel<Explored>(Channel.UNLIMITED)
|
||||
val trackedExploredTask =
|
||||
tryAsyncWith(trackedExploredChannel, Dispatchers.Main) {
|
||||
tryAsyncWith(trackedExploredChannel, Dispatchers.Default) {
|
||||
for (item in exploredChannel) {
|
||||
explored++
|
||||
progressCoalescer.submit(IndexingProgress.Songs(loaded, explored))
|
||||
val exploredNow = explored.incrementAndGet()
|
||||
progressCoalescer.submit(
|
||||
IndexingProgress.Songs(loaded.get(), exploredNow)
|
||||
)
|
||||
trackedExploredChannel.send(item)
|
||||
}
|
||||
progressCoalescer.flush()
|
||||
@@ -178,10 +192,12 @@ private class MusikrImpl(
|
||||
val extractedTask = extractStep.extract(this, trackedExploredChannel, extractedChannel)
|
||||
val trackedExtractedChannel = Channel<Extracted>(Channel.UNLIMITED)
|
||||
val trackedExtractedTask =
|
||||
tryAsyncWith(trackedExtractedChannel, Dispatchers.Main) {
|
||||
tryAsyncWith(trackedExtractedChannel, Dispatchers.Default) {
|
||||
for (item in extractedChannel) {
|
||||
loaded++
|
||||
progressCoalescer.submit(IndexingProgress.Songs(loaded, explored))
|
||||
val loadedNow = loaded.incrementAndGet()
|
||||
progressCoalescer.submit(
|
||||
IndexingProgress.Songs(loadedNow, explored.get())
|
||||
)
|
||||
trackedExtractedChannel.send(item)
|
||||
}
|
||||
progressCoalescer.flush()
|
||||
|
||||
@@ -115,7 +115,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
|
||||
}
|
||||
}
|
||||
val mergeTask =
|
||||
scope.tryAsyncWith(explored, Dispatchers.Main) {
|
||||
scope.tryAsyncWith(explored, Dispatchers.Default) {
|
||||
for (item in finalized) {
|
||||
it.send(item.explored)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
package org.oxycblt.musikr
|
||||
|
||||
import java.util.concurrent.Executors
|
||||
import kotlinx.coroutines.asCoroutineDispatcher
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
@@ -65,4 +67,27 @@ class IndexingProgressCoalescerTest {
|
||||
|
||||
assertEquals(listOf(first, latest, IndexingProgress.Indeterminate), emitted)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun submit_usesConfiguredEmissionContext() = runTest {
|
||||
val executor =
|
||||
Executors.newSingleThreadExecutor { task -> Thread(task, "progress-test") }
|
||||
val emissionContext = executor.asCoroutineDispatcher()
|
||||
try {
|
||||
var callbackThread: String? = null
|
||||
val coalescer =
|
||||
IndexingProgressCoalescer(
|
||||
intervalMs = 250,
|
||||
clock = { 0L },
|
||||
emissionContext = emissionContext,
|
||||
onProgress = { callbackThread = Thread.currentThread().name },
|
||||
)
|
||||
|
||||
coalescer.submit(IndexingProgress.Songs(loaded = 0, explored = 1))
|
||||
|
||||
assertEquals("progress-test", callbackThread)
|
||||
} finally {
|
||||
emissionContext.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user