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

Performance optimization #1879

Merged
merged 31 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
588321b
Basic performance optimization
piaskowyk Mar 30, 2021
b3016a3
Reduced global() calls
piaskowyk Apr 1, 2021
cee453d
Merge & conflicts
piaskowyk Apr 6, 2021
6244908
Colors interpolation improvement
piaskowyk Apr 7, 2021
5c2f757
Merge branch 'master' into @piaskowyk/performance-research
piaskowyk Apr 7, 2021
7dcfc48
Fix TS type
piaskowyk Apr 7, 2021
5d26155
updateProps
piaskowyk Apr 23, 2021
5623891
Remove nested hooks
piaskowyk Apr 26, 2021
d13bf41
Rewriting mappers to C++
piaskowyk Apr 27, 2021
c424b2f
Detect simple object with babel
piaskowyk Apr 28, 2021
3fea952
Improved optimalization detect
piaskowyk Apr 29, 2021
e08c859
Clean up in Mapper
piaskowyk Apr 29, 2021
1919449
Remove unnecesary files
piaskowyk Apr 29, 2021
2c28178
Merge branch 'master' into @piaskowyk/performance-research
piaskowyk Apr 29, 2021
853e726
Revert changes
piaskowyk Apr 29, 2021
6de0f02
Revert changes
piaskowyk Apr 29, 2021
e51eea2
Run prettier
piaskowyk Apr 29, 2021
1fce654
Added mapper default arguments
piaskowyk May 5, 2021
ff6b27f
Fix tests
piaskowyk May 6, 2021
f50bdb3
Remove comments
piaskowyk May 6, 2021
48e1a2a
Runtime type check
piaskowyk May 6, 2021
c5f001f
Added compatibiliy with multithreading library
piaskowyk May 6, 2021
c13b53e
Merge with master
piaskowyk May 6, 2021
2b30e00
Revert lint changes
piaskowyk May 6, 2021
b159fe8
Perf/faster runtime checks (#1997)
mrousavy May 7, 2021
fbc9230
Fix linker error
piaskowyk May 7, 2021
f191acb
tmp
piaskowyk May 7, 2021
d3cd1b2
Merge with master
piaskowyk May 28, 2021
9ab2bd1
Resolve conflict
piaskowyk Jun 1, 2021
d14fda3
Refactor flags checking
piaskowyk Jun 1, 2021
e37a061
Added buffer limit
piaskowyk Jun 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Common/cpp/NativeModules/NativeReanimatedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ NativeReanimatedModule::NativeReanimatedModule(std::shared_ptr<CallInvoker> jsIn
platformDepMethodsHolder.scrollToFunction,
platformDepMethodsHolder.measuringFunction,
platformDepMethodsHolder.getCurrentTime);
onRenderCallback = [this](double timestampMs) {
this->renderRequested = false;
this->onRender(timestampMs);
};
}

void NativeReanimatedModule::installCoreFunctions(jsi::Runtime &rt, const jsi::Value &valueSetter)
Expand Down Expand Up @@ -210,10 +214,7 @@ void NativeReanimatedModule::maybeRequestRender()
if (!renderRequested)
{
renderRequested = true;
requestRender([this](double timestampMs) {
this->renderRequested = false;
this->onRender(timestampMs);
}, *this->runtime);
requestRender(onRenderCallback, *this->runtime);
}
}

