Skip to content
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

Add useHandleRef as a lighter alternative for useAnimatedRef #6500

Merged
merged 2 commits into from
Nov 18, 2024

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Nov 18, 2024

We use useAnimatedRef in a bunch of places just to get the measurements with measure. This is unfortunate because it contains two shared values, one of which stores an internal React node. I'm not sure whether that node goes through the serialization mechanism but the profiler was showing some overhead associated with each useAnimatedRef component.

In this PR, I'm taking just the part that we actually need — reading the component's internal handle. I'm doing the same thing findNodeHandle does except for the slow codepath we don't need (which only triggers for class components). I put that code inline so it's clearer how it works.

On the measure side, I realized that all Reanimated's measure() really wants is a function that returns the handle. (The useAnimatedRef API plays a trick here where it doubles as both a React callback ref which receives the component and as a function that measure can call to get the value.) So I'm just passing () => handle for that case.

I've also replaced <Animated.View> with plain <View> to reduce overhead since there's no need for Reanimated there at all.

Test Plan

Verify you're still able to expand/collapse images:

  • Single image (AutoSizedImage)
  • Gallery
  • User avatar
  • Feed avatar
handle.mov
andr.mov

Before

ImageLayoutGridInner and AutoSizedImage were sticking out as having a bunch of self-time in traces.

Screenshot 2024-11-18 at 16 05 22 Screenshot 2024-11-18 at 16 09 41 Screenshot 2024-11-18 at 16 12 20

After

ImageLayoutGridInner and AutoSizedImage no longer take disproportionate time in traces.

Screenshot 2024-11-18 at 16 17 52 Screenshot 2024-11-18 at 16 20 31 Screenshot 2024-11-18 at 16 24 45

Copy link

github-actions bot commented Nov 18, 2024

Old size New size Diff
8.05 MB 8.05 MB 336 B (0.00%)

@arcalinea arcalinea temporarily deployed to use-handle-ref - social-app PR #6500 November 18, 2024 21:55 — with Render Destroyed
Copy link
Member

@mozzius mozzius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on iOS+Android, lgtm

@gaearon gaearon merged commit 7b6c182 into main Nov 18, 2024
6 checks passed
@gaearon gaearon deleted the use-handle-ref branch November 18, 2024 22:21
khuddite pushed a commit to khuddite/social-app that referenced this pull request Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants