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>
This commit is contained in:
Gregory MoskaliukandClaude Opus 5.5 authored and GitHub committed 2026-10-28 09:42:57 +00:00
1 parent b86c82c844
commit caf9cbcd63
1 file changed
+9 -5
@@ -36,11 +36,15 @@ void LongLivedObjectCollection::add(std::shared_ptr<LongLivedObject> so) {
void LongLivedObjectCollection::remove(const LongLivedObject* o) {
std::scoped_lock lock(collectionMutex_);
for (auto p = collection_.begin(); p != collection_.end(); p++) {
if (p->get() == o) {
collection_.erase(p);
break;
}
// Empty owner: lookup-only alias, with no ownership or control block.
const std::shared_ptr<LongLivedObject> lookupKey{
std::shared_ptr<LongLivedObject>{}, const_cast<LongLivedObject*>(o)};
// A shared_ptr hashes and compares by the pointer it stores, so a
// non-owning shared_ptr to `o` finds its entry without walking the set.
auto entry = collection_.find(lookupKey);
if (entry != collection_.end()) {
collection_.erase(entry);
}
}