Skip to content

Commit 0e1a94a

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Cache accessibilityElements (facebook#51940)
Summary: Pull Request resolved: facebook#51940 We are seeing some reports of badf00d fads (stalls), meaning we are likely doing too much work here. `accessibilityElements` gets called a lot, and is often cached so lets add that in. Changelog: [Internal] Reviewed By: jorge-cab Differential Revision: D76371136 fbshipit-source-id: f9e3423e8135a47a24291b04150c4dc54afbda82
1 parent 683054c commit 0e1a94a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ @implementation RCTViewComponentView {
5151
UIView *_containerView;
5252
BOOL _useCustomContainerView;
5353
NSMutableSet<NSString *> *_accessibilityOrderNativeIDs;
54+
NSMutableArray<NSObject *> *_accessibilityElements;
5455
RCTViewAccessibilityElement *_axElementDescribingSelf;
5556
}
5657

@@ -403,6 +404,8 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
403404
for (const std::string &childId : newViewProps.accessibilityOrder) {
404405
[_accessibilityOrderNativeIDs addObject:RCTNSStringFromString(childId)];
405406
}
407+
408+
_accessibilityElements = [NSMutableArray new];
406409
}
407410

408411
// `accessibilityTraits`
@@ -614,6 +617,7 @@ - (void)prepareForRecycle
614617
_isJSResponder = NO;
615618
_removeClippedSubviews = NO;
616619
_reactSubviews = [NSMutableArray new];
620+
_accessibilityElements = [NSMutableArray new];
617621
}
618622

619623
- (void)setPropKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN:(NSSet<NSString *> *_Nullable)props
@@ -1151,13 +1155,19 @@ - (NSObject *)accessibilityElement
11511155
return super.accessibilityElements;
11521156
}
11531157

1158+
// TODO: Currently this ignores changes to descendant nativeID's. While that should rarely, if ever happen, it's an
1159+
// edge case we should address. Currently this fixes some app deaths so landing this without addressing that edge case
1160+
// for now.
1161+
if ([_accessibilityElements count] > 0) {
1162+
return _accessibilityElements;
1163+
}
1164+
11541165
NSMutableDictionary<NSString *, UIView *> *nativeIdToView = [NSMutableDictionary new];
11551166

11561167
[RCTViewComponentView collectAccessibilityElements:self
11571168
intoDictionary:nativeIdToView
11581169
nativeIds:_accessibilityOrderNativeIDs];
11591170

1160-
NSMutableArray<NSObject *> *elements = [NSMutableArray new];
11611171
for (auto childId : _props->accessibilityOrder) {
11621172
NSString *nsStringChildId = RCTNSStringFromString(childId);
11631173
// Special case to allow for self-referencing with accessibilityOrder
@@ -1166,16 +1176,16 @@ - (NSObject *)accessibilityElement
11661176
_axElementDescribingSelf = [[RCTViewAccessibilityElement alloc] initWithView:self];
11671177
}
11681178
_axElementDescribingSelf.isAccessibilityElement = [super isAccessibilityElement];
1169-
[elements addObject:_axElementDescribingSelf];
1179+
[_accessibilityElements addObject:_axElementDescribingSelf];
11701180
} else {
11711181
UIView *viewWithMatchingNativeId = [nativeIdToView objectForKey:nsStringChildId];
11721182
if (viewWithMatchingNativeId) {
1173-
[elements addObject:viewWithMatchingNativeId];
1183+
[_accessibilityElements addObject:viewWithMatchingNativeId];
11741184
}
11751185
}
11761186
}
11771187

1178-
return elements;
1188+
return _accessibilityElements;
11791189
}
11801190

11811191
+ (void)collectAccessibilityElements:(UIView *)view

0 commit comments

Comments
 (0)