perf: avoid locking initialized cache-map reads

This commit is contained in:
jay
2026-11-13 18:05:22 -05:00
parent 858174e695
commit d2e9eacc03
2 changed files with 81 additions and 4 deletions
@@ -37,13 +37,16 @@ import org.oxycblt.musikr.tag.parse.ParsedTags
* Create an instance with [from]. * Create an instance with [from].
*/ */
class DBCache private constructor(private val readDao: CacheReadDao) : Cache { class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
private var mapping: Map<Uri, CachedFileData>? = null // The initialized map is immutable, so volatile publication is sufficient for cache hits.
@Volatile private var mapping: Map<Uri, CachedFileData>? = null
private val mappingLock = Mutex() private val mappingLock = Mutex()
override suspend fun read(file: File): CacheResult { override suspend fun read(file: File): CacheResult {
val currentMapping = mappingLock.withLock { val currentMapping =
mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it } mapping
} ?: mappingLock.withLock {
mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it }
}
val dbSong = currentMapping[file.uri] ?: return CacheResult.Miss(file) val dbSong = currentMapping[file.uri] ?: return CacheResult.Miss(file)
if (dbSong.modifiedMs != file.modifiedMs) { if (dbSong.modifiedMs != file.modifiedMs) {
return CacheResult.Stale(file, dbSong.addedMs) return CacheResult.Stale(file, dbSong.addedMs)
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2026 Auxio Project
* DBCacheTest.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.cache.db
import android.net.Uri
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.async
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.yield
import org.junit.Assert.assertTrue
import org.junit.Test
import org.oxycblt.musikr.fs.File
class DBCacheTest {
@Test
fun read_initializedMapping_doesNotWaitForMappingLock() = runTest {
val firstUri = mockk<Uri>()
val secondUri = mockk<Uri>()
val readDao = mockk<CacheReadDao>()
coEvery { readDao.selectAllSongs() } returns
listOf(cachedData(firstUri), cachedData(secondUri))
val cache = DBCache.from(readDao)
cache.read(file(firstUri))
val field = DBCache::class.java.getDeclaredField("mappingLock")
field.isAccessible = true
val lock = field.get(cache) as Mutex
lock.lock()
val secondRead = async { cache.read(file(secondUri)) }
try {
yield()
assertTrue(secondRead.isCompleted)
} finally {
lock.unlock()
}
secondRead.await()
}
private fun cachedData(uri: Uri): CachedFileData {
val data = mockk<CachedFileData>(relaxed = true)
every { data.uri } returns uri
every { data.modifiedMs } returns 0L
every { data.addedMs } returns 0L
every { data.mimeType } returns null
return data
}
private fun file(uri: Uri): File {
val file = mockk<File>(relaxed = true)
every { file.uri } returns uri
every { file.modifiedMs } returns 0L
return file
}
}