Optimize the Kotlin implementation to double performance #254
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.
BlurHashDecoder optimizations
String.indexOf()
to find the index of a Base83 char, which is both faster and needs less memory than aHashMap
thanks to better locality and no boxing of chars.value.pow(2f)
with fastervalue * value
Problems with the old cache
The old implementation used a cache that had 3 issues that evaded the tests:
(width * 10) + [number of colors]
, where 10 is the max colors number.useCache
is false), the code still allocated memory but didn't write to it. This had two bad effects:Fixing and keeping the cosines cache would not improve the single-threading performance much, but would make the throughput lower for multi-threading performance because of synchronization. Thus it makes more sense to just remove the cache completely, now that the algorithm is faster without cache than it was before with the cache.
Benchmarks
Simple: 4x4 colors, 32x32 pixels output.
Complex: 9x9 colors, 256x256 pixels output.
Pixel 7 (Android 14)
Nexus 5 (Android 6)
The source code of the benchmark can be found here.
Other code changes
timed()
with standardmeasureTime()
in demo appjunit.framework.Assert.assertTrue
withorg.junit.Assert.assertArrayEquals
.Update configuration to modern standards
(So the project can build on modern versions of Android Studio)
minSdkVersion
to 21 andtargetSdkVersion
to 34namespace
to replacepackage
inAndroidManifest.xml