Skip to content

Optimize the size of the coroutines library in Android projects #1282

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
merged 4 commits into from
Feb 14, 2020

Conversation

elizarov
Copy link
Contributor

@elizarov elizarov commented Jun 19, 2019

updated (see below)

@elizarov elizarov force-pushed the optimize-size branch 5 times, most recently from cf305a8 to 92d1c17 Compare June 20, 2019 07:08
@Tolriq
Copy link

Tolriq commented Jun 20, 2019

Can you please comment a little on Use simpler and smaller CommonPool for both Dispatchers.Default and IO?
I'm currently working on my app to try to better manage threads due to contention potentially starting unneeded threads with workerpool patterns. Maybe there's less needs with this.

@elizarov
Copy link
Contributor Author

elizarov commented Jun 20, 2019

It is just code-size optimization. CommonPool reuses platform code (either FJP or TPE) so its code much is smaller. However, it actually makes thread usage worse, because those underlying platform thread-pools start all threads at once, while a more efficient CoroutinesScheduler starts them only on demand. But CS has way more code, which might be critical for apps targeting extremely low-end devices with very tight budgets on .dex size.

@Tolriq
Copy link

Tolriq commented Jun 21, 2019

Hum OK thanks I'll continue to search why there's no many threads created and how to mitigate then :(

Copy link
Member

@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.

We should document all these hacks in the single document for the sake of future development.
Also, we should know what we are optimizing: debug builder, release build, minified build?

@elizarov elizarov force-pushed the optimize-size branch 2 times, most recently from 3644237 to 3436ac8 Compare July 3, 2019 12:14
@elizarov elizarov force-pushed the optimize-size branch 2 times, most recently from b014114 to abd1ef6 Compare December 20, 2019 09:44
@elizarov
Copy link
Contributor Author

elizarov commented Dec 20, 2019

I've cleaned up this PR, removed questionable optimizations, no more messing up with scheduler (scheduler is quite smaller now anyway). All the rules are bundled in the jar for R6 1.6.0+. Verified to work in a test project using AGP 4.0.0-alpha06. This PR saves additional 16Kb is DEX size in addition to savings you get by simply using a newer R8 version.

@elizarov elizarov marked this pull request as ready for review December 20, 2019 09:49
@elizarov elizarov requested a review from qwwdfsad December 20, 2019 09:50
@elizarov elizarov changed the title Optimize the size of the coroutines library in Android projects (WIP) Optimize the size of the coroutines library in Android projects Dec 20, 2019
Copy link
Member

@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.

Great work!

Copy link
Member

@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.

Almost there! Could you please fix the remaining issue, rebase this branch on develop and do one more last measurement before the merge?

@elizarov
Copy link
Contributor Author

Works like a charm. Now also reports the size of the optimized android DEX file for basic coroutine usage: https://teamcity.jetbrains.com/project/KotlinTools_KotlinxCoroutines?mode=builds&projectTab=stats

Includes additional R8 rules to disable debugging & stack-trace recovery in optimized
Android builds. Additional savings with AGP 4.0.0-alpha06 (r8-2.0.4-dev) are ~16kb
in uncompressed DEX size.

Tests are modified to verify that the classes that are supposed to be removed are
indeed removed.
Copy link
Member

@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.

🚀🚀🚀

@elizarov elizarov merged commit bf9509d into develop Feb 14, 2020
@elizarov elizarov deleted the optimize-size branch February 14, 2020 12:52
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