I'm using Compose Coil for image loading in my pro...
# compose
r
I'm using Compose Coil for image loading in my project, I have a grid of images shown in my composable and they're driven by a state that would update the grid to another set of images. I'm using Coil with this image loader
Copy code
val imageLoader = remember {
    ImageLoader.Builder(context)
        .bitmapFactoryMaxParallelism(10)
        .respectCacheHeaders(false)
        .build()
}
(I was playing around the bitmap parallelism and cache headers, I also tried providing disk/cache policies and enabling them in the
ImageRequest
but it seems like Coil is quite slow to load the images after they are cached. I tried my same code but use picasso-compose and the images load instantly (After being cached) whereas using Coil there's a split second load, I added a debugger to Coil to make sure it's not loading the image again from the internet (and it's not) The above was on production build for both Coil and Picasso Has anyone experience some performance issues with Coil in Compose?
c
cc @Colin White?
I have noticed that images can large slowly, but my CDN currently doesn't send back caching headers properly and so im working on that at the moment.
also... are you sure you're on the latest coil?
c
It sounds like the image is missing the memory cache. Are you sharing one image loader for all your requests?
Also if you enable logging (check the faq in coil-kt.io) it’ll tell you why it’s missing
r
Ah yea it's the same image loaded for all requests. Also I'm using the url as the key for memory and risk cache. I thought respectCacheHeaders would just ignore the request headers and use the cache if it exists?
c
Yep it’ll ignore headers for the disk cache, but this sounds like it could be missing the memory cache
Also Coil should be faster to decode since it only loads the image into memory at the minimum size. Picasso always loads the image’s original size
You can set size(Size.ORIGINAL) on your request to achieve the same behaviour with Coil
r
I tried this
Copy code
val imageLoader = remember {
    ImageLoader.Builder(context)
        .bitmapFactoryMaxParallelism(10)
        .respectCacheHeaders(false)
        .memoryCache {
            MemoryCache
                .Builder(context)
                .build()
        }
        .diskCache {
            DiskCache.Builder()
                .directory(context.cacheDir.resolve("image_cache"))
                .build()
        }
        .build()
}
With this
Copy code
ImageRequest.Builder(context)
   .data(url)
   .memoryCachePolicy(CachePolicy.ENABLED)
   .diskCachePolicy(CachePolicy.ENABLED)
   .allowHardware(true)
   .diskCacheKey(url)
   .memoryCacheKey(url)
   .build()
Shouldn't this ensure there's memory cache enabled/
c
Yes the memory cache is enabled
r
Humm I wonder what I'm missing then 😕
Btw it's inside a custom 'LazyLayout' implementation although not sure if that affects anything
c
Try in just a regular non-lazy layout? just to take that out of the equation?
r
Yep ill try noq
c
also try
Also if you enable logging (check the faq in coil-kt.io) it’ll tell you why it’s missing
r
Ahh yeah I'll do that too
c
that helped me find some issues in coil that i wasn't expecting
mostly networking related. but yeah supppper helpful
r
Ahhh figured out my problem.. It was putting the
ImageLoader
inside the
remember
. Switching it to a
single
inside my koin module fixed that.. There was a split second where the composable that hosts this one would actually be destroyed hence destroying this image loader..
So I'm guessing the memory cache is tied to that one instance of image loader (Which would make sense)
Thanks for the callout on the debugger, it was actually loading everything from disk rather than memory every single time (Which is why it was slower)
c
Yeah. I think that's what Colin said in the begining
It sounds like the image is missing the memory cache. Are you sharing one image loader for all your requests?
glad the debugger helped. CHEERS
r
Yep, that was my assumptiom, I thought I did. But actually I was only doing so for the current state of a grid, not between the different states of it
Thank you for the help !
903 Views