Skip to content

Reduce memory allocations when computing accessibilityLabel #44605

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1123,13 +1123,17 @@ - (NSObject *)accessibilityElement

static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
{
NSMutableString *result = [NSMutableString stringWithString:@""];
// Result string is initialized lazily to prevent useless but costly allocations.
NSMutableString *result = nil;
for (UIView *subview in view.subviews) {
NSString *label = subview.accessibilityLabel;
if (!label) {
label = RCTRecursiveAccessibilityLabel(subview);
}
if (label && label.length > 0) {
if (result == nil) {
result = [NSMutableString string];
}
if (result.length > 0) {
[result appendString:@" "];
}
Expand Down
8 changes: 6 additions & 2 deletions packages/react-native/React/Views/RCTView.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,24 @@ - (UIView *)react_findClipView

static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
{
NSMutableString *str = [NSMutableString stringWithString:@""];
// Result string is initialized lazily to prevent useless but costly allocations.
NSMutableString *str = nil;
for (UIView *subview in view.subviews) {
NSString *label = subview.accessibilityLabel;
if (!label) {
label = RCTRecursiveAccessibilityLabel(subview);
}
if (label && label.length > 0) {
if (str == nil) {
str = [NSMutableString string];
}
if (str.length > 0) {
[str appendString:@" "];
}
[str appendString:label];
}
}
return str.length == 0 ? nil : str;
return str;
}

@implementation RCTView {
Expand Down
Loading