Bug 204492

Summary: There should be one MicrotaskQueue per EventLoop
Product: WebKit Reporter: Ryosuke Niwa <rniwa>
Component: DOMAssignee: Ryosuke Niwa <rniwa>
Status: RESOLVED FIXED    
Severity: Normal CC: cdumez, dbates, esprehn+autocc, ews-watchlist, ggaren, kangil.han, keith_miller, koivisto, mifenton, rniwa, sam, webkit-bug-importer, wenson_hsieh, ysuzuki
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on: 204978    
Bug Blocks: 204042    
Attachments:
Description Flags
WIP
none
WIP2
none
Patch
none
updated for trunk
none
updated for trunk koivisto: review+

Description Ryosuke Niwa 2019-11-21 22:49:13 PST
HTML5 spec mandates that we should have one microtask queue per similar origin documents.
This leads to bugs like https://bugs.webkit.org/show_bug.cgi?id=199565.

In addition, MutationObserver's compound microtask queue must be per similar origin windows,
and Custom Element's backup queue must be per micro per similar origin windows.
Comment 1 Radar WebKit Bug Importer 2019-11-21 22:49:35 PST
<rdar://problem/57420645>
Comment 2 Ryosuke Niwa 2019-12-05 21:28:23 PST
Created attachment 384990 [details]
WIP
Comment 3 Ryosuke Niwa 2019-12-05 22:51:21 PST
Created attachment 384991 [details]
WIP2
Comment 4 Ryosuke Niwa 2019-12-06 16:42:59 PST
Created attachment 385059 [details]
Patch
Comment 5 Sam Weinig 2019-12-06 18:33:13 PST
Comment on attachment 385059 [details]
Patch

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

> Source/WebCore/dom/CustomElementReactionQueue.h:48
> +class ElementQueue {

This name seems a little too generic now that it is not a nested class. Can we make more specific?
Comment 6 Ryosuke Niwa 2019-12-06 18:35:07 PST
(In reply to Sam Weinig from comment #5)
> Comment on attachment 385059 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=385059&action=review
> 
> > Source/WebCore/dom/CustomElementReactionQueue.h:48
> > +class ElementQueue {
> 
> This name seems a little too generic now that it is not a nested class. Can
> we make more specific?

That's what HTML5 spec uses:
https://html.spec.whatwg.org/multipage/custom-elements.html#element-queue

But we can rename it to CustomElementQueue if anything. Perhaps adding a spec URL there might help clarify the purpose of this class?
Comment 7 Ryosuke Niwa 2019-12-06 23:35:48 PST
Comment on attachment 385059 [details]
Patch

Ah, we have a bug in the way event loop is selected for unique origins.
Comment 8 Ryosuke Niwa 2019-12-07 16:54:07 PST
(In reply to Ryosuke Niwa from comment #6)
> (In reply to Sam Weinig from comment #5)
> > Comment on attachment 385059 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=385059&action=review
> > 
> > > Source/WebCore/dom/CustomElementReactionQueue.h:48
> > > +class ElementQueue {
> > 
> > This name seems a little too generic now that it is not a nested class. Can
> > we make more specific?
> 
> That's what HTML5 spec uses:
> https://html.spec.whatwg.org/multipage/custom-elements.html#element-queue
> 
> But we can rename it to CustomElementQueue if anything. Perhaps adding a
> spec URL there might help clarify the purpose of this class?

Added URL and rename end it to CustomElementQueue.
Comment 9 Ryosuke Niwa 2019-12-07 18:51:43 PST
Created attachment 385110 [details]
updated for trunk
Comment 10 Ryosuke Niwa 2019-12-07 19:07:19 PST
Created attachment 385112 [details]
updated for trunk
Comment 11 Antti Koivisto 2019-12-08 00:24:53 PST
Comment on attachment 385112 [details]
updated for trunk

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

> LayoutTests/ChangeLog:1
> -2019-12-07  Ryosuke Niwa  <rniwa@webkit.org>
> +2019-12-08  Ryosuke Niwa  <rniwa@webkit.org>

Stray ChangeLog change.
Comment 12 Antti Koivisto 2019-12-08 00:32:43 PST
Comment on attachment 385112 [details]
updated for trunk

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

> Source/WebCore/dom/WindowEventLoop.h:51
> +    Vector<GCReachableRef<HTMLSlotElement>>& signalSlotList() { return m_signalSlotList; }
> +    HashSet<RefPtr<MutationObserver>>& activeMutationObservers() { return m_activeObservers; }
> +    HashSet<RefPtr<MutationObserver>>& suspendedMutationObservers() { return m_suspendedObservers; }

It is bit sad these need to be exposed as public and mutable. I wonder this code could be factored so that more of the mutations would be internal to WindowEventLoop?
Comment 13 Ryosuke Niwa 2019-12-08 00:39:37 PST
(In reply to Antti Koivisto from comment #12)
> Comment on attachment 385112 [details]
> updated for trunk
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=385112&action=review
> 
> > Source/WebCore/dom/WindowEventLoop.h:51
> > +    Vector<GCReachableRef<HTMLSlotElement>>& signalSlotList() { return m_signalSlotList; }
> > +    HashSet<RefPtr<MutationObserver>>& activeMutationObservers() { return m_activeObservers; }
> > +    HashSet<RefPtr<MutationObserver>>& suspendedMutationObservers() { return m_suspendedObservers; }
> 
> It is bit sad these need to be exposed as public and mutable. I wonder this
> code could be factored so that more of the mutations would be internal to
> WindowEventLoop?

You mean the whole mutation observer notification code? If we did that, we’d have to make more of mutation observer code public. We rely on inlining, etc... so that’s not so great. I’ve also considered making WindowEventLoop supplementable or creating a new class like SimilarOrginWindowData or strict specific to mutation observers but they didn’t seem to make the encapsulation any more strict / cleaner :(
Comment 14 Ryosuke Niwa 2019-12-08 00:42:11 PST
I think we should probably go ahead with this simple approach for now, and think about a way to refactor it better once we’re done with the event loop work and Yusuke’s work to introduce JSC side optimization for microtasks is done. There are just too many moving pieces to come up with a clean abstraction at the moment.
Comment 15 Antti Koivisto 2019-12-08 05:50:14 PST
Yeah,it is fine. Was just wondering.
Comment 16 Ryosuke Niwa 2019-12-08 15:26:53 PST
Comment on attachment 385112 [details]
updated for trunk

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

>> LayoutTests/ChangeLog:1
>> +2019-12-08  Ryosuke Niwa  <rniwa@webkit.org>
> 
> Stray ChangeLog change.

Fixed.
Comment 17 Ryosuke Niwa 2019-12-08 15:27:28 PST
Committed r253279: <https://trac.webkit.org/changeset/253279>