Commit 12ffcc4
[General] Schedule flushes instead of flushing immediately (#3830)
## Description
Currently, when creating a gesture or configuring relations,
`flushOperations` is called immediately. When rendering multiple
detectors in the same batch, this adds unnecessary overhead as it can be
done a single time at the end of the batch. This PR changes that.
I also noticed that `createGestureHandler` is scheduled on the UI
anyway, so I figured there may be no point in keeping it synchronous.
## Test plan
<details>
<summary>Used this stress test to measure impact:</summary>
```js
import React, { Profiler, useEffect, useRef } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { GestureDetector, usePan } from 'react-native-gesture-handler';
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 = usePan({
onUpdate: (event) => {
'worklet';
translateX.value += event.handlerData.changeX;
},
});
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 = 30;
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.pop();
const avgTime =
sortedTimes.reduce((sum, time) => sum + time, 0) / sortedTimes.length;
console.log(`Average render time: ${avgTime} ms`);
}
}, [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>
I saw the average time fall from ~610ms to ~595ms.
---------
Co-authored-by: Michał Bert <[email protected]>1 parent a5fc2c6 commit 12ffcc4
File tree
5 files changed
+10
-11
lines changed- packages/react-native-gesture-handler
- android/src/main/java/com/swmansion/gesturehandler/react
- apple
- src
- specs
- v3
- detectors
- hooks
5 files changed
+10
-11
lines changedLines changed: 1 addition & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
73 | | - | |
74 | 72 | | |
75 | 73 | | |
76 | 74 | | |
| |||
Lines changed: 1 addition & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | | - | |
136 | | - | |
137 | 135 | | |
138 | 136 | | |
139 | 137 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
148 | 149 | | |
149 | 150 | | |
150 | 151 | | |
151 | | - | |
| 152 | + | |
152 | 153 | | |
153 | 154 | | |
154 | 155 | | |
| |||
Lines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
| 85 | + | |
| 86 | + | |
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
88 | 90 | | |
89 | 91 | | |
90 | | - | |
| 92 | + | |
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
96 | 98 | | |
97 | | - | |
| 99 | + | |
98 | 100 | | |
99 | 101 | | |
100 | 102 | | |
| |||
0 commit comments