Skip to content

Commit

Permalink
Simplify Catalyst handleMemoryPressure
Browse files Browse the repository at this point in the history
Reviewed By: cwdick

Differential Revision: D5200555

fbshipit-source-id: 86f12acca33ece265d3482ba52de9afcc83173cd
  • Loading branch information
javache authored and facebook-github-bot committed Jun 26, 2017
1 parent e2dff82 commit 83faa4b
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,18 @@
/**
* Translates and routes memory pressure events to the current catalyst instance.
*/
public class MemoryPressureRouter {
public class MemoryPressureRouter implements ComponentCallbacks2 {
// Trigger this by sending an intent to your activity with adb shell:
// am broadcast -a com.facebook.catalyst.ACTION_TRIM_MEMORY_MODERATE
// am broadcast -a com.facebook.react.ACTION_TRIM_MEMORY_MODERATE
private static final String ACTION_TRIM_MEMORY_UI_HIDDEN =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_UI_HIDDEN";
"com.facebook.react.ACTION_TRIM_MEMORY_UI_HIDDEN";
private static final String ACTION_TRIM_MEMORY_MODERATE =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_MODERATE";
"com.facebook.react.ACTION_TRIM_MEMORY_MODERATE";
private static final String ACTION_TRIM_MEMORY_CRITICAL =
"com.facebook.rnfeed.ACTION_TRIM_MEMORY_CRITICAL";
"com.facebook.react.ACTION_TRIM_MEMORY_CRITICAL";

private final Set<MemoryPressureListener> mListeners =
Collections.synchronizedSet(new LinkedHashSet<MemoryPressureListener>());
private final ComponentCallbacks2 mCallbacks = new ComponentCallbacks2() {
@Override
public void onTrimMemory(int level) {
trimMemory(level);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
}

@Override
public void onLowMemory() {
}
};

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean handleDebugIntent(Application application, String action) {
Expand All @@ -71,7 +57,11 @@ public static boolean handleDebugIntent(Application application, String action)
}

MemoryPressureRouter(Context context) {
context.getApplicationContext().registerComponentCallbacks(mCallbacks);
context.getApplicationContext().registerComponentCallbacks(this);
}

public void destroy(Context context) {
context.getApplicationContext().unregisterComponentCallbacks(this);
}

/**
Expand All @@ -88,11 +78,8 @@ public void removeMemoryPressureListener(MemoryPressureListener listener) {
mListeners.remove(listener);
}

public void destroy(Context context) {
context.getApplicationContext().unregisterComponentCallbacks(mCallbacks);
}

private void trimMemory(int level) {
@Override
public void onTrimMemory(int level) {
if (level >= TRIM_MEMORY_COMPLETE) {
dispatchMemoryPressure(MemoryPressure.CRITICAL);
} else if (level >= TRIM_MEMORY_BACKGROUND || level == TRIM_MEMORY_RUNNING_CRITICAL) {
Expand All @@ -102,6 +89,14 @@ private void trimMemory(int level) {
}
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
}

@Override
public void onLowMemory() {
}

private void dispatchMemoryPressure(MemoryPressure level) {
// copy listeners array to avoid ConcurrentModificationException if any of the listeners remove
// themselves in handleMemoryPressure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,26 +393,14 @@ public Collection<NativeModule> getNativeModules() {
return mNativeModuleRegistry.getAllModules();
}

private native void handleMemoryPressureUiHidden();
private native void handleMemoryPressureModerate();
private native void handleMemoryPressureCritical();
private native void jniHandleMemoryPressure(int level);

@Override
public void handleMemoryPressure(MemoryPressure level) {
if (mDestroyed) {
return;
}
switch (level) {
case UI_HIDDEN:
handleMemoryPressureUiHidden();
break;
case MODERATE:
handleMemoryPressureModerate();
break;
case CRITICAL:
handleMemoryPressureCritical();
break;
}
jniHandleMemoryPressure(level.ordinal());
}

/**
Expand Down
1 change: 1 addition & 0 deletions ReactAndroid/src/main/jni/react/jni/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ cxx_library(
"-DLOG_TAG=\"ReactNativeJNI\"",
"-DWITH_FBSYSTRACE=1",
"-DWITH_INSPECTOR=1",
"-DWITH_JSC_MEMORY_PRESSURE=1",
],
soname = "libreactnativejni.$(ext)",
visibility = [
Expand Down
18 changes: 5 additions & 13 deletions ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ void CatalystInstanceImpl::registerNatives() {
makeNativeMethod("jniCallJSCallback", CatalystInstanceImpl::jniCallJSCallback),
makeNativeMethod("setGlobalVariable", CatalystInstanceImpl::setGlobalVariable),
makeNativeMethod("getJavaScriptContext", CatalystInstanceImpl::getJavaScriptContext),
makeNativeMethod("handleMemoryPressureUiHidden", CatalystInstanceImpl::handleMemoryPressureUiHidden),
makeNativeMethod("handleMemoryPressureModerate", CatalystInstanceImpl::handleMemoryPressureModerate),
makeNativeMethod("handleMemoryPressureCritical", CatalystInstanceImpl::handleMemoryPressureCritical),
makeNativeMethod("jniHandleMemoryPressure", CatalystInstanceImpl::handleMemoryPressure),
makeNativeMethod("supportsProfiling", CatalystInstanceImpl::supportsProfiling),
makeNativeMethod("startProfiler", CatalystInstanceImpl::startProfiler),
makeNativeMethod("stopProfiler", CatalystInstanceImpl::stopProfiler),
Expand Down Expand Up @@ -262,16 +260,10 @@ jlong CatalystInstanceImpl::getJavaScriptContext() {
return (jlong) (intptr_t) instance_->getJavaScriptContext();
}

void CatalystInstanceImpl::handleMemoryPressureUiHidden() {
instance_->handleMemoryPressureUiHidden();
}

void CatalystInstanceImpl::handleMemoryPressureModerate() {
instance_->handleMemoryPressureModerate();
}

void CatalystInstanceImpl::handleMemoryPressureCritical() {
instance_->handleMemoryPressureCritical();
void CatalystInstanceImpl::handleMemoryPressure(int pressureLevel) {
#ifdef WITH_JSC_MEMORY_PRESSURE
instance_->handleMemoryPressure(pressureLevel);
#endif
}

jboolean CatalystInstanceImpl::supportsProfiling() {
Expand Down
4 changes: 1 addition & 3 deletions ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
void setGlobalVariable(std::string propName,
std::string&& jsonValue);
jlong getJavaScriptContext();
void handleMemoryPressureUiHidden();
void handleMemoryPressureModerate();
void handleMemoryPressureCritical();
void handleMemoryPressure(int pressureLevel);
jboolean supportsProfiling();
void startProfiler(const std::string& title);
void stopProfiler(const std::string& title, const std::string& filename);
Expand Down
14 changes: 4 additions & 10 deletions ReactCommon/cxxreact/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,11 @@ void Instance::callJSCallback(uint64_t callbackId, folly::dynamic&& params) {
nativeToJsBridge_->invokeCallback((double) callbackId, std::move(params));
}

void Instance::handleMemoryPressureUiHidden() {
nativeToJsBridge_->handleMemoryPressureUiHidden();
}

void Instance::handleMemoryPressureModerate() {
nativeToJsBridge_->handleMemoryPressureModerate();
}

void Instance::handleMemoryPressureCritical() {
nativeToJsBridge_->handleMemoryPressureCritical();
#ifdef WITH_JSC_MEMORY_PRESSURE
void Instance::handleMemoryPressure(int pressureLevel) {
nativeToJsBridge_->handleMemoryPressure(pressureLevel);
}
#endif

} // namespace react
} // namespace facebook
6 changes: 3 additions & 3 deletions ReactCommon/cxxreact/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class Instance {
return nativeToJsBridge_->callFunctionSync(module, method, std::forward<T>(args));
}

void handleMemoryPressureUiHidden();
void handleMemoryPressureModerate();
void handleMemoryPressureCritical();
#ifdef WITH_JSC_MEMORY_PRESSURE
void handleMemoryPressure(int pressureLevel);
#endif

private:
void callNativeModules(folly::dynamic&& calls, bool isEndOfBatch);
Expand Down
20 changes: 4 additions & 16 deletions ReactCommon/cxxreact/JSCExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,23 +579,11 @@ void JSCExecutor::stopProfiler(const std::string &titleString, const std::string
#endif
}

void JSCExecutor::handleMemoryPressureUiHidden() {
#ifdef WITH_JSC_MEMORY_PRESSURE
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::UI_HIDDEN);
#endif
}

void JSCExecutor::handleMemoryPressureModerate() {
#ifdef WITH_JSC_MEMORY_PRESSURE
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::MODERATE);
#endif
}

void JSCExecutor::handleMemoryPressureCritical() {
#ifdef WITH_JSC_MEMORY_PRESSURE
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::CRITICAL);
#endif
#ifdef WITH_JSC_MEMORY_PRESSURE
void JSCExecutor::handleMemoryPressure(int pressureLevel) {
JSHandleMemoryPressure(this, m_context, static_cast<JSMemoryPressure>(pressureLevel));
}
#endif

void JSCExecutor::flushQueueImmediate(Value&& queue) {
auto queueStr = queue.toJSONString();
Expand Down
6 changes: 3 additions & 3 deletions ReactCommon/cxxreact/JSCExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class RN_EXPORT JSCExecutor : public JSExecutor {
virtual void startProfiler(const std::string &titleString) override;
virtual void stopProfiler(const std::string &titleString, const std::string &filename) override;

virtual void handleMemoryPressureUiHidden() override;
virtual void handleMemoryPressureModerate() override;
virtual void handleMemoryPressureCritical() override;
#ifdef WITH_JSC_MEMORY_PRESSURE
virtual void handleMemoryPressure(int pressureLevel) override;
#endif

virtual void destroy() override;

Expand Down
10 changes: 5 additions & 5 deletions ReactCommon/cxxreact/JSExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ class JSExecutor {
}
virtual void startProfiler(const std::string &titleString) {}
virtual void stopProfiler(const std::string &titleString, const std::string &filename) {}
virtual void handleMemoryPressureUiHidden() {}
virtual void handleMemoryPressureModerate() {}
virtual void handleMemoryPressureCritical() {
handleMemoryPressureModerate();
}

#ifdef WITH_JSC_MEMORY_PRESSURE
virtual void handleMemoryPressure(int pressureLevel) {}
#endif

virtual void destroy() {}
virtual ~JSExecutor() {}
};
Expand Down
18 changes: 4 additions & 14 deletions ReactCommon/cxxreact/NativeToJsBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,13 @@ void NativeToJsBridge::stopProfiler(const std::string& title, const std::string&
});
}

void NativeToJsBridge::handleMemoryPressureUiHidden() {
#ifdef WITH_JSC_MEMORY_PRESSURE
void NativeToJsBridge::handleMemoryPressure(int pressureLevel) {
runOnExecutorQueue([=] (JSExecutor* executor) {
executor->handleMemoryPressureUiHidden();
});
}

void NativeToJsBridge::handleMemoryPressureModerate() {
runOnExecutorQueue([=] (JSExecutor* executor) {
executor->handleMemoryPressureModerate();
});
}

void NativeToJsBridge::handleMemoryPressureCritical() {
runOnExecutorQueue([=] (JSExecutor* executor) {
executor->handleMemoryPressureCritical();
executor->handleMemoryPressure(pressureLevel);
});
}
#endif

void NativeToJsBridge::destroy() {
// All calls made through runOnExecutorQueue have an early exit if
Expand Down
7 changes: 4 additions & 3 deletions ReactCommon/cxxreact/NativeToJsBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ class NativeToJsBridge {
bool supportsProfiling();
void startProfiler(const std::string& title);
void stopProfiler(const std::string& title, const std::string& filename);
void handleMemoryPressureUiHidden();
void handleMemoryPressureModerate();
void handleMemoryPressureCritical();

#ifdef WITH_JSC_MEMORY_PRESSURE
void handleMemoryPressure(int pressureLevel);
#endif

/**
* Synchronously tears down the bridge and the main executor.
Expand Down

0 comments on commit 83faa4b

Please sign in to comment.