perf: run thumbnail generation in background on first load
On cache miss, the fetcher now returns the decoded bitmap immediately for display while saving the thumbnail to disk in a background coroutine. This means the first load is just as fast as the normal Coil pipeline — no blocking on resize + JPEG compress + disk write. The thumbnail is ready for the next load. https://claude.ai/code/session_01PBJS3HDrurLP3i5n4tMsCv
This commit is contained in:
@@ -448,6 +448,7 @@ class AppModules(
|
|||||||
blossomServerResolver = { blossomResolver },
|
blossomServerResolver = { blossomResolver },
|
||||||
callFactory = { okHttpClients.getHttpClient(roleBasedHttpClientBuilder.shouldUseTorForImageDownload(it)) },
|
callFactory = { okHttpClients.getHttpClient(roleBasedHttpClientBuilder.shouldUseTorForImageDownload(it)) },
|
||||||
thumbnailCache = thumbnailDiskCache,
|
thumbnailCache = thumbnailDiskCache,
|
||||||
|
backgroundScope = applicationIOScope,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -45,6 +45,7 @@ import coil3.video.VideoFrameDecoder
|
|||||||
import com.vitorpamplona.amethyst.isDebug
|
import com.vitorpamplona.amethyst.isDebug
|
||||||
import com.vitorpamplona.amethyst.service.uploads.blossom.bud10.BlossomServerResolver
|
import com.vitorpamplona.amethyst.service.uploads.blossom.bud10.BlossomServerResolver
|
||||||
import com.vitorpamplona.quartz.utils.Log
|
import com.vitorpamplona.quartz.utils.Log
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import okhttp3.Call
|
import okhttp3.Call
|
||||||
|
|
||||||
class ImageLoaderSetup {
|
class ImageLoaderSetup {
|
||||||
@@ -67,6 +68,7 @@ class ImageLoaderSetup {
|
|||||||
blossomServerResolver: () -> BlossomServerResolver,
|
blossomServerResolver: () -> BlossomServerResolver,
|
||||||
callFactory: (url: String) -> Call.Factory,
|
callFactory: (url: String) -> Call.Factory,
|
||||||
thumbnailCache: ThumbnailDiskCache,
|
thumbnailCache: ThumbnailDiskCache,
|
||||||
|
backgroundScope: CoroutineScope,
|
||||||
) {
|
) {
|
||||||
SingletonImageLoader.setUnsafe(
|
SingletonImageLoader.setUnsafe(
|
||||||
ImageLoader
|
ImageLoader
|
||||||
@@ -82,7 +84,7 @@ class ImageLoaderSetup {
|
|||||||
add(Base64Fetcher.Factory)
|
add(Base64Fetcher.Factory)
|
||||||
add(BlurHashFetcher.Factory)
|
add(BlurHashFetcher.Factory)
|
||||||
add(BlossomFetcher.Factory(blossomServerResolver, callFactory))
|
add(BlossomFetcher.Factory(blossomServerResolver, callFactory))
|
||||||
add(ProfilePictureFetcher.Factory(thumbnailCache, callFactory))
|
add(ProfilePictureFetcher.Factory(thumbnailCache, callFactory, backgroundScope))
|
||||||
add(Base64Fetcher.BKeyer)
|
add(Base64Fetcher.BKeyer)
|
||||||
add(BlurHashFetcher.BKeyer)
|
add(BlurHashFetcher.BKeyer)
|
||||||
add(ProfilePictureFetcher.BKeyer)
|
add(ProfilePictureFetcher.BKeyer)
|
||||||
|
|||||||
+56
-49
@@ -40,6 +40,8 @@ import coil3.network.NetworkFetcher
|
|||||||
import coil3.network.okhttp.asNetworkClient
|
import coil3.network.okhttp.asNetworkClient
|
||||||
import coil3.request.Options
|
import coil3.request.Options
|
||||||
import com.vitorpamplona.amethyst.commons.ui.components.PROFILE_PIC_SCHEME
|
import com.vitorpamplona.amethyst.commons.ui.components.PROFILE_PIC_SCHEME
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import okhttp3.Call
|
import okhttp3.Call
|
||||||
import kotlin.coroutines.cancellation.CancellationException
|
import kotlin.coroutines.cancellation.CancellationException
|
||||||
|
|
||||||
@@ -49,9 +51,9 @@ import kotlin.coroutines.cancellation.CancellationException
|
|||||||
* URLs are wrapped as `profilepic://https://example.com/pic.jpg` by the avatar composables.
|
* URLs are wrapped as `profilepic://https://example.com/pic.jpg` by the avatar composables.
|
||||||
* This fetcher:
|
* This fetcher:
|
||||||
* 1. Checks the thumbnail disk cache for a pre-resized JPEG (~5-10KB)
|
* 1. Checks the thumbnail disk cache for a pre-resized JPEG (~5-10KB)
|
||||||
* 2. On hit: returns the tiny file directly (fast disk read)
|
* 2. On hit: returns the tiny thumbnail directly (fast disk read)
|
||||||
* 3. On miss: downloads the original via NetworkFetcher, decodes + resizes to 256px,
|
* 3. On miss: returns the network result immediately for display, and generates
|
||||||
* saves the thumbnail to the cache, and returns the small bitmap
|
* the thumbnail in the background for next time
|
||||||
*
|
*
|
||||||
* The zoomable full-screen dialog uses the raw URL (without profilepic:// prefix),
|
* The zoomable full-screen dialog uses the raw URL (without profilepic:// prefix),
|
||||||
* which goes through the normal Coil pipeline for full-resolution display.
|
* which goes through the normal Coil pipeline for full-resolution display.
|
||||||
@@ -62,9 +64,10 @@ class ProfilePictureFetcher(
|
|||||||
private val options: Options,
|
private val options: Options,
|
||||||
private val thumbnailCache: ThumbnailDiskCache,
|
private val thumbnailCache: ThumbnailDiskCache,
|
||||||
private val networkFetcher: Fetcher,
|
private val networkFetcher: Fetcher,
|
||||||
|
private val backgroundScope: CoroutineScope,
|
||||||
) : Fetcher {
|
) : Fetcher {
|
||||||
override suspend fun fetch(): FetchResult? {
|
override suspend fun fetch(): FetchResult? {
|
||||||
// Check thumbnail cache first
|
// Fast path: return pre-resized thumbnail from disk (~5KB read)
|
||||||
val cached = thumbnailCache.get(originalUrl)
|
val cached = thumbnailCache.get(originalUrl)
|
||||||
if (cached != null) {
|
if (cached != null) {
|
||||||
val bitmap = thumbnailCache.decodeThumbnail(cached)
|
val bitmap = thumbnailCache.decodeThumbnail(cached)
|
||||||
@@ -86,61 +89,64 @@ class ProfilePictureFetcher(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the downloaded image to a small thumbnail
|
// For source results, read bytes, decode for immediate display,
|
||||||
val thumbnail = decodeAndResize(result) ?: return result
|
// and generate thumbnail in background for next time
|
||||||
|
if (result is SourceFetchResult) {
|
||||||
|
val bytes = result.source.source().use { it.readByteArray() }
|
||||||
|
|
||||||
// Save thumbnail to our cache
|
// Decode at thumbnail size for immediate display
|
||||||
thumbnailCache.save(originalUrl, thumbnail)
|
val bitmap = decodeThumbnailFromBytes(bytes)
|
||||||
|
if (bitmap != null) {
|
||||||
|
// Fire-and-forget: save thumbnail to disk for next load
|
||||||
|
backgroundScope.launch {
|
||||||
|
try {
|
||||||
|
thumbnailCache.save(originalUrl, bitmap)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
// Best-effort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ImageFetchResult(
|
return ImageFetchResult(
|
||||||
image = thumbnail.asImage(true),
|
image = bitmap.asImage(true),
|
||||||
isSampled = true,
|
isSampled = true,
|
||||||
dataSource = DataSource.NETWORK,
|
dataSource = DataSource.NETWORK,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun decodeAndResize(result: FetchResult): Bitmap? =
|
private fun decodeThumbnailFromBytes(bytes: ByteArray): Bitmap? =
|
||||||
try {
|
try {
|
||||||
when (result) {
|
val targetSize = ThumbnailDiskCache.THUMBNAIL_SIZE_PX
|
||||||
is SourceFetchResult -> {
|
|
||||||
val targetSize = ThumbnailDiskCache.THUMBNAIL_SIZE_PX
|
|
||||||
|
|
||||||
// First pass: decode bounds only
|
// First pass: decode bounds only
|
||||||
val boundsOptions =
|
val boundsOptions =
|
||||||
BitmapFactory.Options().apply {
|
BitmapFactory.Options().apply {
|
||||||
inJustDecodeBounds = true
|
inJustDecodeBounds = true
|
||||||
}
|
|
||||||
val source = result.source
|
|
||||||
val bytes = source.source().use { it.readByteArray() }
|
|
||||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, boundsOptions)
|
|
||||||
|
|
||||||
// Calculate inSampleSize for efficient decode
|
|
||||||
val sampleSize = calculateInSampleSize(boundsOptions, targetSize, targetSize)
|
|
||||||
|
|
||||||
// Second pass: decode at reduced size
|
|
||||||
val decodeOptions =
|
|
||||||
BitmapFactory.Options().apply {
|
|
||||||
inSampleSize = sampleSize
|
|
||||||
}
|
|
||||||
val decoded =
|
|
||||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, decodeOptions)
|
|
||||||
?: return null
|
|
||||||
|
|
||||||
// Scale to exact target size
|
|
||||||
val scaled = Bitmap.createScaledBitmap(decoded, targetSize, targetSize, true)
|
|
||||||
if (scaled !== decoded) {
|
|
||||||
decoded.recycle()
|
|
||||||
}
|
|
||||||
scaled
|
|
||||||
}
|
}
|
||||||
|
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, boundsOptions)
|
||||||
|
|
||||||
is ImageFetchResult -> {
|
// Calculate inSampleSize for efficient memory use during decode
|
||||||
// Already decoded (e.g. from Base64Fetcher) — not expected for profile pics
|
val sampleSize = calculateInSampleSize(boundsOptions, targetSize, targetSize)
|
||||||
null
|
|
||||||
|
// Second pass: decode at reduced size
|
||||||
|
val decodeOptions =
|
||||||
|
BitmapFactory.Options().apply {
|
||||||
|
inSampleSize = sampleSize
|
||||||
}
|
}
|
||||||
|
val decoded =
|
||||||
|
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, decodeOptions)
|
||||||
|
?: return null
|
||||||
|
|
||||||
|
// Scale to exact target size
|
||||||
|
val scaled = Bitmap.createScaledBitmap(decoded, targetSize, targetSize, true)
|
||||||
|
if (scaled !== decoded) {
|
||||||
|
decoded.recycle()
|
||||||
}
|
}
|
||||||
|
scaled
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,6 +181,7 @@ class ProfilePictureFetcher(
|
|||||||
class Factory(
|
class Factory(
|
||||||
private val thumbnailCache: ThumbnailDiskCache,
|
private val thumbnailCache: ThumbnailDiskCache,
|
||||||
private val networkClient: (url: String) -> Call.Factory,
|
private val networkClient: (url: String) -> Call.Factory,
|
||||||
|
private val backgroundScope: CoroutineScope,
|
||||||
) : Fetcher.Factory<Uri> {
|
) : Fetcher.Factory<Uri> {
|
||||||
private val connectivityCheckerLazy = singleParameterLazy(::ConnectivityChecker)
|
private val connectivityCheckerLazy = singleParameterLazy(::ConnectivityChecker)
|
||||||
|
|
||||||
@@ -198,7 +205,7 @@ class ProfilePictureFetcher(
|
|||||||
concurrentRequestStrategy = lazy { ConcurrentRequestStrategy.UNCOORDINATED },
|
concurrentRequestStrategy = lazy { ConcurrentRequestStrategy.UNCOORDINATED },
|
||||||
)
|
)
|
||||||
|
|
||||||
return ProfilePictureFetcher(originalUrl, options, thumbnailCache, netFetcher)
|
return ProfilePictureFetcher(originalUrl, options, thumbnailCache, netFetcher, backgroundScope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user