Implement optimization for Android with R8 #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1
Tested with Android Studio on emulators with API 35 (Android 15) and API 23 (Android 6.0) with the following config:
At my side with call to
ServiceLoader.load<TestService>
inMainActivity
just aftersuper.onCreate(savedInstanceState)
with three implementations takes around:isMinifyEnabled=false
on 0.1.0isMinifyEnabled=true
on 0.1.0isMinifyEnabled=false
on this PRisMinifyEnabled=true
on this PRI 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 passingobjects
.I get 100-200 nanoseconds with
isMinifyEnabled=true
if to remove the machinery and just usejava.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 theService
is in base module, butServiceProvider
is in the feature module.