Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OkHttp lazy initialization #1190

Merged
merged 10 commits into from
Feb 8, 2024
Merged

OkHttp lazy initialization #1190

merged 10 commits into from
Feb 8, 2024

Conversation

mlykotom
Copy link
Contributor

What I have done and why
Move Coil's initialization with OkHttp to a background thread.
Without this change, Coil and OkHttp is initialized with the first composed image, which causes skipped frames, because the initialization can take up to 100ms.

image

Do tests pass?

  • Run local tests on DemoDebug variant: ./gradlew testDemoDebug
  • Check formatting: ./gradlew --init-script gradle/init.gradle.kts spotlessApply

@SimonMarquis
Copy link
Contributor

100ms 🤯 can you measure this on release builds as well? Debug builds are notoriously slow because they are not optimized and contain debugging features that slows down everything (especially Compose related code)

@mlykotom
Copy link
Contributor Author

100ms 🤯 can you measure this on release builds as well? Debug builds are notoriously slow because they are not optimized and contain debugging features that slows down everything (especially Compose related code)

I'm always measuring on release (benchmark) build 😁 . I think on the new versions of Android might not be as problematic, but on Pixel 6 it takes ~15ms.

@mlykotom
Copy link
Contributor Author

mlykotom commented Feb 1, 2024

@hoc081098 could you pls sign the CLA so that we can merge this PR? Thank you 😊

@hoc081098
Copy link
Contributor

@hoc081098 could you pls sign the CLA so that we can merge this PR? Thank you 😊

I have signed the CLA for "[email protected]". Don't know why the check failed 😂?

@mlykotom
Copy link
Contributor Author

mlykotom commented Feb 6, 2024

@hoc081098 could you pls sign the CLA so that we can merge this PR? Thank you 😊

I have signed the CLA for "[email protected]". Don't know why the check failed 😂?

Pls check the CLA failure for more info. Maybe you used a different email? Not sure what went wrong then.

mlykotom and others added 2 commits February 8, 2024 14:59
This way, we can load Coil's backend on a background thread and not block the MainThread with it.
Previously, the Coil image loader was initialized with the first composed image, which caused ~10ms duration and most likely skipped frames.

Change-Id: Iaa583b6adc1df7d7a51dbae1473e539f2c0b0b62
Co-authored-by: Yuri Schimke <[email protected]>
@mlykotom mlykotom force-pushed the mlykotom/coil-async-init branch 2 times, most recently from 29fa6c9 to 863bfd3 Compare February 8, 2024 14:06
Change-Id: Icbbdbcbcac1a6275857ebe998509f1e09109db7a
Change-Id: I641187ce277f434c6fca49a11b3cfccd50ecf5da
Change-Id: I0a77eb6457cac27c1a4d604c8efdcbbdce95bc48
Change-Id: I859babab7278137a4a2e49e5a085c65632888dd0
Change-Id: I347f1080ab5adf774a0cdd3c659cbf25c4820f9a
Change-Id: Ic7a6887b76caf26f00b58b0753271d426b67e75b
As discussed in coil-kt/coil#2097 the problem is caused by regitering system services, which will be fixed in 2.6.0

Change-Id: I9085309780508137f10b25ff82deed3c62e5d159
@mlykotom
Copy link
Contributor Author

mlykotom commented Feb 8, 2024

Rebased on main, so it's not dependent on the TimeZone changes PR.
Also, I removed the AsyncImageLoaderFactory, because it will be fixed directly in Coil (coil-kt/coil#2097), so once 2.6.0 is shipped, we'll get the improvement automatically.

Change-Id: I847827efc08f5cbc00f0c809ad992c267f826e50
@mlykotom mlykotom changed the base branch from mlykotom/tz-perf-improvement to main February 8, 2024 15:07
Copy link
Contributor

@alexvanyo alexvanyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! If I'm understanding it right - we are now lazily creating the okHttpCallFactory, both for our own normal network calls, and once the Coil changes are released, for Coil as well.

Code looks good, maybe the title of the PR should be "Lazily initialize okHttpCallFactory" or something similar if we aren't affecting Coil too directly in this PR?

@mlykotom mlykotom changed the title Coil asynchronous initialization OkHttp async initialization Feb 8, 2024
@mlykotom mlykotom changed the title OkHttp async initialization OkHttp lazy initialization Feb 8, 2024
@mlykotom
Copy link
Contributor Author

mlykotom commented Feb 8, 2024

We're initializing OkHttp lazily async both for our Retrofit and Coil implementation. So with this PR we have half of the improvement. The remaining work for Coil library is to lazily initialize system callbacks on a worker thread.

Also, renamed the PR.

Thanks for all reviews! ❤️

@mlykotom mlykotom merged commit b85d149 into main Feb 8, 2024
1 check passed
@mlykotom mlykotom deleted the mlykotom/coil-async-init branch February 8, 2024 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants