Skip to content

Commit

Permalink
Avoid using Vector::unsafeAppendWithoutCapacityCheck() in ThreadSafeW…
Browse files Browse the repository at this point in the history
…eakHashSet::values()

https://bugs.webkit.org/show_bug.cgi?id=264545

Reviewed by Darin Adler.

Use WTF::compactMap() instead.

* Source/WTF/wtf/ThreadSafeWeakHashSet.h:

Canonical link: https://commits.webkit.org/270529@main
  • Loading branch information
cdumez committed Nov 10, 2023
1 parent 8aaa6aa commit f870891
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Source/WTF/wtf/ThreadSafeWeakHashSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <wtf/Algorithms.h>
#include <wtf/HashMap.h>
#include <wtf/ThreadSafeWeakPtr.h>
#include <wtf/Vector.h>

namespace WTF {

Expand Down Expand Up @@ -150,16 +151,15 @@ class ThreadSafeWeakHashSet final {
Vector<Ref<T>> strongReferences;
{
Locker locker { m_lock };
strongReferences.reserveInitialCapacity(m_map.size());
m_map.removeIf([&] (auto& pair) {
auto& controlBlock = pair.value;
auto* objectOfCorrectType = pair.key;
if (auto refPtr = controlBlock->template makeStrongReferenceIfPossible<T>(objectOfCorrectType)) {
strongReferences.unsafeAppendWithoutCapacityCheck(refPtr.releaseNonNull());
return false;
}
return true;
bool hasNullReferences = false;
strongReferences = compactMap(m_map, [&hasNullReferences](auto& pair) -> RefPtr<T> {
if (RefPtr strongReference = pair.value->template makeStrongReferenceIfPossible<T>(pair.key))
return strongReference;
hasNullReferences = true;
return nullptr;
});
if (hasNullReferences)
m_map.removeIf([](auto& pair) { return pair.value->objectHasStartedDeletion(); });
cleanupHappened();
}
return strongReferences;
Expand Down

0 comments on commit f870891

Please sign in to comment.