Skip to content

Commit 51e464f

Browse files
javachefacebook-github-bot
authored andcommitted
Remove unnecessary synchronized collections from ReactHostImpl (facebook#45026)
Summary: Pull Request resolved: facebook#45026 All callsites for these containers already explicitly synchronize using these objects, so there's no need to use a synchronized collection wrapper here. Changelog: [Internal] Reviewed By: rshest Differential Revision: D58724044 fbshipit-source-id: 5151ebb0ceda8656b6039d9984cc32a843051abd
1 parent 9b67547 commit 51e464f

File tree

1 file changed

+19
-22
lines changed
  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime

1 file changed

+19
-22
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import com.facebook.react.bridge.ReactSoftExceptionLogger;
4545
import com.facebook.react.bridge.RuntimeExecutor;
4646
import com.facebook.react.bridge.UiThreadUtil;
47-
import com.facebook.react.bridge.queue.QueueThreadExceptionHandler;
4847
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
4948
import com.facebook.react.common.LifecycleState;
5049
import com.facebook.react.common.build.ReactBuildConfig;
@@ -72,8 +71,8 @@
7271
import java.lang.ref.WeakReference;
7372
import java.util.ArrayList;
7473
import java.util.Collection;
75-
import java.util.Collections;
7674
import java.util.HashSet;
75+
import java.util.List;
7776
import java.util.Set;
7877
import java.util.concurrent.Executor;
7978
import java.util.concurrent.Executors;
@@ -104,14 +103,10 @@ public class ReactHostImpl implements ReactHost {
104103
private final DevSupportManager mDevSupportManager;
105104
private final Executor mBGExecutor;
106105
private final Executor mUIExecutor;
107-
private final QueueThreadExceptionHandler mQueueThreadExceptionHandler;
108-
private final Set<ReactSurfaceImpl> mAttachedSurfaces =
109-
Collections.synchronizedSet(new HashSet<>());
106+
private final Set<ReactSurfaceImpl> mAttachedSurfaces = new HashSet<>();
110107
private final MemoryPressureRouter mMemoryPressureRouter;
111108
private final boolean mAllowPackagerServerAccess;
112109
private final boolean mUseDevSupport;
113-
private final Collection<ReactInstanceEventListener> mReactInstanceEventListeners =
114-
Collections.synchronizedList(new ArrayList<>());
115110

116111
// todo: T192399917 This no longer needs to store the react instance
117112
private final BridgelessAtomicRef<Task<ReactInstance>> mCreateReactInstanceTaskRef =
@@ -133,8 +128,8 @@ public class ReactHostImpl implements ReactHost {
133128
private @Nullable MemoryPressureListener mMemoryPressureListener;
134129
private @Nullable DefaultHardwareBackBtnHandler mDefaultHardwareBackBtnHandler;
135130

136-
private final Set<Function0<Unit>> mBeforeDestroyListeners =
137-
Collections.synchronizedSet(new HashSet<>());
131+
private final List<ReactInstanceEventListener> mReactInstanceEventListeners = new ArrayList<>();
132+
private final List<Function0<Unit>> mBeforeDestroyListeners = new ArrayList<>();
138133

139134
private @Nullable ReactHostInspectorTarget mReactHostInspectorTarget;
140135

@@ -167,7 +162,6 @@ public ReactHostImpl(
167162
mComponentFactory = componentFactory;
168163
mBGExecutor = bgExecutor;
169164
mUIExecutor = uiExecutor;
170-
mQueueThreadExceptionHandler = ReactHostImpl.this::handleHostException;
171165
mMemoryPressureRouter = new MemoryPressureRouter(context);
172166
mAllowPackagerServerAccess = allowPackagerServerAccess;
173167
mUseDevSupport = useDevSupport;
@@ -416,12 +410,16 @@ public boolean onBackPressed() {
416410

417411
/** Add a listener to be notified of ReactInstance events. */
418412
public void addReactInstanceEventListener(ReactInstanceEventListener listener) {
419-
mReactInstanceEventListeners.add(listener);
413+
synchronized (mReactInstanceEventListeners) {
414+
mReactInstanceEventListeners.add(listener);
415+
}
420416
}
421417

422418
/** Remove a listener previously added with {@link #addReactInstanceEventListener}. */
423419
public void removeReactInstanceEventListener(ReactInstanceEventListener listener) {
424-
mReactInstanceEventListeners.remove(listener);
420+
synchronized (mReactInstanceEventListeners) {
421+
mReactInstanceEventListeners.remove(listener);
422+
}
425423
}
426424

427425
/**
@@ -1067,7 +1065,7 @@ private Task<ReactInstance> getOrCreateReactInstanceTask() {
10671065
mReactHostDelegate,
10681066
mComponentFactory,
10691067
devSupportManager,
1070-
mQueueThreadExceptionHandler,
1068+
this::handleHostException,
10711069
mUseDevSupport,
10721070
getOrCreateReactHostInspectorTarget());
10731071
mReactInstance = instance;
@@ -1147,13 +1145,13 @@ class Result {
11471145
reactContext, getCurrentActivity());
11481146
}
11491147

1150-
ReactInstanceEventListener[] listeners =
1151-
new ReactInstanceEventListener[mReactInstanceEventListeners.size()];
1152-
final ReactInstanceEventListener[] finalListeners =
1153-
mReactInstanceEventListeners.toArray(listeners);
1154-
11551148
log(method, "Executing ReactInstanceEventListeners");
1156-
for (ReactInstanceEventListener listener : finalListeners) {
1149+
ReactInstanceEventListener[] instanceEventListeners;
1150+
synchronized (mReactInstanceEventListeners) {
1151+
instanceEventListeners =
1152+
mReactInstanceEventListeners.toArray(new ReactInstanceEventListener[0]);
1153+
}
1154+
for (ReactInstanceEventListener listener : instanceEventListeners) {
11571155
if (listener != null) {
11581156
listener.onReactContextInitialized(reactContext);
11591157
}
@@ -1397,11 +1395,10 @@ private Task<ReactInstance> getOrCreateReloadTask(String reason) {
13971395
reactInstanceTaskUnwrapper.unwrap(
13981396
task, "3: Executing Before Destroy Listeners");
13991397

1400-
Set<Function0<Unit>> beforeDestroyListeners;
1398+
Function0<Unit>[] beforeDestroyListeners;
14011399
synchronized (mBeforeDestroyListeners) {
1402-
beforeDestroyListeners = new HashSet<>(mBeforeDestroyListeners);
1400+
beforeDestroyListeners = mBeforeDestroyListeners.toArray(new Function0[0]);
14031401
}
1404-
14051402
for (Function0<Unit> destroyListener : beforeDestroyListeners) {
14061403
destroyListener.invoke();
14071404
}

0 commit comments

Comments
 (0)