From c4046d62a7cff03ce03f2099075eb083ade09c23 Mon Sep 17 00:00:00 2001 From: Venryx Date: Tue, 29 Nov 2016 02:43:49 -0800 Subject: [PATCH] * 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 https://github.com/facebook/react-native/pull/11118 Reviewed By: astreet Differential Revision: D4232794 Pulled By: javache fbshipit-source-id: 25ca1cfc170a343e71ff8915c3fa7e38884a402b --- .../bridge/JavaScriptModuleRegistration.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistration.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistration.java index afa32d964b7827..53ed41d2046127 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistration.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistration.java @@ -10,6 +10,7 @@ package com.facebook.react.bridge; import javax.annotation.concurrent.Immutable; +import javax.annotation.Nullable; import java.lang.reflect.Method; import java.util.Arrays; @@ -26,6 +27,7 @@ public class JavaScriptModuleRegistration { private final Class mModuleInterface; + private @Nullable String mName; public JavaScriptModuleRegistration(Class moduleInterface) { mModuleInterface = moduleInterface; @@ -45,17 +47,22 @@ public JavaScriptModuleRegistration(Class moduleInte public Class getModuleInterface() { return mModuleInterface; } - + public String getName() { - // With proguard obfuscation turned on, proguard apparently (poorly) emulates inner classes or - // something because Class#getSimpleName() no longer strips the outer class name. We manually - // strip it here if necessary. - String name = mModuleInterface.getSimpleName(); - int dollarSignIndex = name.lastIndexOf('$'); - if (dollarSignIndex != -1) { - name = name.substring(dollarSignIndex + 1); + if (mName == null) { + // With proguard obfuscation turned on, proguard apparently (poorly) emulates inner classes or + // something because Class#getSimpleName() no longer strips the outer class name. We manually + // strip it here if necessary. + String name = mModuleInterface.getSimpleName(); + int dollarSignIndex = name.lastIndexOf('$'); + if (dollarSignIndex != -1) { + name = name.substring(dollarSignIndex + 1); + } + + // getting the class name every call is expensive, so cache it + mName = name; } - return name; + return mName; } public List getMethods() {