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

Cache worklets #6758

Merged
merged 10 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
PoC
  • Loading branch information
piaskowyk committed Nov 25, 2024
commit d1fce78bc9de17390621fb12b20293e284a88413
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ jsi::Value NativeWorkletsModule::makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) {
const jsi::Value &nativeStateSource,
const jsi::Value &staticFunction) {
// TODO: It might be a good idea to rename one of these methods to avoid
// confusion.
return worklets::makeShareableClone(
rt, value, shouldRetainRemote, nativeStateSource);
rt, value, shouldRetainRemote, nativeStateSource, staticFunction);
}

} // namespace worklets
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class NativeWorkletsModule : public NativeWorkletsModuleSpec {
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) override;
const jsi::Value &nativeStateSource,
const jsi::Value &staticFunction) override;

[[nodiscard]] inline std::string getValueUnpackerCode() const {
return valueUnpackerCode_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static jsi::Value WORKLETS_SPEC_PREFIX(makeShareableClone)(
size_t) {
return static_cast<NativeWorkletsModuleSpec *>(&turboModule)
->makeShareableClone(
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
rt, std::move(args[0]), std::move(args[1]), std::move(args[2]), std::move(args[3]));
}

NativeWorkletsModuleSpec::NativeWorkletsModuleSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class JSI_EXPORT NativeWorkletsModuleSpec : public TurboModule {
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) = 0;
const jsi::Value &nativeStateSource,
const jsi::Value &staticFunction) = 0;
};

} // namespace worklets
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource) {
const jsi::Value &nativeStateSource,
const jsi::Value &staticFunction) {
std::shared_ptr<Shareable> shareable;
if (value.isObject()) {
auto object = value.asObject(rt);
if (!object.getProperty(rt, "__workletHash").isUndefined()) {
shareable = std::make_shared<ShareableWorklet>(rt, object);
if (staticFunction.getBool()) {
shareable = std::make_shared<ShareableStaticWorklet>(rt, object);
} else {
shareable = std::make_shared<ShareableWorklet>(rt, object);
}
} else if (!object.getProperty(rt, "__init").isUndefined()) {
shareable = std::make_shared<ShareableHandle>(rt, object);
} else if (object.isFunction(rt)) {
Expand Down Expand Up @@ -213,8 +218,9 @@ ShareableObject::ShareableObject(
nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt);
}
}

static int counter = 0;
jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) {
counter++;
auto obj = jsi::Object(rt);
for (size_t i = 0, size = data_.size(); i < size; i++) {
obj.setProperty(
Expand Down Expand Up @@ -249,6 +255,23 @@ jsi::Value ShareableWorklet::toJSValue(jsi::Runtime &rt) {
rt, obj, jsi::String::createFromAscii(rt, "Worklet"));
}

jsi::Value ShareableStaticWorklet::toJSValue(jsi::Runtime &rt) {
assert(
std::any_of(
data_.cbegin(),
data_.cend(),
[](const auto &item) { return item.first == "__workletHash"; }) &&
"ShareableWorklet doesn't have `__workletHash` property");
if (jsValue_.get()) {
return jsValue_.get();
}
jsi::Value obj = ShareableObject::toJSValue(rt);
auto jsValue = getValueUnpacker(rt).call(
rt, obj, jsi::String::createFromAscii(rt, "Worklet"));
jsValue_ = std::make_shared<jsi::Value>(std::move(jsValue));
return jsValue;
}

jsi::Value ShareableRemoteFunction::toJSValue(jsi::Runtime &rt) {
if (&rt == runtime_) {
return jsi::Value(rt, *function_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource);
const jsi::Value &nativeStateSource,
const jsi::Value &staticFunction);

std::shared_ptr<Shareable> extractShareableOrThrow(
jsi::Runtime &rt,
Expand Down Expand Up @@ -246,6 +247,17 @@ class ShareableWorklet : public ShareableObject {
jsi::Value toJSValue(jsi::Runtime &rt) override;
};

class ShareableStaticWorklet : public ShareableObject {
public:
ShareableStaticWorklet(jsi::Runtime &rt, const jsi::Object &worklet)
: ShareableObject(rt, worklet) {
valueType_ = WorkletType;
}

jsi::Value toJSValue(jsi::Runtime &rt) override;
std::shared_ptr<jsi::Value> jsValue_;
};

class ShareableRemoteFunction
: public Shareable,
public std::enable_shared_from_this<ShareableRemoteFunction> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void WorkletRuntimeDecorator::decorate(
const jsi::Value &nativeStateSource) {
auto shouldRetainRemote = jsi::Value::undefined();
return makeShareableClone(
rt, value, shouldRetainRemote, nativeStateSource);
rt, value, shouldRetainRemote, nativeStateSource, false);
});

jsi_utils::installJsiFunction(
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-reanimated/src/animation/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,4 @@ export const withTiming = function (
} as TimingAnimation;
});
} as withTimingType;
// withTiming.staticFn = true;
12 changes: 10 additions & 2 deletions packages/react-native-reanimated/src/shareables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function makeShareableCloneRecursive<T>(
const type = typeof value;
const isTypeObject = type === 'object';
const isTypeFunction = type === 'function';
let isStaticFunction = false;
if ((isTypeObject || isTypeFunction) && value !== null) {
const cached = shareableMappingCache.get(value);
if (cached === shareableMappingFlag) {
Expand Down Expand Up @@ -166,6 +167,11 @@ export function makeShareableCloneRecursive<T>(
} else if (isPlainJSObject(value) || isTypeFunction) {
toAdapt = {};
if (isWorkletFunction(value)) {
if (value.staticFn) {
console.log('value', value);
isStaticFunction = true;
}
// console.log('value', value);
if (__DEV__) {
const babelVersion = value.__initData.version;
if (babelVersion !== undefined && babelVersion !== jsVersion) {
Expand Down Expand Up @@ -276,7 +282,8 @@ Offending code was: \`${getWorkletCode(value)}\``);
const adapted = WorkletsModule.makeShareableClone(
toAdapt,
shouldPersistRemote,
value
value,
isStaticFunction
);
shareableMappingCache.set(value, adapted);
shareableMappingCache.set(adapted);
Expand All @@ -286,7 +293,8 @@ Offending code was: \`${getWorkletCode(value)}\``);
return WorkletsModule.makeShareableClone(
value,
shouldPersistRemote,
undefined
undefined,
false
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
makeShareableClone<T>(
value: T,
shouldPersistRemote: boolean,
nativeStateSource?: object
nativeStateSource?: object,
staticFunction: boolean = false
) {
return this.#workletsModuleProxy.makeShareableClone(
value,
shouldPersistRemote,
nativeStateSource
nativeStateSource,
staticFunction
);
}
}