Skip to content

Enable R8 optimization of Dispatchers.Main loading #1232

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

Merged

Conversation

wojtek-kalicinski
Copy link
Contributor

The developer still has to disable the FastServiceLoader manually
if they're using R8.

R8 is then able to optimize away the ServiceLoader and reflection
entirely, resulting in direct class instantiation and no extra I/O
on calling thread.

Fixes #1231

The developer still has to disable the FastServiceLoader manually
if they're using R8.

R8 is then able to optimize away the ServiceLoader and reflection
entirely, resulting in direct class instantiation and no extra I/O
on calling thread.

Fixes Kotlin#1231
@wojtek-kalicinski wojtek-kalicinski marked this pull request as ready for review May 30, 2019 12:26
@LouisCAD
Copy link
Contributor

@wojtek-kalicinski Would it possible for the R8 team to disable FastServiceLoader so we don't have to add an extra configuration step for all the projects in the world (risking people forgetting it)?

@wojtek-kalicinski
Copy link
Contributor Author

I'm looking into ways we can do it, I agree that having a manual step that developers have to remember about does not solve the issue fully, but I just want to unblock the R8 optimization first.
I'll follow up on this if I have a solution.

@ansman
Copy link
Contributor

ansman commented May 30, 2019

@wojtek-kalicinski Not sure if out of scope for this PR but CoroutineExceptionHandlerImpl.kt also uses ServiceLoader.load to load the exception handler and I'm guessing that code would need to be updated as well.

@ansman
Copy link
Contributor

ansman commented May 30, 2019

I'm looking into ways we can do it, I agree that having a manual step that developers have to remember about does not solve the issue fully, but I just want to unblock the R8 optimization first.
I'll follow up on this if I have a solution.

Couldn't the default value just be false since the FastServiceLoader is only really needed on Android? JAR verification seems like a useful thing on the server side so it would make sense to hade it use the regular service loader by default.

@elizarov elizarov requested review from qwwdfsad and elizarov May 30, 2019 20:17
Copy link
Contributor

@elizarov elizarov left a comment

Choose a reason for hiding this comment

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

Looks good to me. We've been only using FastServiceLoader for MainDispatcher so it makes sense to keep it this way.

@JakeWharton
Copy link
Contributor

I'm working on a test for this which actually invokes R8 and verifies that all ServiceLoader calls are removed from the final dex to prevent regression or new occurrences in the wrong form. Feel free to merge without it though, as it might be a few days.

@LouisCAD
Copy link
Contributor

LouisCAD commented May 31, 2019 via email

Copy link
Collaborator

@qwwdfsad qwwdfsad left a comment

Choose a reason for hiding this comment

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

LGTM.

I'm going to disable FastServiceLoader by default if optimization works on the latest R8 versions.
The rationale is quite simple: if users use the latest version of kx.coroutines, they'll probably use the latest version of Android toolchain, then it makes sense to simplify the development process for then.
For the rest of the users, we can recommend either to enable FSL or to update R8 version.

Could you please specify since which R8 version is this optimization present?

@wojtek-kalicinski
Copy link
Contributor Author

I would argue that we should not disable FastServiceLoader by default just yet, and discuss this further.
We have two problems here:

  1. we want apps that use R8 to be able to get the optimization - this is what my PR unblocks
  2. a lot (A LOT) of apps are not using R8 or ProGuard (they have minification disabled). FastServiceLoader is actually a desireable optimization for those apps and it would be a shame if they lost it

It's difficult to come up with a solution that works for both groups, but I have some ideas that I'd like to further discuss. I'll follow up in a bit.

@wojtek-kalicinski
Copy link
Contributor Author

wojtek-kalicinski commented May 31, 2019

Could you please specify since which R8 version is this optimization present?

Also to add to my previous comment, if I understand the R8 releases correctly, this optimization will be present in future R8 releases from the 1.5.X branch, so this further reinforces why we should not disable the fast loader for everyone just yet.

@Tolriq
Copy link

Tolriq commented Jun 1, 2019

Out of curiosity is there a proper way to set system properties on Android?

Or it's to be done in code as early as possible in application.OnCreate?

@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Jun 2, 2019

@wojtek-kalicinski good points, I'll merge it as is in that case.

Please let us know when R8 is released so we could update our documentation

@qwwdfsad qwwdfsad merged commit b9b7d82 into Kotlin:develop Jun 2, 2019
@wojtek-kalicinski wojtek-kalicinski deleted the fix_r8_fastserviceloader branch June 28, 2019 09:07
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.

7 participants