Skip to content

Commit 7517aa3

Browse files
Yqwedgabrieldonadel
authored andcommitted
Do not key WeakHashMap by Integer
After JEP-401 that will throw IdentityException. Also, int-s outside of [-128;128) are not cached and boxing creates new instances, meaning that GC is free to reclaim them immediately. getStyleFromString only returns 7 different values and the map stores pair of int-s: using LruCache-s is overkill and SparseArray is fine. #60220 <!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. --> ## Summary: Do not key WeakHashMap by Integer as preparation for JEP-401 <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> ## Changelog: Do not key WeakHashMap by Integer as preparation for JEP-401. Pick one each for the category and type tags: [ANDROID][FIXED] - Do not key WeakHashMap by Integer For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> ## Test Plan: <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. --> Differential Revision: D133975563 Pull Request resolved: #60223 (cherry picked from commit 375d0a9)
1 parent 3cf0cfb commit 7517aa3

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/progressbar/ReactProgressBarViewManager.kt‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package com.facebook.react.views.progressbar
1111

1212
import android.content.Context
1313
import android.util.Pair
14+
import android.util.SparseArray
1415
import android.view.View
1516
import android.widget.ProgressBar
1617
import com.facebook.common.logging.FLog
@@ -28,7 +29,6 @@ import com.facebook.react.viewmanagers.AndroidProgressBarManagerDelegate
2829
import com.facebook.react.viewmanagers.AndroidProgressBarManagerInterface
2930
import com.facebook.yoga.YogaMeasureMode
3031
import com.facebook.yoga.YogaMeasureOutput
31-
import java.util.WeakHashMap
3232

3333
/**
3434
* Manages instances of [ProgressBar]. [ProgressBar] is wrapped in a [ProgressBarContainerView]
@@ -40,8 +40,7 @@ import java.util.WeakHashMap
4040
internal class ReactProgressBarViewManager :
4141
BaseViewManager<ProgressBarContainerView, LayoutShadowNode>(),
4242
AndroidProgressBarManagerInterface<ProgressBarContainerView> {
43-
@Suppress("IDENTITY_SENSITIVE_OPERATIONS_WITH_VALUE_TYPE")
44-
private val measuredStyles = WeakHashMap<Int, Pair<Int, Int>>()
43+
private val measuredStyles = SparseArray<Pair<Int, Int>>()
4544

4645
private val delegate: ViewManagerDelegate<ProgressBarContainerView> =
4746
AndroidProgressBarManagerDelegate(this)
@@ -111,11 +110,16 @@ internal class ReactProgressBarViewManager :
111110
): Long {
112111
val style = getStyleFromString(props?.getString(PROP_STYLE))
113112
val value =
114-
measuredStyles.getOrPut(style) {
115-
val progressBar = createProgressBar(context, style)
116-
val spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
117-
progressBar.measure(spec, spec)
118-
Pair.create(progressBar.measuredWidth, progressBar.measuredHeight)
113+
synchronized(measuredStyles) {
114+
measuredStyles[style]
115+
?: run {
116+
val progressBar = createProgressBar(context, style)
117+
val spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
118+
progressBar.measure(spec, spec)
119+
Pair.create(progressBar.measuredWidth, progressBar.measuredHeight).also {
120+
measuredStyles.put(style, it)
121+
}
122+
}
119123
}
120124

121125
return YogaMeasureOutput.make(

0 commit comments

Comments
 (0)