Open colinrtwhite opened 1 week ago
Took a spike at using launch(start = CoroutineStart.UNDISPATCHED)
instead of Dispatchers.Main.immedate
and UNDISPATCHED
still causes us to dispatch as we only avoid dispatching "until the next suspension", which is imageLoader.execute
. This means we likely need to keep Dispatchers.Main.immedate
.
Why don't we perform the memory cache check before calling imageLoader.execute
?
AsyncImagePainter
. We also then have two places we're checking the memory cache which complicates the design.MemoryCache.Key
and SizeResolver.size
is a suspending function, which means launch(start = CoroutineStart.UNDISPATCHED)
still wouldn't work.What does imageLoader.execute
actually do asynchronously in the fast path (memory cache hit)?
One idea: use a custom dispatcher, similar to the one Compose uses, that drains the queue right before drawing. This dispatcher would only be used for Coil's own coroutine loading calls, so on most frames the pre-draw drain would be a no-op and on frames where there is work, it would only be image-related work.
Pros:
Cons:
Is your feature request related to a problem? Please describe. There's been some appetite to perform the memory cache check ahead of the interceptor chain. This is a relatively big behaviour change, but it has a couple benefits:
Interceptor
code will run by default on background threads. Most interceptors don't need to intercept access to the memory cache and can safely on run any thread. This also allows interceptors tosuspend
without worrying about delaying the memory cache check and missing the first frame.AsyncImagePainter
here. Instead we can uselaunch(start = CoroutineStart.UNDISPATCHED)
. We can also remove our dependency onDispatchers.Main
on non-Android platforms (we still need it for Android when usingimageLoader.enqueue
).Describe the solution you'd like Add an opt in flag to
ImageLoader
andImageRequest
to perform the mapping, keying, and memory cache check here instead of here.We'll still need to preserve the option to intercept before the memory cache check as some users might want that.