Closed Bug 1742333 Opened 3 years ago Closed 3 years ago

Clean up `EventListenerManager::AddEventListenerInternal` with `switch`

Categories

(Core :: DOM: Events, enhancement, P3)

enhancement

Tracking

()

RESOLVED FIXED
96 Branch
Tracking Status
firefox96 --- fixed

People

(Reporter: masayuki, Assigned: masayuki)

References

Details

Attachments

(14 files)

(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details

It now has a lot of if - else ifs and compare pointers of nsAtom. However, all of them are mapped to EventMessage, thus, we can make the code simpler.

Luckily, my experimental patches do not cause any performance regression on tryserver:
https://treeherder.mozilla.org/perfherder/compare?originalProject=try&originalRevision=26d3174ce6aebfd881c4bfcafec017cdb98957e8&newProject=try&newRevision=b3689f19030348fc4d60ed3784f2d6a81191a36b&framework=10&page=1&showOnlyImportant=1

In theory, switch statement is faster than if - else ifs especially when
there are a lot of else ifs. Although it may be optimized by the compilers
in these days, but they may have same performance even in the worst case.
So if we can rewrite the big if - else if block in
EventListenerManager::AddEventListenerInternal() with a switch statement,
it may become faster and anyway looks much simpler.

A lot of them compares an nsAtom pointer and nsGkAtoms, and the others
compares EventMessage. Fortunately, it treats only known events which
are registered both in EventMessage and nsGkAtoms. Therefore, we can
use switch statement with EventMessage.

For detecting unexpected regressions, this and the following patches put
NS_ASSERTIONs into the default block. Unfortunately, we cannot output
dynamic values like the value of variables with MOZ_ASSERT, we need to
use NS_ASSERTION here. See bug 779173.

Depends on D131750

Current code firstly check whether the given event message is a pointer event
message or not, but it does not necessary for switch statement. Thus, we
can make it simpler with the switch statement.

Depends on D131756

Oddly, nsContentUtils does not register touch events in desktop environments
which do have a touch screen into the map of EventMessage and nsGkAtoms.
However, AddEventListenerInternal() needs to cache whether there are some
touch event listeners since touch screen may be connected later. Therefore,
this patch makes AddEventListenerInternal() adjusts aEventMessage only
when given event message is eUnidentifiedEvent and given nsAtom is one
of the touch events before the switch statement. I guess that the dynamical
registration is for some handlers of touch events, but from the point of view
of EventListenerManager, there should be another way to check whether touch
events may be fired or not, and the mapping table should be same in any
environments. However, it's out of scope of this cleaning up bug and
fortunately, we can skip the additional check with the eUnidentifiedEvent
check and nsAtom::IsStatic() check.

Depends on D131759

Pushed by masayuki@d-toybox.com: https://hg.mozilla.org/integration/autoland/rev/6e7596255bd0 part 1: Make `EventListenerManager::AddEventListenerInternal()` use `switch` to consider event type r=smaug https://hg.mozilla.org/integration/autoland/rev/82ac9416cd39 part 2: Make `EventListenerManager::AddEventListenerInternal()` handle "pointerenter" and "pointerleave" events with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/4fb45f5cd3c3 part 3: Make `EventListenerManager::AddEventListenerInternal()` handle gamepad events with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/49d2cf7a138b part 4: Make `EventListenerManager::AddEventListenerInternal()` handle device enabled events with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/36c02a5f1577 part 5: Make `EventListenerManager::AddEventListenerInternal()` handle touch events with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/a72f1ea12741 part 6: Make `EventListenerManager::AddEventListenerInternal()` handle "mouseenter" and "mouveleave" events with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/7ea03729fff3 part 7: Make `EventListenerManager::AddEventListenerInternal()` handle keyboard events with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/6819df524734 part 8: Make `EventListenerManager::AddEventListenerInternal()` handle composition events and "input" event with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/4810f4a305cf part 9: Make `EventListenerManager::AddEventListenerInternal()` handle "selectionchange" event with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/9774d93127d5 part 10: Make `EventListenerManager::AddEventListenerInternal()` handle "select" event of form controls with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/b25d0d0cce20 part 11: Make `EventListenerManager::AddEventListenerInternal()` handle events for marquee with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/f3fafdfe78d0 part 12: Make `EventListenerManager::AddEventListenerInternal()` handle "beforeinput" event with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/0b5f194a5e3c part 13: Make `EventListenerManager::AddEventListenerInternal()` handle "overflow" and "underflow" events with the `switch` r=smaug https://hg.mozilla.org/integration/autoland/rev/b2075a9d8174 part 14: Make `EventListenerManager::AddEventListenerInternal()` handle legacy mouse events with the `switch` r=smaug
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: