Bug 222159 - Avoid tree traversals to look for form and canvas elements
Summary: Avoid tree traversals to look for form and canvas elements
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Ryosuke Niwa
URL:
Keywords: InRadar
Depends on: 222404 222510
Blocks:
  Show dependency treegraph
 
Reported: 2021-02-18 21:58 PST by Ryosuke Niwa
Modified: 2021-02-27 01:32 PST (History)
15 users (show)

See Also:


Attachments
WIP (11.58 KB, patch)
2021-02-18 21:59 PST, Ryosuke Niwa
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
WIP2 - alternative approach (25.52 KB, patch)
2021-02-23 22:32 PST, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Patch (31.24 KB, patch)
2021-02-24 18:11 PST, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Updated for trunk (26.89 KB, patch)
2021-02-25 00:18 PST, Ryosuke Niwa
koivisto: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ryosuke Niwa 2021-02-18 21:58:42 PST
We can avoid traversing DOM tree up to find canvas/form elements.
Comment 1 Ryosuke Niwa 2021-02-18 21:59:13 PST
Created attachment 420921 [details]
WIP
Comment 2 Ryosuke Niwa 2021-02-23 22:32:58 PST
Created attachment 421386 [details]
WIP2 - alternative approach
Comment 3 Radar WebKit Bug Importer 2021-02-23 22:35:45 PST
<rdar://problem/74680265>
Comment 4 Ryosuke Niwa 2021-02-24 18:11:36 PST
Created attachment 421490 [details]
Patch
Comment 5 Simon Fraser (smfr) 2021-02-24 22:09:52 PST
Comment on attachment 421490 [details]
Patch

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

> Source/WebCore/dom/Element.cpp:2404
> +    if (init.delegatesFocus)
> +        setDelegatesFocusToShadowRoot();

Would be nice to split these changes into another patch.

> Source/WebCore/dom/Node.h:927
> +    if (states.contains(AncestorState::Canvas))

I'd prefer a blank line above this.

> Source/WebCore/html/HTMLFormElement.cpp:915
> +    if (UNLIKELY(is<HTMLFormElement>(startElement))) {
> +        auto* parentElement = startElement.parentElement();
> +        if (!parentElement || !parentElement->inclusiveAncestorStates().contains(AncestorState::Form)) {
> +            ASSERT(!ancestorsOfType<HTMLFormElement>(startElement).first());
> +            return nullptr;
> +        }
> +    }

Why don't we add firstAncestorsOfType<> and use it everywhere we do ancestorsOfType<>().first()?
Comment 6 Ryosuke Niwa 2021-02-24 23:35:02 PST
(In reply to Simon Fraser (smfr) from comment #5)
> Comment on attachment 421490 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=421490&action=review
> 
> > Source/WebCore/dom/Element.cpp:2404
> > +    if (init.delegatesFocus)
> > +        setDelegatesFocusToShadowRoot();
> 
> Would be nice to split these changes into another patch.

Done that in the bug 222404.

> > Source/WebCore/dom/Node.h:927
> > +    if (states.contains(AncestorState::Canvas))
> 
> I'd prefer a blank line above this.

Done.

> > Source/WebCore/html/HTMLFormElement.cpp:915
> > +    if (UNLIKELY(is<HTMLFormElement>(startElement))) {
> > +        auto* parentElement = startElement.parentElement();
> > +        if (!parentElement || !parentElement->inclusiveAncestorStates().contains(AncestorState::Form)) {
> > +            ASSERT(!ancestorsOfType<HTMLFormElement>(startElement).first());
> > +            return nullptr;
> > +        }
> > +    }
> 
> Why don't we add firstAncestorsOfType<> and use it everywhere we do
> ancestorsOfType<>().first()?

That sounds like a good idea. Probably in a separate patch though.
Comment 7 Ryosuke Niwa 2021-02-25 00:18:33 PST
Created attachment 421508 [details]
Updated for trunk
Comment 8 Antti Koivisto 2021-02-25 00:49:34 PST
Comment on attachment 421508 [details]
Updated for trunk

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

> Source/WebCore/html/HTMLFormElement.cpp:912
> +        if (!parentElement || !parentElement->inclusiveAncestorStates().contains(AncestorState::Form)) {
> +            ASSERT(!ancestorsOfType<HTMLFormElement>(startElement).first());

Maybe you should make ancestorsOfType and pals to do these checks (based on the type obv) so all future code benefits too.
Comment 9 Ryosuke Niwa 2021-02-25 00:51:18 PST
(In reply to Antti Koivisto from comment #8)
> Comment on attachment 421508 [details]
> Updated for trunk
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=421508&action=review
> 
> > Source/WebCore/html/HTMLFormElement.cpp:912
> > +        if (!parentElement || !parentElement->inclusiveAncestorStates().contains(AncestorState::Form)) {
> > +            ASSERT(!ancestorsOfType<HTMLFormElement>(startElement).first());
> 
> Maybe you should make ancestorsOfType and pals to do these checks (based on
> the type obv) so all future code benefits too.

Oh you mean as a type specialization for HTMLFormElement? That might be a neat idea. If we spot a few more of these, it might be worth doing.
Comment 10 Ryosuke Niwa 2021-02-25 00:51:41 PST
Thanks for the reviews, Antti & Simon!
Comment 11 Antti Koivisto 2021-02-25 00:51:54 PST
> > Why don't we add firstAncestorsOfType<> and use it everywhere we do
> > ancestorsOfType<>().first()?
> 
> That sounds like a good idea. Probably in a separate patch though.

Please don't increase complexity for no gain. That literally just saves one '.' of typing.
Comment 12 Antti Koivisto 2021-02-25 00:58:49 PST
> Oh you mean as a type specialization for HTMLFormElement? That might be a
> neat idea. If we spot a few more of these, it might be worth doing.

Or maybe have a static field that gives the bit to test.
Comment 13 Ryosuke Niwa 2021-02-25 00:59:47 PST
(In reply to Antti Koivisto from comment #11)
> > > Why don't we add firstAncestorsOfType<> and use it everywhere we do
> > > ancestorsOfType<>().first()?
> > 
> > That sounds like a good idea. Probably in a separate patch though.
> 
> Please don't increase complexity for no gain. That literally just saves one
> '.' of typing.

I think it's more to do with the fact "first" comes at the end split with (~). Maybe what we need if something like firstIfExists(ancestorsOfType<>(~))
Comment 14 Ryosuke Niwa 2021-02-25 01:01:41 PST
(In reply to Antti Koivisto from comment #12)
> > Oh you mean as a type specialization for HTMLFormElement? That might be a
> > neat idea. If we spot a few more of these, it might be worth doing.
> 
> Or maybe have a static field that gives the bit to test.

Yeah, but I'm imagining it's a template specialization based on whether that bit is available or not. I agree we don't want to repeat all this code for each element type.
Comment 15 Ryosuke Niwa 2021-02-25 01:02:59 PST
Committed r273479 (234558@main): <https://commits.webkit.org/234558@main>
Comment 16 Antti Koivisto 2021-02-25 01:17:44 PST
> Yeah, but I'm imagining it's a template specialization based on whether that
> bit is available or not. I agree we don't want to repeat all this code for
> each element type.

if constexpr () is probably all that is needed
Comment 17 WebKit Commit Bot 2021-02-27 01:27:29 PST
Re-opened since this is blocked by bug 222510
Comment 18 Ryosuke Niwa 2021-02-27 01:32:37 PST
This was not an overall speed up after all.