Skip to content

Commit 5bce833

Browse files
perf: avoid calling executeOnUIRuntimeSync if shared value has initial value
1 parent 7450097 commit 5bce833

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

packages/react-native-reanimated/src/mutables.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import {
33
executeOnUIRuntimeSync,
44
makeShareableCloneRecursive,
5+
makeSynchronizable,
6+
SynchronizableRef,
57
runOnUI,
68
shareableMappingCache,
79
} from 'react-native-worklets';
@@ -94,10 +96,14 @@ function hideInternalValueProp<Value>(mutable: PartialMutable<Value>) {
9496
});
9597
}
9698

97-
export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
99+
export function makeMutableUI<Value>(
100+
initial: Value,
101+
isDirtySynchronizable?: SynchronizableRef<number>
102+
): Mutable<Value> {
98103
'worklet';
99104
const listeners = new Map<number, Listener<Value>>();
100105
let value = initial;
106+
let isDirty = false;
101107

102108
const mutable: PartialMutable<Value> = {
103109
get value() {
@@ -114,6 +120,10 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
114120
listeners.forEach((listener) => {
115121
listener(newValue);
116122
});
123+
if (isDirtySynchronizable && !isDirty && value !== initial) {
124+
isDirty = true;
125+
isDirtySynchronizable.setBlocking(1);
126+
}
117127
},
118128
modify: (modifier, forceUpdate = true) => {
119129
valueSetter(
@@ -140,20 +150,26 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
140150
}
141151

142152
function makeMutableNative<Value>(initial: Value): Mutable<Value> {
153+
const isDirtySynchronizable = makeSynchronizable(0);
154+
143155
const handle = makeShareableCloneRecursive({
144156
__init: () => {
145157
'worklet';
146-
return makeMutableUI(initial);
158+
return makeMutableUI(initial, isDirtySynchronizable);
147159
},
148160
});
149161

150162
const mutable: PartialMutable<Value> = {
151163
get value(): Value {
152164
checkInvalidReadDuringRender();
153-
const uiValueGetter = executeOnUIRuntimeSync((sv: Mutable<Value>) => {
154-
return sv.value;
155-
});
156-
return uiValueGetter(mutable as Mutable<Value>);
165+
if (isDirtySynchronizable.getBlocking()) {
166+
const uiValueGetter = executeOnUIRuntimeSync((sv: Mutable<Value>) => {
167+
return sv.value;
168+
});
169+
return uiValueGetter(mutable as Mutable<Value>);
170+
} else {
171+
return initial;
172+
}
157173
},
158174
set value(newValue) {
159175
checkInvalidWriteDuringRender();

0 commit comments

Comments
 (0)