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

Implement optimization for Android with R8 #2

Merged
merged 1 commit into from
Aug 14, 2024
Merged

Conversation

whyoleg
Copy link
Owner

@whyoleg whyoleg commented Aug 12, 2024

Fixes #1

Tested with Android Studio on emulators with API 35 (Android 15) and API 23 (Android 6.0) with the following config:

debug {
    isDebuggable = false
    isMinifyEnabled = true
    proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"))
}

At my side with call to ServiceLoader.load<TestService> in MainActivity just after super.onCreate(savedInstanceState) with three implementations takes around:

  • 10-20 millis with isMinifyEnabled=false on 0.1.0
  • 10-20 millis with isMinifyEnabled=true on 0.1.0
  • 10-20 millis with isMinifyEnabled=false on this PR
  • 300-500 nanoseconds with isMinifyEnabled=true on this PR

I get the same 300-500 nanoseconds with isMinifyEnabled=true if I will just repeat the same machinery in the app, as I use under the hood to aggregate services, when just passing objects.
I get 100-200 nanoseconds with isMinifyEnabled=true if to remove the machinery and just use java.util.ServiceLoader.load.

Yeah, there is some overhead, but I would say it's rather minimal. Of course the situation on real devices will be different, but it will be different even with manual initialization.

So, looks like in the end currently ServiceLoader on Android is not really slow. Except for the case when the Service is in base module, but ServiceProvider is in the feature module.

@whyoleg whyoleg self-assigned this Aug 12, 2024
@whyoleg whyoleg merged commit ce9719b into main Aug 14, 2024
9 checks passed
@whyoleg whyoleg deleted the r8-optimization branch August 14, 2024 19:10
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.

ServiceLoader is very, very expensive on Android, please just crash instead
1 participant