mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Do not run UiThreadUtil asserts outside of debug mode
Summary: I have never seen these asserts fire in production. They're pretty cheap but the cost is not zero. We will use annotations and test carefully in debug if we need to ensure that something runs on a particular thread - which we do anyway. Motivation: this method is called /extremely frequently/, everywhere in the mounting layer. And 99.99% of the time it's completely useless and results in absolutely no signal. In many cases, it will be called hundreds or thousands of times during a single operation (for example, when executing the IntMountBuffer items, each sub-item will call this many times). Wall-clock time is usually low according to systrace (sometimes there are odd spikes), but over time these do add up and it seems good to save a few ms here and there. Changelog: [Internal] Reviewed By: javache Differential Revision: D36539399 fbshipit-source-id: 3e023be64b8c9f0e6c3c8347c077ce9fa38f74a4
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b36afe74ca
commit
16ce685708
@@ -10,6 +10,7 @@ package com.facebook.react.bridge;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.react.common.build.ReactBuildConfig;
|
||||
|
||||
/** Utility for interacting with the UI thread. */
|
||||
public class UiThreadUtil {
|
||||
@@ -21,14 +22,26 @@ public class UiThreadUtil {
|
||||
return Looper.getMainLooper().getThread() == Thread.currentThread();
|
||||
}
|
||||
|
||||
/** Throws an {@link AssertionException} if the current thread is not the UI thread. */
|
||||
/**
|
||||
* Throws an {@link AssertionException} if the current thread is not the UI thread. This is a noop
|
||||
* in production, and is only meant to run in debug mode! If you need to check for
|
||||
* incorrect-thread issues in production, duplicate this code and call it elsewhere.
|
||||
*/
|
||||
public static void assertOnUiThread() {
|
||||
SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!");
|
||||
if (ReactBuildConfig.DEBUG) {
|
||||
SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!");
|
||||
}
|
||||
}
|
||||
|
||||
/** Throws an {@link AssertionException} if the current thread is the UI thread. */
|
||||
/**
|
||||
* Throws an {@link AssertionException} if the current thread is the UI thread. This is a noop in
|
||||
* production, and is only meant to run in debug mode! If you need to check for incorrect-thread
|
||||
* issues in production, duplicate this code and call it elsewhere.
|
||||
*/
|
||||
public static void assertNotOnUiThread() {
|
||||
SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!");
|
||||
if (ReactBuildConfig.DEBUG) {
|
||||
SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!");
|
||||
}
|
||||
}
|
||||
|
||||
/** Runs the given {@code Runnable} on the UI thread. */
|
||||
|
||||
Reference in New Issue
Block a user