-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Optimize useAnimatedReaction
#4466
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tomekzaw
approved these changes
May 17, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
piaskowyk
approved these changes
May 17, 2023
kmagiera
approved these changes
May 18, 2023
kmagiera
reviewed
May 18, 2023
fluiddot
pushed a commit
to wordpress-mobile/react-native-reanimated
that referenced
this pull request
Jun 5, 2023
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. --> ## Summary Values returned by the `prepare` function inside `useAnimatedReaction` are assigned to the `previous` shared value. Even though it is only read from the UI thread, those values are cloned so that they can be read from the JS thread. This process turns out to be a tad problematic when dealing with large objects, as it can take a significant amount of time. This PR sets the `oneWayReadsOnly` flag when creating `previous` to true, disabling the cloning process and reducing the performance impact of the hook. ## Test plan <details> <summary> Here's a simple reproduction for this problem </summary> ```jsx import React from 'react'; import {SafeAreaView} from 'react-native'; import { Gesture, GestureDetector, GestureHandlerRootView, } from 'react-native-gesture-handler'; import Animated, { useAnimatedReaction, useAnimatedStyle, useSharedValue, } from 'react-native-reanimated'; const largeArray = new Array(1000).fill(0).map((_, i) => i); function Bar(props: any) { const backgroundColor = useSharedValue('#ff0000ff'); useAnimatedReaction( () => { return [props.isScrolling.value, largeArray]; }, ([isScrolling]) => { if (isScrolling) { backgroundColor.value = '#00ff00ff'; } else { backgroundColor.value = '#ff0000ff'; } }, ); const style = useAnimatedStyle(() => { return { transform: [ {translateX: props.scrollX.value}, {translateY: props.scrollY.value}, ], backgroundColor: backgroundColor.value, }; }); return ( <Animated.View style={[ {width: 300, height: 50, backgroundColor: 'red', marginTop: 8}, style, ]} /> ); } const items = new Array(50).fill(0).map((_, i) => i); export default function App() { const scrollX = useSharedValue(0); const scrollY = useSharedValue(0); const isScrolling = useSharedValue(false); const pan = Gesture.Pan() .onStart(() => { isScrolling.value = true; }) .onEnd(() => { isScrolling.value = false; }) .onChange(e => { scrollX.value += e.changeX; scrollY.value += e.changeY; }) .runOnJS(true); return ( <SafeAreaView style={{flex: 1}}> <GestureHandlerRootView style={{flex: 1}}> <GestureDetector gesture={pan}> <Animated.View style={{flex: 1}}> {items.map(item => ( <Bar key={item} scrollX={scrollX} scrollY={scrollY} isScrolling={isScrolling} /> ))} </Animated.View> </GestureDetector> </GestureHandlerRootView> </SafeAreaView> ); } ``` </details>
github-merge-queue bot
pushed a commit
that referenced
this pull request
Jun 27, 2023
…eserve value between renders (#4633) <!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. --> ## Summary In #4466 I've replaced `useSharedValue` with `useRef` to avoid cloning saved objects on every update. This worked really well unless, as it turned out, there was a re-render in between the updates, in which case the saved value would reset back to null. I think it's because the ref object that was captured in the closure of the worklet responsible for updating was actually a cloned object instead of the real ref, so each update would be applied to the cloned one. This PR reverts `useRef` change and instead passes `true` for `oneWayReadsOnly`, simply preventing the cloning as it was originally done in #4466. Fixes #4615 ## Test plan Check out the reproduction code from the issue above.
Latropos
pushed a commit
that referenced
this pull request
Jul 3, 2023
…eserve value between renders (#4633) <!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. --> ## Summary In #4466 I've replaced `useSharedValue` with `useRef` to avoid cloning saved objects on every update. This worked really well unless, as it turned out, there was a re-render in between the updates, in which case the saved value would reset back to null. I think it's because the ref object that was captured in the closure of the worklet responsible for updating was actually a cloned object instead of the real ref, so each update would be applied to the cloned one. This PR reverts `useRef` change and instead passes `true` for `oneWayReadsOnly`, simply preventing the cloning as it was originally done in #4466. Fixes #4615 ## Test plan Check out the reproduction code from the issue above.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Values returned by the
prepare
function insideuseAnimatedReaction
are assigned to theprevious
shared value. Even though it is only read from the UI thread, those values are cloned so that they can be read from the JS thread. This process turns out to be a tad problematic when dealing with large objects, as it can take a significant amount of time.This PR sets the
oneWayReadsOnly
flag when creatingprevious
to true, disabling the cloning process and reducing the performance impact of the hook.Test plan
Here's a simple reproduction for this problem