Commit 80006ff
authored
[General] Reduce number of created closures (#3853)
## Description
I noticed that `useGestureCallbacks` still took significant time to run,
despite not doing that much. I narrowed it down to the fact that it
created a lot of closures for different events, while we only need two:
one for JS and the other for Reanimated.
This PR refactors how the state machine is created for gestures to
reduce the number of closures created to 2 per `useGestureCallbacks`
call.
## Test plan
<details>
<summary>Tested on the stress-test</summary>
```jsx
import React, { Profiler, useEffect, useRef } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { Gesture, GestureDetector, usePanGesture } from 'react-native-gesture-handler';
import { PerfMonitor } from 'react-native-gesture-handler/src/v3/PerfMonitor';
import Animated, {
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated';
const DATA = new Array(500).fill(null).map((_, i) => `Item ${i + 1}`);
function Item() {
const translateX = useSharedValue(0);
const style = useAnimatedStyle(() => {
return {
transform: [{ translateX: translateX.value }],
};
});
const pan = usePanGesture({
disableReanimated: false,
onUpdate: (event) => {
'worklet';
console.log('pan onUpdate', event.changeX);
},
onActivate: () => {
'worklet';
console.log('pan onStart', _WORKLET);
},
onDeactivate: () => {
'worklet';
console.log('pan onEnd');
},
onBegin: () => {
'worklet';
console.log('pan onBegin');
},
onFinalize: () => {
'worklet';
console.log('pan onFinalize');
},
onTouchesDown: () => {
'worklet';
console.log('pan onTouchesDown');
},
});
return (
<View style={{ height: 80, padding: 16, backgroundColor: 'gray' }}>
<GestureDetector gesture={pan}>
{/* <View collapsable={false} style={{opacity: 0.5}}> */}
<Animated.View
style={[
{ backgroundColor: 'red', height: '100%', aspectRatio: 1 },
style,
]}
/>
{/* </View> */}
</GestureDetector>
</View>
);
}
function Benchmark() {
return (
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={{ flexGrow: 1, gap: 8 }}>
{DATA.map((_, index) => (
<Item key={index} />
))}
</ScrollView>
);
}
const TIMES = 35;
export default function EmptyExample() {
const times = useRef<number[]>([]).current;
const [visible, setVisible] = React.useState(false);
useEffect(() => {
if (!visible && times.length < TIMES) {
setTimeout(() => {
setVisible(true);
}, 24);
}
if (times.length === TIMES) {
// calculate average, but remove highest and lowest
const sortedTimes = [...times].sort((a, b) => a - b);
sortedTimes.shift();
sortedTimes.shift();
sortedTimes.pop();
sortedTimes.pop();
const avgTime =
sortedTimes.reduce((sum, time) => sum + time, 0) / sortedTimes.length;
console.log(`Average render time: ${avgTime} ms`);
console.log(JSON.stringify(PerfMonitor.getMeasures(), null, 2));
PerfMonitor.clear();
}
}, [visible]);
return (
<View style={styles.container}>
{visible && (
<Profiler
id="v3"
onRender={(_id, _phase, actualDuration) => {
times.push(actualDuration);
setTimeout(() => {
setVisible(false);
}, 24);
}}>
<Benchmark />
</Profiler>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
```
</details>
||Before|After|
|-|-|-|
|Reanimated|549ms|519ms|
|No reanimated|472ms|460ms|1 parent b27c7c4 commit 80006ff
File tree
10 files changed
+247
-228
lines changed- packages/react-native-gesture-handler/src/v3/hooks
- callbacks
- js
- utils
10 files changed
+247
-228
lines changedLines changed: 127 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
Lines changed: 29 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
13 | 17 | | |
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
43 | 35 | | |
44 | | - | |
| 36 | + | |
| 37 | + | |
45 | 38 | | |
46 | 39 | | |
47 | 40 | | |
48 | 41 | | |
49 | 42 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
57 | 52 | | |
58 | 53 | | |
59 | 54 | | |
| |||
Lines changed: 4 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | 36 | | |
38 | | - | |
39 | 37 | | |
40 | 38 | | |
41 | 39 | | |
42 | 40 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | 41 | | |
55 | 42 | | |
56 | 43 | | |
| |||
95 | 82 | | |
96 | 83 | | |
97 | 84 | | |
98 | | - | |
| 85 | + | |
99 | 86 | | |
100 | 87 | | |
101 | 88 | | |
102 | 89 | | |
103 | | - | |
104 | | - | |
105 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
106 | 93 | | |
107 | 94 | | |
108 | 95 | | |
| |||
128 | 115 | | |
129 | 116 | | |
130 | 117 | | |
131 | | - | |
132 | 118 | | |
133 | | - | |
134 | 119 | | |
135 | 120 | | |
136 | 121 | | |
| |||
0 commit comments