Skip to content

[iOS] Avoid unnecessary string allocations#3499

Merged
m-bert merged 3 commits into
mainfrom
@mbert/string-allocation
Apr 30, 2025
Merged

[iOS] Avoid unnecessary string allocations#3499
m-bert merged 3 commits into
mainfrom
@mbert/string-allocation

Conversation

@m-bert
Copy link
Copy Markdown
Collaborator

@m-bert m-bert commented Apr 29, 2025

Description

#3290 introduced recursive algorithm to obtain accessibility label from children. However, unlike in React Native, it allocates String at every recursive call. This may lower the performance as those allocations come at high cost.

Test plan

Accessibility labels are inferred as before.

Comment thread apple/RNGestureHandlerButton.mm
@hirbod
Copy link
Copy Markdown

hirbod commented Apr 30, 2025

And what about the infinite recursion? It’s extremely hard to repro, but it can happen. In our case, it was happening randomly when I touched a RectButton inside our LegendList. Sometimes, just backgrounding and foregrounding the app (and likely re-rendering the RectButton in the list) would trigger it. After about 60 seconds of hanging, memory would spike from 300MB to 7GB and the app would crash. It’s hard to tell when and how this happens, but it feels like some kind of bailout mechanism (max depth check) is missing, no? For sure just a bandaid and not the root issue, at least if would prevent those crashes

Screenshot 2025-04-30 at 12 50 44

@m-bert
Copy link
Copy Markdown
Collaborator Author

m-bert commented Apr 30, 2025

And what about the infinite recursion?

We'd like to take a closer look into this issue. Sure, setting max depth is a solution, but maybe we will be able to figure out how to fix it in a better way. Given that, we will do this in another PR. Also it would be great if you could provide a reproduction (if possible).

If you need these changes asap, you can make a patch which reverts this commit. This is where these changes were introduced, so reverting it should make it work again.

@m-bert m-bert merged commit fee1a09 into main Apr 30, 2025
3 checks passed
@m-bert m-bert deleted the @mbert/string-allocation branch April 30, 2025 13:12
@hirbod
Copy link
Copy Markdown

hirbod commented Apr 30, 2025

I fixed the recursion issue for now by just providing an accessibilityLabel to every RectButton, so I’m good and don't need a patch. But I saw a few people on X saying they ran into the same issue, so it might affect more people.

I'll try my best to come up with a repro but this is a tough one for sure.

@hirbod
Copy link
Copy Markdown

hirbod commented Apr 30, 2025

FYI, the issue is reproducible with Pressable by react-native as well. So this is a core issue!

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