Revert "perf: move indexing relays off main thread"

This reverts commit af4829f32a.
This commit is contained in:
jay
2026-11-13 18:24:48 -05:00
parent af4829f32a
commit cc88b1e7eb
3 changed files with 12 additions and 53 deletions
@@ -21,15 +21,11 @@ package org.oxycblt.musikr
import android.content.Context import android.content.Context
import android.os.SystemClock import android.os.SystemClock
import java.util.Collections 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.Dispatchers
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.oxycblt.musikr.fs.File import org.oxycblt.musikr.fs.File
import org.oxycblt.musikr.pipeline.EvaluateStep import org.oxycblt.musikr.pipeline.EvaluateStep
import org.oxycblt.musikr.pipeline.ExploreStep import org.oxycblt.musikr.pipeline.ExploreStep
@@ -120,7 +116,6 @@ sealed interface IndexingProgress {
internal class IndexingProgressCoalescer( internal class IndexingProgressCoalescer(
private val intervalMs: Long = 250, private val intervalMs: Long = 250,
private val clock: () -> Long = SystemClock::elapsedRealtime, private val clock: () -> Long = SystemClock::elapsedRealtime,
private val emissionContext: CoroutineContext = EmptyCoroutineContext,
private val onProgress: suspend (IndexingProgress) -> Unit, private val onProgress: suspend (IndexingProgress) -> Unit,
) { ) {
private val mutex = Mutex() private val mutex = Mutex()
@@ -139,7 +134,7 @@ internal class IndexingProgressCoalescer(
} }
IndexingProgress.Indeterminate -> { IndexingProgress.Indeterminate -> {
flushLatest() flushLatest()
emit(progress) onProgress(progress)
lastEmissionMs = clock() lastEmissionMs = clock()
} }
} }
@@ -149,13 +144,10 @@ internal class IndexingProgressCoalescer(
private suspend fun flushLatest() { private suspend fun flushLatest() {
val progress = latestSongs ?: return val progress = latestSongs ?: return
emit(progress) onProgress(progress)
latestSongs = null latestSongs = null
lastEmissionMs = clock() lastEmissionMs = clock()
} }
private suspend fun emit(progress: IndexingProgress) =
withContext(emissionContext) { onProgress(progress) }
} }
private class MusikrImpl( private class MusikrImpl(
@@ -165,25 +157,19 @@ private class MusikrImpl(
private val evaluateStep: EvaluateStep, private val evaluateStep: EvaluateStep,
) : Musikr { ) : Musikr {
override suspend fun run(onProgress: suspend (IndexingProgress) -> Unit) = coroutineScope { override suspend fun run(onProgress: suspend (IndexingProgress) -> Unit) = coroutineScope {
val progressCoalescer = val progressCoalescer = IndexingProgressCoalescer(onProgress = onProgress)
IndexingProgressCoalescer(
emissionContext = Dispatchers.Main,
onProgress = onProgress,
)
onProgress(IndexingProgress.Songs(0, 0)) onProgress(IndexingProgress.Songs(0, 0))
val explored = AtomicInteger() var explored = 0
val loaded = AtomicInteger() var loaded = 0
val exploredChannel = Channel<Explored>(Channel.UNLIMITED) val exploredChannel = Channel<Explored>(Channel.UNLIMITED)
val playlistFiles = Collections.synchronizedList(mutableListOf<File>()) val playlistFiles = Collections.synchronizedList(mutableListOf<File>())
val exploredTask = exploreStep.explore(this, exploredChannel, playlistFiles) val exploredTask = exploreStep.explore(this, exploredChannel, playlistFiles)
val trackedExploredChannel = Channel<Explored>(Channel.UNLIMITED) val trackedExploredChannel = Channel<Explored>(Channel.UNLIMITED)
val trackedExploredTask = val trackedExploredTask =
tryAsyncWith(trackedExploredChannel, Dispatchers.Default) { tryAsyncWith(trackedExploredChannel, Dispatchers.Main) {
for (item in exploredChannel) { for (item in exploredChannel) {
val exploredNow = explored.incrementAndGet() explored++
progressCoalescer.submit( progressCoalescer.submit(IndexingProgress.Songs(loaded, explored))
IndexingProgress.Songs(loaded.get(), exploredNow)
)
trackedExploredChannel.send(item) trackedExploredChannel.send(item)
} }
progressCoalescer.flush() progressCoalescer.flush()
@@ -192,12 +178,10 @@ private class MusikrImpl(
val extractedTask = extractStep.extract(this, trackedExploredChannel, extractedChannel) val extractedTask = extractStep.extract(this, trackedExploredChannel, extractedChannel)
val trackedExtractedChannel = Channel<Extracted>(Channel.UNLIMITED) val trackedExtractedChannel = Channel<Extracted>(Channel.UNLIMITED)
val trackedExtractedTask = val trackedExtractedTask =
tryAsyncWith(trackedExtractedChannel, Dispatchers.Default) { tryAsyncWith(trackedExtractedChannel, Dispatchers.Main) {
for (item in extractedChannel) { for (item in extractedChannel) {
val loadedNow = loaded.incrementAndGet() loaded++
progressCoalescer.submit( progressCoalescer.submit(IndexingProgress.Songs(loaded, explored))
IndexingProgress.Songs(loadedNow, explored.get())
)
trackedExtractedChannel.send(item) trackedExtractedChannel.send(item)
} }
progressCoalescer.flush() progressCoalescer.flush()
@@ -115,7 +115,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
} }
} }
val mergeTask = val mergeTask =
scope.tryAsyncWith(explored, Dispatchers.Default) { scope.tryAsyncWith(explored, Dispatchers.Main) {
for (item in finalized) { for (item in finalized) {
it.send(item.explored) it.send(item.explored)
} }
@@ -18,8 +18,6 @@
package org.oxycblt.musikr package org.oxycblt.musikr
import java.util.concurrent.Executors
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Test import org.junit.Test
@@ -67,27 +65,4 @@ class IndexingProgressCoalescerTest {
assertEquals(listOf(first, latest, IndexingProgress.Indeterminate), emitted) 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()
}
}
} }