Skip to content

Commit c4046d6

Browse files
VenryxFacebook Github Bot
authored and
Facebook Github Bot
committed
* Reduce emit overhead to ~50% of prior, by caching class-name of java-script module/interface.
Summary: Made modification to react-native code that reduces the communication channel overhead to ~50% of prior, in some cases, by caching the class-name of the java-script module/interface. For me it reduced the run-time of the RCTDeviceEventEmitter.emit function from 1438ms to 715ms, over a period of 8 seconds in my Android app. My project requires many emit calls, as I'm transferring real-time EEG data from a Muse headband to my react-native UI to be graphed, so this optimization was very helpful in my case. Closes #11118 Reviewed By: astreet Differential Revision: D4232794 Pulled By: javache fbshipit-source-id: 25ca1cfc170a343e71ff8915c3fa7e38884a402b
1 parent 30152ff commit c4046d6

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistration.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package com.facebook.react.bridge;
1111

1212
import javax.annotation.concurrent.Immutable;
13+
import javax.annotation.Nullable;
1314

1415
import java.lang.reflect.Method;
1516
import java.util.Arrays;
@@ -26,6 +27,7 @@
2627
public class JavaScriptModuleRegistration {
2728

2829
private final Class<? extends JavaScriptModule> mModuleInterface;
30+
private @Nullable String mName;
2931

3032
public JavaScriptModuleRegistration(Class<? extends JavaScriptModule> moduleInterface) {
3133
mModuleInterface = moduleInterface;
@@ -45,17 +47,22 @@ public JavaScriptModuleRegistration(Class<? extends JavaScriptModule> moduleInte
4547
public Class<? extends JavaScriptModule> getModuleInterface() {
4648
return mModuleInterface;
4749
}
48-
50+
4951
public String getName() {
50-
// With proguard obfuscation turned on, proguard apparently (poorly) emulates inner classes or
51-
// something because Class#getSimpleName() no longer strips the outer class name. We manually
52-
// strip it here if necessary.
53-
String name = mModuleInterface.getSimpleName();
54-
int dollarSignIndex = name.lastIndexOf('$');
55-
if (dollarSignIndex != -1) {
56-
name = name.substring(dollarSignIndex + 1);
52+
if (mName == null) {
53+
// With proguard obfuscation turned on, proguard apparently (poorly) emulates inner classes or
54+
// something because Class#getSimpleName() no longer strips the outer class name. We manually
55+
// strip it here if necessary.
56+
String name = mModuleInterface.getSimpleName();
57+
int dollarSignIndex = name.lastIndexOf('$');
58+
if (dollarSignIndex != -1) {
59+
name = name.substring(dollarSignIndex + 1);
60+
}
61+
62+
// getting the class name every call is expensive, so cache it
63+
mName = name;
5764
}
58-
return name;
65+
return mName;
5966
}
6067

6168
public List<Method> getMethods() {

0 commit comments

Comments
 (0)