Commit e7eaa40
[General] Reduce the number of calls to
## Description
During testing, I've noticed that calling `useEvent` from Reanimated is
relatively heavy. Since we're doing it three times, I thought it might
be worth exploring how much impact it has on our hooks. This PR reduces
the number of `useEvent` calls to one per gesture, which reduces the
execution time of `usePan` by ~10%. I'd assume the results for other
gestures will be the same, since they all depend on internal
`useGesture`.
## Test plan
<details>
<summary>Tested on this stress-test</summary>
```jsx
import React, { Profiler, useEffect, useRef } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { GestureDetector, usePan } 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(1).fill(null).map((_, i) => `Item ${i + 1}`);
function Item() {
const translateX = useSharedValue(0);
const style = useAnimatedStyle(() => {
return {
transform: [{ translateX: translateX.value }],
};
});
const pan = usePan({
// disableReanimated: true,
onUpdate: (event) => {
'worklet';
console.log('pan onUpdate', event.handlerData.changeX);
},
onStart: () => {
'worklet';
console.log('pan onStart');
},
onEnd: () => {
'worklet';
console.log('pan onEnd');
},
onBegin: () => {
'worklet';
console.log('pan onBegin');
},
onFinalize: () => {
'worklet';
console.log('pan onFinalize');
},
onTouchesDown: () => {
'worklet';
console.log('pan onTouchesDown');
},
});
return null;
}
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(true);
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: ~280ms, after: ~253ms
---------
Co-authored-by: Michał Bert <[email protected]>useEvent (#3836)1 parent 5fb9999 commit e7eaa40
File tree
11 files changed
+154
-142
lines changed- packages/react-native-gesture-handler/src
- handlers/gestures
- v3/hooks
- callbacks
- js
- reanimated
- web/handlers
11 files changed
+154
-142
lines changedLines changed: 8 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
27 | 32 | | |
28 | 33 | | |
29 | 34 | | |
| |||
33 | 38 | | |
34 | 39 | | |
35 | 40 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
40 | 44 | | |
41 | 45 | | |
42 | 46 | | |
| |||
Lines changed: 5 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | | - | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | | - | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
Lines changed: 3 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | 20 | | |
24 | 21 | | |
25 | 22 | | |
| |||
33 | 30 | | |
34 | 31 | | |
35 | 32 | | |
| 33 | + | |
36 | 34 | | |
37 | 35 | | |
Lines changed: 0 additions & 26 deletions
This file was deleted.
packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedTouchEvent.ts
Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | | - | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| |||
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
42 | 48 | | |
43 | 49 | | |
44 | 50 | | |
| |||
47 | 53 | | |
48 | 54 | | |
49 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
50 | 60 | | |
51 | 61 | | |
52 | 62 | | |
Lines changed: 68 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 | + | |
Lines changed: 21 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
39 | | - | |
40 | | - | |
| 39 | + | |
41 | 40 | | |
42 | 41 | | |
43 | 42 | | |
| |||
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
| 54 | + | |
61 | 55 | | |
62 | 56 | | |
63 | 57 | | |
| |||
109 | 103 | | |
110 | 104 | | |
111 | 105 | | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
116 | 124 | | |
117 | 125 | | |
118 | 126 | | |
| |||
123 | 131 | | |
124 | 132 | | |
125 | 133 | | |
126 | | - | |
127 | | - | |
128 | | - | |
| 134 | + | |
129 | 135 | | |
130 | 136 | | |
131 | 137 | | |
| |||
0 commit comments