Bug 210492 - Make use of WeakHashSet for MediaStreamTrackPrivate and RealtimeMediaSource observers
Summary: Make use of WeakHashSet for MediaStreamTrackPrivate and RealtimeMediaSource o...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebRTC (show other bugs)
Version: WebKit Local Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: youenn fablet
URL:
Keywords: InRadar
: 213928 (view as bug list)
Depends on:
Blocks:
 
Reported: 2020-04-14 06:53 PDT by youenn fablet
Modified: 2020-07-27 01:45 PDT (History)
15 users (show)

See Also:


Attachments
Patch (14.68 KB, patch)
2020-04-14 06:59 PDT, youenn fablet
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description youenn fablet 2020-04-14 06:53:14 PDT
Make use of WeakHashSet for MediaStreamTrackPrivate and RealtimeMediaSource observers
Comment 1 youenn fablet 2020-04-14 06:59:13 PDT
Created attachment 396410 [details]
Patch
Comment 2 Geoffrey Garen 2020-04-14 16:04:57 PDT
Comment on attachment 396410 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=396410&action=review

> Source/WTF/wtf/WeakHashSet.h:144
> +        for (auto& item : map(m_set, [](auto& item) { return makeWeakPtr(item->template get<T>()); })) {
> +            if (item && m_set.contains(*item.m_impl))

This doesn't look right to me.

The lambda shouldn't need to call makeWeakPtr. That's a needless cost. It can just return the pointer or null, depending on whether the WeakPtrImpl is truthy. 

Also, we shouldn't need to check contains(). We just need to check whether the pointer is non-null / the WeakPtrImpl is truthy.
Comment 3 youenn fablet 2020-04-15 05:23:06 PDT
(In reply to Geoffrey Garen from comment #2)
> Comment on attachment 396410 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=396410&action=review
> 
> > Source/WTF/wtf/WeakHashSet.h:144
> > +        for (auto& item : map(m_set, [](auto& item) { return makeWeakPtr(item->template get<T>()); })) {
> > +            if (item && m_set.contains(*item.m_impl))
> 
> This doesn't look right to me.
> 
> The lambda shouldn't need to call makeWeakPtr. That's a needless cost. It
> can just return the pointer or null, depending on whether the WeakPtrImpl is
> truthy. 

What is the guarantee that the vector pointers remain valid while looping over the vector and calling callback(item)?

WeakHashSet::add/remove could be made knowledgeable about the fact that a forEach call is ongoing to make sure m_set does not get modified during the forEach execution. Then we no longer need a vector at all. We would still probably need to check for any to-be-removed entry.

> Also, we shouldn't need to check contains(). We just need to check whether
> the pointer is non-null / the WeakPtrImpl is truthy.

This keeps parity with the existing code.
m_set can be modified as part of a callback(item) call and may no longer contain the next item in the vector.
We probably do not want an observer to be called after the observer removed itself from the set.
Comment 4 Geoffrey Garen 2020-04-16 10:38:40 PDT
Comment on attachment 396410 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=396410&action=review

r=me

>>> Source/WTF/wtf/WeakHashSet.h:144
>>> +            if (item && m_set.contains(*item.m_impl))
>> 
>> This doesn't look right to me.
>> 
>> The lambda shouldn't need to call makeWeakPtr. That's a needless cost. It can just return the pointer or null, depending on whether the WeakPtrImpl is truthy. 
>> 
>> Also, we shouldn't need to check contains(). We just need to check whether the pointer is non-null / the WeakPtrImpl is truthy.
> 
> What is the guarantee that the vector pointers remain valid while looping over the vector and calling callback(item)?
> 
> WeakHashSet::add/remove could be made knowledgeable about the fact that a forEach call is ongoing to make sure m_set does not get modified during the forEach execution. Then we no longer need a vector at all. We would still probably need to check for any to-be-removed entry.

I see. I guess I was imagining behavior more like other iteration and WTF::forEach, where modifying the set during iteration just isn't supported. But I don't object to supporting it.
Comment 5 EWS 2020-04-17 01:40:57 PDT
Committed r260241: <https://trac.webkit.org/changeset/260241>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 396410 [details].
Comment 6 Radar WebKit Bug Importer 2020-04-17 01:41:15 PDT
<rdar://problem/61925770>
Comment 7 youenn fablet 2020-07-27 01:45:43 PDT
*** Bug 213928 has been marked as a duplicate of this bug. ***