Comments (8)
There's a couple of problems measuring the timing this way:
- In general, you shouldn't measure performance on emulator as this way you effectively measure how powerful hosting machine you have
- This way, you are can be hitting all the system caches, which doesn't show in the real app.
What I did to benchmak this, was using Macrobenchmark in the real app with StartupMode.COLD
to see the worst-case scenario.
For 10 iterations, I see these results
NiaImageLoaderMs min 2.0, median 4.2, max 6.9
Traces: Iteration 0 1 2 3 4 5 6 7 8 9
I further started digging into what's happening and I think I know now.
I followed you advice to only have OkHttp
asynchronously, but keep the ImageLoader
on-demand and this is what I see in the traces.
When the first image is shown on screen (in NIA it's the SingleTopicButton
), it starts to initialize Coil, which takes ~7ms.
If this was during startup, it wouldn't be much of an issue, but with a first composition, it might be problematic (trace here).
The cause are (my hypothesis) the binder transaction
sections, which comes from the SystemCallbacks
.
I think Coil should be able to register the system services on a Default
thread to prevent the initialization hit and then it won't matter when the ImageLoader
is actually initialized.
from coil.
Fixed here in the 3.x release. I'll also backport the change to the next release, 2.6.0
.
I tried running my own macrobenchmark test after the changes, but the initialization method didn't appear in the measured methods.
from coil.
Awesome! Thanks! 🎉
If you'd like to help diagnose the macrobenchmark tests, maybe ping me on ASG?
from coil.
It looks like you were initializing your OkHttpClient
eagerly here, which slowed down the ImageLoader
creation. Coil already supports lazy initialization of the OkHttpClient
using the ImageLoader.Builder.callFactory { }
function that accepts a callback that'll be invoked on a background thread. By default it'll create the OkHttpClient
using that method.
newImageLoader
is invoked the first time Coil.imageLoader()
is called, which is generally on the main thread, but doesn't have to be. I don't think we should eagerly asynchronously initialize the singleton ImageLoader
for a couple reasons:
ImageLoader
s are cheap to initialize (see benchmark below).- Eagerly initializing the
ImageLoader
would require importingandroidx.startup
. - We wouldn't be able to eagerly initialize on non-Android platforms so the behaviour wouldn't be consistent.
- Eagerly initializing would create a race condition if the user uses
Coil.setImageLoader
before the firstCoil.imageLoader
call.
For docs, I'd be open to a PR that improves this!
from coil.
I ran a quick benchmark on a Pixel 7 emulator and an ImageLoader
with default arguments takes 0.1 milliseconds to initialize. The result is similar when using ImageLoader.Builder.callFactory { }
. Here's the code:
val iterations = 1_000
var sum = 0L
var imageLoader: ImageLoader?
repeat(iterations) {
sum += measureNanoTime {
imageLoader = ImageLoader(context)
}
imageLoader?.shutdown()
}
println("BENCHMARK OUTPUT: ${sum / iterations.toDouble()}")
from coil.
When I set .networkObserverEnabled(false)
, then the initialization drops to ~1.5ms. (trace)
from coil.
Ah thanks for digging in! Agreed - SystemCallbacks
should be registered lazily on a background thread. We should be able to delay checking if the device has a suitable network connection until we're on a background thread and move the network callbacks + memory callbacks registration to a background thread.
from coil.
PR for a cautionary fix on OkHttp 5.x
https://github.com/square/okhttp/pull/8248/files
from coil.
Related Issues (20)
- Consider adding workaround for U + gainmap + hardware bitmap bug HOT 2
- Update Roborazzi tests to also run on desktop JVM
- LazyColumn + AsyncImage loads the image, and when an error occurs during fast scrolling HOT 5
- Unable to sync coil3:coil-gif while using cocoapods to include a pod HOT 2
- Update ContentPainterModifier with ModifierNodeElement to allow reuse on LazyList
- [Coil 3] AsyncImage does not respect default placeholder/error/fallback
- Memory leak problem of ImageRequest.context HOT 1
- Flicker when changing `ByteArray` data HOT 2
- java.lang.IllegalStateException: BitmapFactory returned a null bitmap. Often this means BitmapFactory could not decode the image data read from the input source (e.g. network, disk, or memory) as it's not HOT 5
- Changing date/time into the future breaks showing the image HOT 2
- VideoFrameDecoder HLS Support HOT 4
- rememberAsyncImagePainter renders nothing when using dragAndDropSource HOT 4
- [Coil 3] Crash on Desktop HOT 3
- Cant install coil :( HOT 1
- Use WeakRef on JS/WASM
- Crashing when trying to load image.9.png using coil compose function rememberAsyncImagePainter HOT 1
- Only support https? HOT 2
- [Coil 3] Consider replacing coil internal LruCache with androidx one HOT 1
- Excessive API Calls in Image Loading with RecyclerView When Using custom fetcher HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coil.