perf: reuse successful cover lookups per index
This commit is contained in:
@@ -20,6 +20,10 @@ package org.oxycblt.musikr.covers
|
|||||||
|
|
||||||
import android.os.ParcelFileDescriptor
|
import android.os.ParcelFileDescriptor
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
import kotlinx.coroutines.Deferred
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.coroutineScope
|
||||||
import org.oxycblt.musikr.fs.File
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
import org.oxycblt.musikr.metadata.Metadata
|
||||||
|
|
||||||
@@ -41,6 +45,20 @@ interface Covers<T : Cover> {
|
|||||||
suspend fun obtain(id: String): CoverResult<T>
|
suspend fun obtain(id: String): CoverResult<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A per-run successful-hit cache that shares in-flight lookups and retries misses. */
|
||||||
|
internal class CachedCovers<T : Cover>(private val delegate: Covers<T>) : Covers<T> {
|
||||||
|
private val lookups = ConcurrentHashMap<String, Deferred<CoverResult<T>>>()
|
||||||
|
|
||||||
|
override suspend fun obtain(id: String): CoverResult<T> = coroutineScope {
|
||||||
|
val lookup = lookups.computeIfAbsent(id) { async { delegate.obtain(id) } }
|
||||||
|
val result = lookup.await()
|
||||||
|
if (result is CoverResult.Miss) {
|
||||||
|
lookups.remove(id, lookup)
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An mutable repoistory for cover information.
|
* An mutable repoistory for cover information.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.oxycblt.musikr.Config
|
|||||||
import org.oxycblt.musikr.Storage
|
import org.oxycblt.musikr.Storage
|
||||||
import org.oxycblt.musikr.cache.CacheResult
|
import org.oxycblt.musikr.cache.CacheResult
|
||||||
import org.oxycblt.musikr.cache.CachedFile
|
import org.oxycblt.musikr.cache.CachedFile
|
||||||
|
import org.oxycblt.musikr.covers.CachedCovers
|
||||||
import org.oxycblt.musikr.covers.CoverResult
|
import org.oxycblt.musikr.covers.CoverResult
|
||||||
import org.oxycblt.musikr.fs.FS
|
import org.oxycblt.musikr.fs.FS
|
||||||
import org.oxycblt.musikr.fs.File
|
import org.oxycblt.musikr.fs.File
|
||||||
@@ -59,6 +60,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
|
|||||||
explored: Channel<Explored>,
|
explored: Channel<Explored>,
|
||||||
playlistFiles: MutableList<File>,
|
playlistFiles: MutableList<File>,
|
||||||
): Deferred<Result<Unit>> {
|
): Deferred<Result<Unit>> {
|
||||||
|
val cachedCovers = CachedCovers(storage.covers)
|
||||||
val files = Channel<File>(Channel.UNLIMITED)
|
val files = Channel<File>(Channel.UNLIMITED)
|
||||||
val filesTask = fs.explore(files)
|
val filesTask = fs.explore(files)
|
||||||
|
|
||||||
@@ -94,7 +96,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
|
|||||||
val audio = item.cachedFile.audio ?: return@mapParallel Finalized(NotAudio)
|
val audio = item.cachedFile.audio ?: return@mapParallel Finalized(NotAudio)
|
||||||
val coverId =
|
val coverId =
|
||||||
when (
|
when (
|
||||||
val result = audio.coverId?.let { id -> storage.covers.obtain(id) }
|
val result = audio.coverId?.let { id -> cachedCovers.obtain(id) }
|
||||||
) {
|
) {
|
||||||
is CoverResult.Hit -> result.cover
|
is CoverResult.Hit -> result.cover
|
||||||
is CoverResult.Miss ->
|
is CoverResult.Miss ->
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Auxio Project
|
||||||
|
* CachedCoversTest.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.covers
|
||||||
|
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Assert.assertSame
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class CachedCoversTest {
|
||||||
|
@Test
|
||||||
|
fun obtain_sameId_reusesHit() = runTest {
|
||||||
|
val cover = mockk<Cover>()
|
||||||
|
val delegate = mockk<Covers<Cover>>()
|
||||||
|
coEvery { delegate.obtain("shared") } returns CoverResult.Hit(cover)
|
||||||
|
val cached = CachedCovers(delegate)
|
||||||
|
|
||||||
|
val first = cached.obtain("shared") as CoverResult.Hit<Cover>
|
||||||
|
val second = cached.obtain("shared") as CoverResult.Hit<Cover>
|
||||||
|
|
||||||
|
assertSame(cover, first.cover)
|
||||||
|
assertSame(cover, second.cover)
|
||||||
|
coVerify(exactly = 1) { delegate.obtain("shared") }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user