Skip to content

Commit 508b152

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Avoid array copies on every MapBuffer read (#52386)
Summary: Pull Request resolved: #52386 Enum `values()` function makes a copy of an underlying array on each call. This happens in a hot path, and seems to show up during profiling. Let's cache it. Changelog: [Internal] Reviewed By: lenaic Differential Revision: D77623705 fbshipit-source-id: 5a33425822f477f63fe104ca9e5ed474385a2022
1 parent 0d455f3 commit 508b152

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.facebook.jni.HybridClassBase
1111
import com.facebook.proguard.annotations.DoNotStrip
1212
import com.facebook.react.common.annotations.StableReactNativeAPI
1313
import com.facebook.react.common.mapbuffer.MapBuffer.Companion.KEY_RANGE
14+
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
1415
import java.lang.StringBuilder
1516
import java.nio.ByteBuffer
1617
import java.nio.ByteOrder
@@ -86,7 +87,11 @@ private constructor(
8687

8788
private fun readDataType(bucketIndex: Int): MapBuffer.DataType {
8889
val value = readUnsignedShort(getKeyOffsetForBucketIndex(bucketIndex) + TYPE_OFFSET).toInt()
89-
return MapBuffer.DataType.values()[value]
90+
return if (ReactNativeFeatureFlags.enableAndroidTextMeasurementOptimizations()) {
91+
DATA_TYPES[value]
92+
} else {
93+
MapBuffer.DataType.values()[value]
94+
}
9095
}
9196

9297
private fun getTypedValueOffsetForKey(key: Int, expected: MapBuffer.DataType): Int {
@@ -264,7 +269,12 @@ private constructor(
264269
get() = readUnsignedShort(bucketOffset).toInt()
265270

266271
override val type: MapBuffer.DataType
267-
get() = MapBuffer.DataType.values()[readUnsignedShort(bucketOffset + TYPE_OFFSET).toInt()]
272+
get() =
273+
if (ReactNativeFeatureFlags.enableAndroidTextMeasurementOptimizations()) {
274+
DATA_TYPES[readUnsignedShort(bucketOffset + TYPE_OFFSET).toInt()]
275+
} else {
276+
MapBuffer.DataType.values()[readUnsignedShort(bucketOffset + TYPE_OFFSET).toInt()]
277+
}
268278

269279
override val doubleValue: Double
270280
get() {
@@ -318,5 +328,7 @@ private constructor(
318328

319329
// 4 bytes = 2 (key) + 2 (type)
320330
private const val VALUE_OFFSET = 4
331+
332+
private val DATA_TYPES = MapBuffer.DataType.values()
321333
}
322334
}

0 commit comments

Comments
 (0)