⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 287802 in webkit


Ignore:
Timestamp:
Jan 7, 2022, 6:13:46 PM (5 years ago)
Author:
commit-queue@webkit.org
Message:

Don't dispatch "focusin" / "focusout" events if there are no listeners
https://bugs.webkit.org/show_bug.cgi?id=234928

Patch by Alexey Shvayka <ashvayka@apple.com> on 2022-01-07
Reviewed by Geoff Garen.

This patch avoids creating and dispatching "focusin" / "focusout" events if it's
guaranteed there are no registered listeners for them. These events are rather new
and not widely popular: according to Chrome stats, only 17% page loads use them [1],
which is even less than document.all.

Together with r287787, creation of four FocusEvent instances is now avoided when
focus is changed, advancing attached microbenchmark by 6%.

[1] https://chromestatus.com/metrics/feature/timeline/popularity/433

No new tests, no behavior change.

  • dom/Document.cpp:

(WebCore::Document::setFocusedElement):
(WebCore::Document::addListenerTypeIfNeeded):

  • dom/Document.h:
  • dom/Element.cpp:

(WebCore::Element::dispatchFocusInEventIfNeeded):
(WebCore::Element::dispatchFocusOutEventIfNeeded):
(WebCore::Element::dispatchFocusInEvent): Deleted.
(WebCore::Element::dispatchFocusOutEvent): Deleted.

  • dom/Element.h:
Location:
trunk/Source/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r287787 r287802  
     12022-01-07  Alexey Shvayka  <ashvayka@apple.com>
     2
     3        Don't dispatch "focusin" / "focusout" events if there are no listeners
     4        https://bugs.webkit.org/show_bug.cgi?id=234928
     5
     6        Reviewed by Geoff Garen.
     7
     8        This patch avoids creating and dispatching "focusin" / "focusout" events if it's
     9        guaranteed there are no registered listeners for them. These events are rather new
     10        and not widely popular: according to Chrome stats, only 17% page loads use them [1],
     11        which is even less than `document.all`.
     12
     13        Together with r287787, creation of four FocusEvent instances is now avoided when
     14        focus is changed, advancing attached microbenchmark by 6%.
     15
     16        [1] https://chromestatus.com/metrics/feature/timeline/popularity/433
     17
     18        No new tests, no behavior change.
     19
     20        * dom/Document.cpp:
     21        (WebCore::Document::setFocusedElement):
     22        (WebCore::Document::addListenerTypeIfNeeded):
     23        * dom/Document.h:
     24        * dom/Element.cpp:
     25        (WebCore::Element::dispatchFocusInEventIfNeeded):
     26        (WebCore::Element::dispatchFocusOutEventIfNeeded):
     27        (WebCore::Element::dispatchFocusInEvent): Deleted.
     28        (WebCore::Element::dispatchFocusOutEvent): Deleted.
     29        * dom/Element.h:
     30
    1312022-01-07  Alexey Shvayka  <ashvayka@apple.com>
    232
  • trunk/Source/WebCore/dom/Document.cpp

    r287787 r287802  
    46594659            }
    46604660
    4661             oldFocusedElement->dispatchFocusOutEvent(newFocusedElement.copyRef()); // DOM level 3 bubbling blur event.
     4661            oldFocusedElement->dispatchFocusOutEventIfNeeded(newFocusedElement.copyRef()); // DOM level 3 bubbling blur event.
    46624662
    46634663            if (m_focusedElement) {
     
    47244724        }
    47254725
    4726         m_focusedElement->dispatchFocusInEvent(oldFocusedElement.copyRef()); // DOM level 3 bubbling focus event.
     4726        m_focusedElement->dispatchFocusInEventIfNeeded(oldFocusedElement.copyRef()); // DOM level 3 bubbling focus event.
    47274727
    47284728        if (m_focusedElement != newFocusedElement) {
     
    52865286    else if (eventType == eventNames().resizeEvent)
    52875287        addListenerType(RESIZE_LISTENER);
     5288    else if (eventType == eventNames().focusinEvent)
     5289        addListenerType(FOCUSIN_LISTENER);
     5290    else if (eventType == eventNames().focusoutEvent)
     5291        addListenerType(FOCUSOUT_LISTENER);
    52885292}
    52895293
  • trunk/Source/WebCore/dom/Document.h

    r287707 r287802  
    907907        FORCEDOWN_LISTENER                   = 1 << 15,
    908908        FORCEUP_LISTENER                     = 1 << 16,
    909         RESIZE_LISTENER                      = 1 << 17
     909        RESIZE_LISTENER                      = 1 << 17,
     910        FOCUSIN_LISTENER                     = 1 << 18,
     911        FOCUSOUT_LISTENER                    = 1 << 19,
    910912    };
    911913
  • trunk/Source/WebCore/dom/Element.cpp

    r287787 r287802  
    31833183}
    31843184
    3185 void Element::dispatchFocusInEvent(RefPtr<Element>&& oldFocusedElement)
    3186 {
     3185void Element::dispatchFocusInEventIfNeeded(RefPtr<Element>&& oldFocusedElement)
     3186{
     3187    if (!document().hasListenerType(Document::FOCUSIN_LISTENER))
     3188        return;
    31873189    RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(ScriptDisallowedScope::InMainThread::isScriptAllowed() || !isInWebProcess());
    31883190    dispatchScopedEvent(FocusEvent::create(eventNames().focusinEvent, Event::CanBubble::Yes, Event::IsCancelable::No, document().windowProxy(), 0, WTFMove(oldFocusedElement)));
    31893191}
    31903192
    3191 void Element::dispatchFocusOutEvent(RefPtr<Element>&& newFocusedElement)
    3192 {
     3193void Element::dispatchFocusOutEventIfNeeded(RefPtr<Element>&& newFocusedElement)
     3194{
     3195    if (!document().hasListenerType(Document::FOCUSOUT_LISTENER))
     3196        return;
    31933197    RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(ScriptDisallowedScope::InMainThread::isScriptAllowed() || !isInWebProcess());
    31943198    dispatchScopedEvent(FocusEvent::create(eventNames().focusoutEvent, Event::CanBubble::Yes, Event::IsCancelable::No, document().windowProxy(), 0, WTFMove(newFocusedElement)));
  • trunk/Source/WebCore/dom/Element.h

    r287787 r287802  
    572572
    573573    // FIXME: Consider changing signature to accept Element* because all callers perform copyRef().
    574     void dispatchFocusInEvent(RefPtr<Element>&& oldFocusedElement);
    575     void dispatchFocusOutEvent(RefPtr<Element>&& newFocusedElement);
     574    void dispatchFocusInEventIfNeeded(RefPtr<Element>&& oldFocusedElement);
     575    void dispatchFocusOutEventIfNeeded(RefPtr<Element>&& newFocusedElement);
    576576    virtual void dispatchFocusEvent(RefPtr<Element>&& oldFocusedElement, FocusDirection);
    577577    virtual void dispatchBlurEvent(RefPtr<Element>&& newFocusedElement);
Note: See TracChangeset for help on using the changeset viewer.