From 35b22bbcc416d5165c3244b49342c165bc3624dc Mon Sep 17 00:00:00 2001 From: Muh Isfhani Ghiath Date: Thu, 18 Feb 2021 14:21:29 +0700 Subject: [PATCH 1/2] use SparseArrayCompat for better performance SparseArray can be used to replace HashMap when the key is a primitive type. There are some variants for different key/value types, even though not all of them are publicly available. (read more: https://speakerdeck.com/romainguy/android-memories?slide=90) --- .../java/com/wolt/blurhashkt/BlurHashDecoder.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt b/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt index 3278868b..432d52db 100644 --- a/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt +++ b/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt @@ -2,6 +2,7 @@ package com.wolt.blurhashkt import android.graphics.Bitmap import android.graphics.Color +import androidx.collection.SparseArrayCompat import kotlin.math.cos import kotlin.math.pow import kotlin.math.withSign @@ -11,8 +12,8 @@ object BlurHashDecoder { // cache Math.cos() calculations to improve performance. // The number of calculations can be huge for many bitmaps: width * height * numCompX * numCompY * 2 * nBitmaps // the cache is enabled by default, it is recommended to disable it only when just a few images are displayed - private val cacheCosinesX = HashMap() - private val cacheCosinesY = HashMap() + private val cacheCosinesX = SparseArrayCompat() + private val cacheCosinesY = SparseArrayCompat() /** * Clear calculations stored in memory cache. @@ -133,21 +134,21 @@ object BlurHashDecoder { private fun getArrayForCosinesY(calculate: Boolean, height: Int, numCompY: Int) = when { calculate -> { DoubleArray(height * numCompY).also { - cacheCosinesY[height * numCompY] = it + cacheCosinesY.put(height * numCompY, it) } } else -> { - cacheCosinesY[height * numCompY]!! + cacheCosinesY.get(height * numCompY)!! } } private fun getArrayForCosinesX(calculate: Boolean, width: Int, numCompX: Int) = when { calculate -> { DoubleArray(width * numCompX).also { - cacheCosinesX[width * numCompX] = it + cacheCosinesX.plut(width * numCompX, it) } } - else -> cacheCosinesX[width * numCompX]!! + else -> cacheCosinesX.get(width * numCompX)!! } private fun DoubleArray.getCos( From eabdd8172be5f8be93906c925a7b92b996cc4a8d Mon Sep 17 00:00:00 2001 From: Muh Isfhani Ghiath Date: Sat, 20 Feb 2021 21:13:53 +0700 Subject: [PATCH 2/2] fix typo --- Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt b/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt index 432d52db..f5495a45 100644 --- a/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt +++ b/Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt @@ -145,7 +145,7 @@ object BlurHashDecoder { private fun getArrayForCosinesX(calculate: Boolean, width: Int, numCompX: Int) = when { calculate -> { DoubleArray(width * numCompX).also { - cacheCosinesX.plut(width * numCompX, it) + cacheCosinesX.put(width * numCompX, it) } } else -> cacheCosinesX.get(width * numCompX)!!