Expand All @@ -223,7 +224,7 @@ void NativeReanimatedModule::onRender(double timestampMs)
{
std::vector<FrameCallback> callbacks = frameCallbacks;
frameCallbacks.clear();
for (auto callback : callbacks)
for (auto& callback : callbacks)
{
callback(timestampMs);
}
Expand Down
6 changes: 3 additions & 3 deletions Common/cpp/SharedItems/MutableValueSetterProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ namespace reanimated {

void MutableValueSetterProxy::set(jsi::Runtime &rt, const jsi::PropNameID &name, const jsi::Value &newValue) {
auto propName = name.utf8(rt);
if (propName == "value") {
// you call `this.value` inside of value setter, we should throw
} else if (propName == "_value") {
if (propName == "_value") {
mutableValue->setValue(rt, newValue);
} else if (propName == "_animation") {
// TODO: assert to allow animation to be set from UI only
if (mutableValue->animation.expired()) {
mutableValue->animation = mutableValue->getWeakRef(rt);
}
*mutableValue->animation.lock() = jsi::Value(rt, newValue);
} else if (propName == "value") {
// you call `this.value` inside of value setter, we should throw
}
}

Expand Down
30 changes: 15 additions & 15 deletions Common/cpp/SharedItems/ShareableValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void ShareableValue::adaptCache(jsi::Runtime &rt, const jsi::Value &value) {
}

void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType objectType) {
bool isRNRuntime = RuntimeDecorator::isReactRuntime(rt);
if (value.isObject()) {
jsi::Object object = value.asObject(rt);
jsi::Value hiddenValue = object.getProperty(rt, HIDDEN_HOST_OBJECT_PROP);
Expand Down Expand Up @@ -117,7 +116,7 @@ void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType
valueContainer = std::make_unique<FrozenObjectWrapper>(std::make_shared<FrozenObject>(rt, object, runtimeManager));
auto& frozenObject = ValueWrapper::asFrozenObject(valueContainer);
containsHostFunction |= frozenObject->containsHostFunction;
if (isRNRuntime && !containsHostFunction) {
if (RuntimeDecorator::isReactRuntime(rt) && !containsHostFunction) {
addHiddenProperty(rt, createHost(rt, frozenObject), object, HIDDEN_HOST_OBJECT_PROP);
}
}
Expand Down Expand Up @@ -154,7 +153,7 @@ void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType
);
auto& frozenObject = ValueWrapper::asFrozenObject(valueContainer);
containsHostFunction |= frozenObject->containsHostFunction;
if (isRNRuntime) {
if (RuntimeDecorator::isReactRuntime(rt)) {
if (!containsHostFunction) {
addHiddenProperty(rt, createHost(rt, frozenObject), object, HIDDEN_HOST_OBJECT_PROP);
}
Expand All @@ -179,17 +178,16 @@ jsi::Value ShareableValue::getValue(jsi::Runtime &rt) {
// TODO: maybe we can cache toJSValue results on a per-runtime basis, need to avoid ref loops
if (RuntimeDecorator::isWorkletRuntime(rt)) {
if (remoteValue.expired()) {
auto ref = getWeakRef(rt);
remoteValue = ref;
remoteValue = getWeakRef(rt);
}

if (remoteValue.lock()->isUndefined()) {
(*remoteValue.lock()) = jsi::Value(rt, toJSValue(rt));
(*remoteValue.lock()) = toJSValue(rt);
}
return jsi::Value(rt, *remoteValue.lock());
} else {
if (hostValue.get() == nullptr) {
hostValue = std::make_unique<jsi::Value>(rt, toJSValue(rt));
hostValue = std::make_unique<jsi::Value>(toJSValue(rt));
}
return jsi::Value(rt, *hostValue);
}
Expand Down Expand Up @@ -339,8 +337,10 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
const jsi::Value *args,
size_t count
) mutable -> jsi::Value {
jsi::Value oldJSThis = rt.global().getProperty(rt, "jsThis");
rt.global().setProperty(rt, "jsThis", *jsThis); //set jsThis
const jsi::String jsThisName = jsi::String::createFromAscii(rt, "jsThis");
jsi::Object global = rt.global();
jsi::Value oldJSThis = global.getProperty(rt, jsThisName);
global.setProperty(rt, jsThisName, *jsThis); //set jsThis

jsi::Value res = jsi::Value::undefined();
try {
Expand All @@ -361,8 +361,7 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
runtimeManager->errorHandler->setError(str);
runtimeManager->errorHandler->raise();
}

rt.global().setProperty(rt, "jsThis", oldJSThis); //clean jsThis
global.setProperty(rt, jsThisName, oldJSThis); //clean jsThis
return res;
};
return jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, name.c_str()), 0, clb);
Expand Down Expand Up @@ -394,9 +393,10 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
}

jsi::Value returnedValue;

jsi::Value oldJSThis = rt.global().getProperty(rt, "jsThis");
rt.global().setProperty(rt, "jsThis", jsThis); //set jsThis
const jsi::String jsThisName = jsi::String::createFromAscii(rt, "jsThis");
jsi::Object global = rt.global();
jsi::Value oldJSThis = global.getProperty(rt, jsThisName);
global.setProperty(rt, jsThisName, jsThis); //set jsThis
try {
returnedValue = funPtr->call(rt,
static_cast<const jsi::Value*>(args),
Expand All @@ -416,7 +416,7 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
runtimeManager->errorHandler->setError(str);
runtimeManager->errorHandler->raise();
}
rt.global().setProperty(rt, "jsThis", oldJSThis); //clean jsThis
global.setProperty(rt, jsThisName, oldJSThis); //clean jsThis

delete [] args;
// ToDo use returned value to return promise
Expand Down
3 changes: 2 additions & 1 deletion Common/cpp/headers/NativeModules/NativeReanimatedModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec, public Runtime
private:
std::shared_ptr<MapperRegistry> mapperRegistry;
std::shared_ptr<EventHandlerRegistry> eventHandlerRegistry;
std::function<void(FrameCallback, jsi::Runtime&)> requestRender;
std::function<void(FrameCallback&, jsi::Runtime&)> requestRender;
std::shared_ptr<jsi::Value> dummyEvent;
std::vector<FrameCallback> frameCallbacks;
bool renderRequested = false;
std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)> propObtainer;
std::function<void(double)> onRenderCallback;
};

} // namespace reanimated
16 changes: 10 additions & 6 deletions android/src/main/cpp/NativeProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ void NativeProxy::installJSIBindings()
//as we use diffrent timer to better handle events. The lambda is translated to NodeManager.OnAnimationFrame
//and treated just like reanimated 1 frame callbacks which make use of the timestamp.
auto wrappedOnRender = [getCurrentTime, &rt, onRender](double doNotUse) {
double frameTimestamp = getCurrentTime();
rt.global().setProperty(rt, "_frameTimestamp", frameTimestamp);
onRender(frameTimestamp);
rt.global().setProperty(rt, "_frameTimestamp", jsi::Value::undefined());
jsi::Object global = rt.global();
jsi::String frameTimestampName = jsi::String::createFromAscii(rt, "_frameTimestamp");
double frameTimestamp = getCurrentTime();
global.setProperty(rt, frameTimestampName, frameTimestamp);
onRender(frameTimestamp);
global.setProperty(rt, frameTimestampName, jsi::Value::undefined());
};
this->requestRender(std::move(wrappedOnRender));
};
Expand Down Expand Up @@ -119,9 +121,11 @@ void NativeProxy::installJSIBindings()
_nativeReanimatedModule = module;

