perf: coalesce indexing progress updates

This commit is contained in:
jay
2026-11-13 14:46:44 -05:00
parent bb0d0ebbde
commit 23419cc35e
2 changed files with 97 additions and 4 deletions
@@ -19,10 +19,13 @@
package org.oxycblt.musikr
import android.content.Context
import android.os.SystemClock
import java.util.Collections
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.oxycblt.musikr.fs.File
import org.oxycblt.musikr.pipeline.EvaluateStep
import org.oxycblt.musikr.pipeline.ExploreStep
@@ -50,7 +53,8 @@ interface Musikr {
* Start loading music using the given config and the configuration provided earlier.
*
* @param onProgress Optional callback to receive progress on the current status of the music
* pipeline. Warning: These events will be rapid-fire.
* pipeline. Intermediate song counts are coalesced, while terminal states are delivered
* promptly.
* @return A handle to the newly created library alongside further cleanup.
*/
suspend fun run(onProgress: suspend (IndexingProgress) -> Unit = {}): LibraryResult
@@ -108,6 +112,44 @@ sealed interface IndexingProgress {
data object Indeterminate : IndexingProgress
}
/** Coalesces intermediate counts while retaining exact final and terminal progress. */
internal class IndexingProgressCoalescer(
private val intervalMs: Long = 250,
private val clock: () -> Long = SystemClock::elapsedRealtime,
private val onProgress: suspend (IndexingProgress) -> Unit,
) {
private val mutex = Mutex()
private var latestSongs: IndexingProgress.Songs? = null
private var lastEmissionMs: Long? = null
suspend fun submit(progress: IndexingProgress) =
mutex.withLock {
when (progress) {
is IndexingProgress.Songs -> {
latestSongs = progress
val lastEmission = lastEmissionMs
if (lastEmission == null || clock() - lastEmission >= intervalMs) {
flushLatest()
}
}
IndexingProgress.Indeterminate -> {
flushLatest()
onProgress(progress)
lastEmissionMs = clock()
}
}
}
suspend fun flush() = mutex.withLock { flushLatest() }
private suspend fun flushLatest() {
val progress = latestSongs ?: return
onProgress(progress)
latestSongs = null
lastEmissionMs = clock()
}
}
private class MusikrImpl(
private val config: Config,
private val exploreStep: ExploreStep,
@@ -115,6 +157,7 @@ private class MusikrImpl(
private val evaluateStep: EvaluateStep,
) : Musikr {
override suspend fun run(onProgress: suspend (IndexingProgress) -> Unit) = coroutineScope {
val progressCoalescer = IndexingProgressCoalescer(onProgress = onProgress)
onProgress(IndexingProgress.Songs(0, 0))
var explored = 0
var loaded = 0
@@ -126,9 +169,10 @@ private class MusikrImpl(
tryAsyncWith(trackedExploredChannel, Dispatchers.Main) {
for (item in exploredChannel) {
explored++
onProgress(IndexingProgress.Songs(loaded, explored))
progressCoalescer.submit(IndexingProgress.Songs(loaded, explored))
trackedExploredChannel.send(item)
}
progressCoalescer.flush()
}
val extractedChannel = Channel<Extracted>(Channel.UNLIMITED)
val extractedTask = extractStep.extract(this, trackedExploredChannel, extractedChannel)
@@ -137,10 +181,11 @@ private class MusikrImpl(
tryAsyncWith(trackedExtractedChannel, Dispatchers.Main) {
for (item in extractedChannel) {
loaded++
onProgress(IndexingProgress.Songs(loaded, explored))
progressCoalescer.submit(IndexingProgress.Songs(loaded, explored))
trackedExtractedChannel.send(item)
}
onProgress(IndexingProgress.Indeterminate)
progressCoalescer.flush()
progressCoalescer.submit(IndexingProgress.Indeterminate)
}
val library = evaluateStep.evaluate(trackedExtractedChannel, playlistFiles)
merge(exploredTask, extractedTask, trackedExploredTask, trackedExtractedTask).await()
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2026 Auxio Project
* IndexingProgressCoalescerTest.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.musikr
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
class IndexingProgressCoalescerTest {
@Test
fun submit_burstDuringInterval_emitsFirstAndLatestOnFlush() = runTest {
val emitted = mutableListOf<IndexingProgress>()
val coalescer =
IndexingProgressCoalescer(
intervalMs = 250,
clock = { 0L },
onProgress = { emitted.add(it) },
)
val first = IndexingProgress.Songs(loaded = 0, explored = 1)
val latest = IndexingProgress.Songs(loaded = 2, explored = 3)
coalescer.submit(first)
coalescer.submit(IndexingProgress.Songs(loaded = 1, explored = 2))
coalescer.submit(latest)
assertEquals(listOf<IndexingProgress>(first), emitted)
coalescer.flush()
assertEquals(listOf(first, latest), emitted)
}
}