perf: eliminate O(n²) queue merges, N+1 refresh queries, and dead schema columns

- QueueBuilder: convert List.contains to HashSet lookups in mergeWithExistingQueue
  and mergeWithExistingQueueDeterministic (O(n²) → O(n))
- AlbumRefreshWorker: load existing folder URIs once per folder with
  getExistingWallpaperUris() instead of one DB query per scanned wallpaper (N+1 → 1)
- WallpaperEntity: remove unused cropOffsetX/Y/Scale columns (always null, never read)
- DATABASE_VERSION bumped to 4; fallbackToDestructiveMigration handles upgrade

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Anthonyy232andClaude Sonnet 4.6 committed 2026-03-28 13:49:49 -07:00
1 parent 4565893787
commit 8e4b0771c8
7 files changed
+30 -26

No files matched your search

@@ -6,7 +6,7 @@ package com.anthonyla.paperize.core.constants
object Constants {
// Database
const val DATABASE_NAME = "paperize_database"
const val DATABASE_VERSION = 3 // v3: Added wallpaper_current table for reapply-effects support
const val DATABASE_VERSION = 4 // v4: Removed unused cropOffsetX/Y/Scale columns from wallpapers table
// DataStore
const val PREFERENCES_NAME = "paperize_preferences"
@@ -187,7 +187,7 @@ object PreferenceKeys {
const val LIVE_ENABLE_GRAYSCALE = "live_enable_grayscale"
const val LIVE_GRAYSCALE = "live_grayscale"
const val LIVE_ENABLE_DOUBLE_TAP = "live_enable_double_tap"
const val LIVE_ENABLE_CHANGE_ON_SCREEN_OFF = "live_enable_change_on_screen_on"
const val LIVE_ENABLE_CHANGE_ON_SCREEN_OFF = "live_enable_change_on_screen_off"
const val LIVE_ENABLE_PARALLAX = "live_enable_parallax"
const val LIVE_PARALLAX_INTENSITY = "live_parallax_intensity"
@@ -38,13 +38,10 @@ object QueueBuilder {
existingQueueIds: List<String>,
allWallpaperIds: List<String>
): List<String> {
// Filter existing queue to only include IDs that still exist
val existingValid = existingQueueIds.filter { it in allWallpaperIds }
// Find new wallpapers not in existing queue
val newWallpaperIds = allWallpaperIds.filter { it !in existingQueueIds }
// Return existing order with new wallpapers shuffled at the end
val allSet = allWallpaperIds.toHashSet()
val existingSet = existingQueueIds.toHashSet()
val existingValid = existingQueueIds.filter { it in allSet }
val newWallpaperIds = allWallpaperIds.filter { it !in existingSet }
return existingValid + newWallpaperIds.shuffled()
}
@@ -56,8 +53,10 @@ object QueueBuilder {
existingQueueIds: List<String>,
allWallpaperIds: List<String>
): List<String> {
val existingValid = existingQueueIds.filter { it in allWallpaperIds }
val newWallpaperIds = allWallpaperIds.filter { it !in existingQueueIds }
val allSet = allWallpaperIds.toHashSet()
val existingSet = existingQueueIds.toHashSet()
val existingValid = existingQueueIds.filter { it in allSet }
val newWallpaperIds = allWallpaperIds.filter { it !in existingSet }
return existingValid + newWallpaperIds
}
@@ -122,6 +122,12 @@ interface WallpaperDao {
@Query("SELECT COUNT(*) FROM wallpapers WHERE albumId = :albumId")
suspend fun getWallpaperCountByAlbum(albumId: String): Int
/**
* Get all URIs for wallpapers in a folder — used for duplicate detection during folder rescan.
*/
@Query("SELECT uri FROM wallpapers WHERE albumId = :albumId AND folderId = :folderId")
suspend fun getWallpaperUrisByAlbumAndFolder(albumId: String, folderId: String): List<String>
/**
* Get wallpaper IDs only for queue building (shuffle mode).
* Avoids loading full entities when only IDs are needed.
@@ -52,17 +52,5 @@ data class WallpaperEntity(
val sourceType: WallpaperSourceType = WallpaperSourceType.DIRECT,
val addedAt: Long = System.currentTimeMillis(),
/**
* Media type (IMAGE)
* Added in database version 2 for live wallpaper support
*/
val mediaType: WallpaperMediaType = WallpaperMediaType.IMAGE,
/**
* Manual crop parameters for live wallpaper rendering
* Added in database version 2
*/
val cropOffsetX: Float? = null,
val cropOffsetY: Float? = null,
val cropScale: Float? = null
)
@@ -308,4 +308,7 @@ class WallpaperRepositoryImpl @Inject constructor(
override suspend fun isWallpaperInAlbum(albumId: String, uri: String): Boolean =
wallpaperDao.getWallpaperByUri(uri)?.let { it.albumId == albumId } ?: false
override suspend fun getExistingWallpaperUris(albumId: String, folderId: String): Set<String> =
wallpaperDao.getWallpaperUrisByAlbumAndFolder(albumId, folderId).toHashSet()
}
@@ -119,4 +119,10 @@ interface WallpaperRepository {
* Optimized for checking duplicates during folder sync
*/
suspend fun isWallpaperInAlbum(albumId: String, uri: String): Boolean
/**
* Get all existing wallpaper URIs for a specific folder within an album.
* Used for bulk duplicate detection during folder rescan (avoids N+1 queries).
*/
suspend fun getExistingWallpaperUris(albumId: String, folderId: String): Set<String>
}
@@ -76,19 +76,21 @@ class AlbumRefreshWorker @AssistedInject constructor(
coroutineScope {
album.folders.map { folder ->
async {
// Load existing URIs for this folder once — avoids one DB query per scanned wallpaper
val existingUris = wallpaperRepository.getExistingWallpaperUris(album.id, folder.id)
when (val scanResult = wallpaperRepository.scanFolderForWallpapers(android.net.Uri.parse(folder.uri))) {
is CoreResult.Success -> {
val scannedWallpapers = scanResult.data
// Find and add new wallpapers not already in album
scannedWallpapers.forEach { wallpaper ->
// Memory-efficient check: check database for existing URI in this album
if (!wallpaperRepository.isWallpaperInAlbum(album.id, wallpaper.uri)) {
if (wallpaper.uri !in existingUris) {
val wallpaperToAdd = wallpaper.copy(
albumId = album.id,
folderId = folder.id
)
when (wallpaperRepository.addWallpaper(wallpaperToAdd)) {
is CoreResult.Success -> {
totalAdded.incrementAndGet()