Skip to content

Commit caf9cbc

Browse files
hryhoriiK97claude
andauthored
Find a LongLivedObject in its collection without walking it (#59403)
* Find a LongLivedObject in its collection without walking it LongLivedObjectCollection::remove walked the whole set to find the object, so releasing N pending callbacks or promises cost O(N^2). Look it up with a non-owning shared_ptr instead, which hashes and compares by the same pointer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Name the lookup key in LongLivedObjectCollection::remove Address review: make it explicit that the key is a non-owning alias. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b86c82c commit caf9cbc

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ void LongLivedObjectCollection::add(std::shared_ptr<LongLivedObject> so) {
3636

3737
void LongLivedObjectCollection::remove(const LongLivedObject* o) {
3838
std::scoped_lock lock(collectionMutex_);
39-
for (auto p = collection_.begin(); p != collection_.end(); p++) {
40-
if (p->get() == o) {
41-
collection_.erase(p);
42-
break;
43-
}
39+
// Empty owner: lookup-only alias, with no ownership or control block.
40+
const std::shared_ptr<LongLivedObject> lookupKey{
41+
std::shared_ptr<LongLivedObject>{}, const_cast<LongLivedObject*>(o)};
42+
43+
// A shared_ptr hashes and compares by the pointer it stores, so a
44+
// non-owning shared_ptr to `o` finds its entry without walking the set.
45+
auto entry = collection_.find(lookupKey);
46+
if (entry != collection_.end()) {
47+
collection_.erase(entry);
4448
}
4549
}
4650

0 commit comments

Comments
 (0)