Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve init and runtime performance of native modules #10084

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ android {
}

dependencies {
annotationProcessor project(':processor');
compile fileTree(dir: 'src/main/third-party/java/infer-annotations/', include: ['*.jar'])
compile 'javax.inject:javax.inject:1'
compile 'com.android.support:appcompat-v7:23.0.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.facebook.react.bridge;

import java.util.List;
import com.facebook.common.logging.FLog;

/**
* Accelerated version of JavaModuleWrapper
* Created by ransj on 2017/8/7.
*/

public final class AcJavaModuleWrapper extends JavaModuleWrapper {

private static ModuleProvider mCoreModuleProvider;
private static ModuleProvider mCustomModuleProvider;
private final ModuleHelper mModuleHelper;

static {
String clsName = JavaModuleWrapper.class.getName();
try {
// CoreModuleProvider generated by ReactNativeModuleProcessor.java,
// aim to provide ModuleHelper for official native modules
Class<?> coreHelperCls = Class.forName(clsName + "$CoreModuleProvider");
//noinspection unchecked
mCoreModuleProvider = (ModuleProvider) coreHelperCls.newInstance();
} catch (ClassNotFoundException e) {
FLog.w("JavaModuleHelper", "Could not find generated core module provider");
} catch (InstantiationException e) {
throw new RuntimeException("Unable to instantiate core module provider ", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Unable to instantiate core module provider ", e);
}
try {
// CustomModuleProvider generated by ReactNativeModuleProcessor.java,
// aim to provide ModuleHelper for third-party native modules
Class<?> customHelperCls = Class.forName(clsName + "$CustomModuleProvider");
//noinspection unchecked
mCustomModuleProvider = (ModuleProvider) customHelperCls.newInstance();
} catch (ClassNotFoundException e) {
FLog.w("JavaModuleHelper", "Could not find generated custom module provider");
} catch (InstantiationException e) {
throw new RuntimeException("Unable to instantiate custom module provider ", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Unable to instantiate custom module provider ", e);
}
}

public AcJavaModuleWrapper(JSInstance jsInstance, Class<? extends NativeModule> moduleClass, ModuleHolder moduleHolder) {
super(jsInstance, moduleClass, moduleHolder);
ModuleHelper helper = null;
if (helper == null && mCustomModuleProvider != null) {
helper = mCustomModuleProvider.getModuleHelper(getModule());
}
if (mCoreModuleProvider != null) {
helper = mCoreModuleProvider.getModuleHelper(getModule());
}
mModuleHelper = helper;
}

@Override
public List<MethodDescriptor> getMethodDescriptors() {
return mModuleHelper == null ? super.getMethodDescriptors() : mModuleHelper.getMethodDescriptors();
}

@Override
public void invoke(int methodId, ReadableNativeArray parameters) {
if (mModuleHelper == null) {
super.invoke(methodId, parameters);
} else {
mModuleHelper.invoke(mJSInstance, methodId, parameters);
}
}

public interface ModuleHelper {

/**
* @return
*/
List<MethodDescriptor> getMethodDescriptors();

/**
* @param jsInstance
* @param methodId
* @param parameters
*/
void invoke(JSInstance jsInstance, int methodId, ReadableNativeArray parameters);
}

interface ModuleProvider {
ModuleHelper getModuleHelper(BaseJavaModule module);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,8 @@

@DoNotStrip
public class JavaModuleWrapper {
@DoNotStrip
public class MethodDescriptor {
@DoNotStrip
Method method;
@DoNotStrip
String signature;
@DoNotStrip
String name;
@DoNotStrip
String type;
}

private final JSInstance mJSInstance;
protected final JSInstance mJSInstance;
private final ModuleHolder mModuleHolder;
private final Class<? extends NativeModule> mModuleClass;
private final ArrayList<NativeModule.NativeMethod> mMethods;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.facebook.react.bridge;

import com.facebook.proguard.annotations.DoNotStrip;

import java.lang.reflect.Method;

/**
* Created by ransj on 2017/8/14.
*/

@DoNotStrip
public class MethodDescriptor {
@DoNotStrip
Method method;
@DoNotStrip
String signature;
@DoNotStrip
String name;
@DoNotStrip
String type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private ArrayList<ModuleHolder> getBatchCompleteListenerModules() {
for (Map.Entry<Class<? extends NativeModule>, ModuleHolder> entry : mModules.entrySet()) {
Class<? extends NativeModule> type = entry.getKey();
if (!CxxModuleWrapperBase.class.isAssignableFrom(type)) {
javaModules.add(new JavaModuleWrapper(jsInstance, type, entry.getValue()));
javaModules.add(new AcJavaModuleWrapper(jsInstance, type, entry.getValue()));
}
}
return javaModules;
Expand Down