Skip to content

Commit 54047f8

Browse files
Nick Lefevermeta-codesync[bot]
authored andcommitted
Optimize getHashMapFromPropsReadableMap in SurfaceMountingManager (facebook#54358)
Summary: Pull Request resolved: facebook#54358 Improved the performance of getHashMapFromPropsReadableMap by replacing the iterator-based approach with direct key lookups. The function now directly checks for "transform" and "opacity" keys using hasKey() and getType() instead of iterating through all entries in the ReadableMap. This optimization reduces the time complexity from O(n) to O(1) for the two specific keys we need, eliminating unnecessary iterations through irrelevant entries. Additionally, the ArrayList for transform is now pre-sized with the correct capacity to avoid reallocations. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D85967272 fbshipit-source-id: 7da60aa6383dd8b5b830f201d5bc66c7ab984572
1 parent d8e6a98 commit 54047f8

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import java.util.ArrayList;
6161
import java.util.HashMap;
6262
import java.util.HashSet;
63-
import java.util.Iterator;
6463
import java.util.LinkedList;
6564
import java.util.Map;
6665
import java.util.Queue;
@@ -71,6 +70,8 @@ public class SurfaceMountingManager {
7170
public static final String TAG = SurfaceMountingManager.class.getSimpleName();
7271

7372
private static final boolean SHOW_CHANGED_VIEW_HIERARCHIES = ReactBuildConfig.DEBUG && false;
73+
private static final String PROP_TRANSFORM = "transform";
74+
private static final String PROP_OPACITY = "opacity";
7475

7576
private volatile boolean mIsStopped = false;
7677
private volatile boolean mRootViewAttached = false;
@@ -700,7 +701,7 @@ private static void overridePropsReadableMap(
700701
String propKey = entry.getKey();
701702
if (outputReadableMap.hasKey(propKey)) {
702703
Object propValue = entry.getValue();
703-
if (propKey.equals("transform")) {
704+
if (propKey.equals(PROP_TRANSFORM)) {
704705
assert (outputReadableMap.getType(propKey) == ReadableType.Array
705706
&& propValue instanceof ArrayList);
706707
WritableArray array = new WritableNativeArray();
@@ -720,7 +721,7 @@ private static void overridePropsReadableMap(
720721
}
721722
}
722723
outputReadableMap.putArray(propKey, array);
723-
} else if (propKey.equals("opacity")) {
724+
} else if (propKey.equals(PROP_OPACITY)) {
724725
assert (outputReadableMap.getType(propKey) == ReadableType.Number
725726
&& propValue instanceof Number);
726727
outputReadableMap.putDouble(propKey, ((Number) propValue).doubleValue());
@@ -732,25 +733,26 @@ private static void overridePropsReadableMap(
732733
private static Map<String, Object> getHashMapFromPropsReadableMap(ReadableMap readableMap) {
733734
HashMap<String, Object> outputMap = new HashMap<>();
734735

735-
Iterator<Map.Entry<String, Object>> iter = readableMap.getEntryIterator();
736-
while (iter.hasNext()) {
737-
Map.Entry<String, Object> entry = iter.next();
738-
String propKey = entry.getKey();
739-
Object propValue = entry.getValue();
740-
if (propKey.equals("transform") && propValue instanceof ReadableArray) {
741-
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>();
742-
for (int i = 0; i < ((ReadableArray) propValue).size(); i++) {
743-
ReadableMap map = ((ReadableArray) propValue).getMap(i);
736+
if (readableMap.hasKey(PROP_TRANSFORM)
737+
&& readableMap.getType(PROP_TRANSFORM) == ReadableType.Array) {
738+
ReadableArray transformArray = readableMap.getArray(PROP_TRANSFORM);
739+
if (transformArray != null) {
740+
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>(transformArray.size());
741+
for (int i = 0; i < transformArray.size(); i++) {
742+
ReadableMap map = transformArray.getMap(i);
744743
if (map != null) {
745744
arrayList.add(map.toHashMap());
746745
}
747746
}
748-
outputMap.put(propKey, arrayList);
749-
} else if (propKey.equals("opacity") && propValue instanceof Number) {
750-
outputMap.put(propKey, ((Number) propValue).doubleValue());
747+
outputMap.put(PROP_TRANSFORM, arrayList);
751748
}
752749
}
753750

751+
if (readableMap.hasKey(PROP_OPACITY)
752+
&& readableMap.getType(PROP_OPACITY) == ReadableType.Number) {
753+
outputMap.put(PROP_OPACITY, readableMap.getDouble(PROP_OPACITY));
754+
}
755+
754756
return outputMap;
755757
}
756758

0 commit comments

Comments
 (0)