this->registerEventHandler([module, getCurrentTime](std::string eventName, std::string eventAsString) {
module->runtime->global().setProperty(*module->runtime, "_eventTimestamp", getCurrentTime());
jsi::Object global = module->runtime->global();
jsi::String eventTimestampName = jsi::String::createFromAscii(*module->runtime, "_eventTimestamp");
global.setProperty(*module->runtime, eventTimestampName, getCurrentTime());
module->onEvent(eventName, eventAsString);
module->runtime->global().setProperty(*module->runtime, "_eventTimestamp", jsi::Value::undefined());
global.setProperty(*module->runtime, eventTimestampName, jsi::Value::undefined());
});

runtime_->global().setProperty(
Expand Down
12 changes: 8 additions & 4 deletions ios/native/NativeProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ static id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &v
auto requestRender = [reanimatedModule, &module](std::function<void(double)> onRender, jsi::Runtime &rt) {
[reanimatedModule.nodesManager postOnAnimation:^(CADisplayLink *displayLink) {
double frameTimestamp = displayLink.targetTimestamp * 1000;
rt.global().setProperty(rt, "_frameTimestamp", frameTimestamp);
jsi::Object global = rt.global();
jsi::String frameTimestampName = jsi::String::createFromAscii(rt, "_frameTimestamp");
global.setProperty(rt, frameTimestampName, frameTimestamp);
onRender(frameTimestamp);
rt.global().setProperty(rt, "_frameTimestamp", jsi::Value::undefined());
global.setProperty(rt, frameTimestampName, jsi::Value::undefined());
}];
};

Expand Down Expand Up @@ -157,9 +159,11 @@ static id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &v
std::string eventAsString = folly::toJson(convertIdToFollyDynamic([event arguments][2]));

eventAsString = "{ NativeMap:" + eventAsString + "}";
module->runtime->global().setProperty(*module->runtime, "_eventTimestamp", CACurrentMediaTime() * 1000);
jsi::Object global = module->runtime->global();
jsi::String eventTimestampName = jsi::String::createFromAscii(*module->runtime, "_eventTimestamp");
global.setProperty(*module->runtime, eventTimestampName, CACurrentMediaTime() * 1000);
module->onEvent(eventNameString, eventAsString);
module->runtime->global().setProperty(*module->runtime, "_eventTimestamp", jsi::Value::undefined());
global.setProperty(*module->runtime, eventTimestampName, jsi::Value::undefined());
}];

return module;
Expand Down
Loading