From 23419cc35ef4177047390e2cb7f13811ff9faba3 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 13 Nov 2026 14:46:44 -0500 Subject: [PATCH] perf: coalesce indexing progress updates --- .../main/java/org/oxycblt/musikr/Musikr.kt | 53 +++++++++++++++++-- .../musikr/IndexingProgressCoalescerTest.kt | 48 +++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 musikr/src/test/java/org/oxycblt/musikr/IndexingProgressCoalescerTest.kt diff --git a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt index f53c3dca9..7b376c64d 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt @@ -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(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() diff --git a/musikr/src/test/java/org/oxycblt/musikr/IndexingProgressCoalescerTest.kt b/musikr/src/test/java/org/oxycblt/musikr/IndexingProgressCoalescerTest.kt new file mode 100644 index 000000000..a4f54df47 --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/IndexingProgressCoalescerTest.kt @@ -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 . + */ + +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() + 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(first), emitted) + + coalescer.flush() + + assertEquals(listOf(first, latest), emitted) + } +}