Skip to content

perf: avoid calling executeOnUIRuntimeSync if shared value has initial value #7913

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

Open
wants to merge 5 commits into
base: @tjzel/worklets/synchronizable
Choose a base branch
from
Open
Changes from all commits
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
28 changes: 22 additions & 6 deletions packages/react-native-reanimated/src/mutables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';
import type { Synchronizable } from 'react-native-worklets';
import {
executeOnUIRuntimeSync,
makeShareableCloneRecursive,
makeSynchronizable,
runOnUI,
shareableMappingCache,
} from 'react-native-worklets';
Expand Down Expand Up @@ -94,10 +96,14 @@ function hideInternalValueProp<Value>(mutable: PartialMutable<Value>) {
});
}

export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
export function makeMutableUI<Value>(
initial: Value,
isDirtySynchronizable?: Synchronizable<boolean>
): Mutable<Value> {
'worklet';
const listeners = new Map<number, Listener<Value>>();
let value = initial;
let isDirty = false;

const mutable: PartialMutable<Value> = {
get value() {
Expand All @@ -114,6 +120,10 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
listeners.forEach((listener) => {
listener(newValue);
});
if (isDirtySynchronizable && !isDirty && value !== initial) {
isDirty = true;
isDirtySynchronizable.setBlocking(true);
}
},
modify: (modifier, forceUpdate = true) => {
valueSetter(
Expand All @@ -140,20 +150,26 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
}

function makeMutableNative<Value>(initial: Value): Mutable<Value> {
const isDirtySynchronizable = makeSynchronizable(false);

const handle = makeShareableCloneRecursive({
__init: () => {
'worklet';
return makeMutableUI(initial);
return makeMutableUI(initial, isDirtySynchronizable);
},
});

const mutable: PartialMutable<Value> = {
get value(): Value {
checkInvalidReadDuringRender();
const uiValueGetter = executeOnUIRuntimeSync((sv: Mutable<Value>) => {
return sv.value;
});
return uiValueGetter(mutable as Mutable<Value>);
if (isDirtySynchronizable.getBlocking()) {
const uiValueGetter = executeOnUIRuntimeSync((sv: Mutable<Value>) => {
return sv.value;
});
return uiValueGetter(mutable as Mutable<Value>);
} else {
return initial;
}
},
set value(newValue) {
checkInvalidWriteDuringRender();
Expand Down
Loading