Skip to content

Commit

Permalink
Lazy init for event name mapping registry
Browse files Browse the repository at this point in the history
Summary:
## Changelog:

[Internal] -

Noticed this when setting up C++ unit tests for WebPerformance - the change in this diff both makes sure there is no "global constructor" warning generated when `-Wglobal-constructors` is enabled, and also makes this bit potentially a little bit more efficient, as we don't pay for the registry construction if we don't use the WebPerformance API.

Reviewed By: christophpurrer

Differential Revision: D43663521

fbshipit-source-id: 59952f2415f49bb455a3443b3bfd8971108ac72b
  • Loading branch information
rshest authored and facebook-github-bot committed Mar 1, 2023
1 parent a0adf57 commit 3997fc1
Showing 1 changed file with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions Libraries/WebPerformance/PerformanceEntryReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,53 +218,58 @@ struct StrKeyHash {
// https://www.w3.org/TR/event-timing/#sec-events-exposed
// Not all of these are currently supported by RN, but we map them anyway for
// future-proofing.
static const std::unordered_map<StrKey, const char *, StrKeyHash>
SUPPORTED_EVENTS = {
{"topAuxClick", "auxclick"},
{"topClick", "click"},
{"topContextMenu", "contextmenu"},
{"topDblClick", "dblclick"},
{"topMouseDown", "mousedown"},
{"topMouseEnter", "mouseenter"},
{"topMouseLeave", "mouseleave"},
{"topMouseOut", "mouseout"},
{"topMouseOver", "mouseover"},
{"topMouseUp", "mouseup"},
{"topPointerOver", "pointerover"},
{"topPointerEnter", "pointerenter"},
{"topPointerDown", "pointerdown"},
{"topPointerUp", "pointerup"},
{"topPointerCancel", "pointercancel"},
{"topPointerOut", "pointerout"},
{"topPointerLeave", "pointerleave"},
{"topGotPointerCapture", "gotpointercapture"},
{"topLostPointerCapture", "lostpointercapture"},
{"topTouchStart", "touchstart"},
{"topTouchEnd", "touchend"},
{"topTouchCancel", "touchcancel"},
{"topKeyDown", "keydown"},
{"topKeyPress", "keypress"},
{"topKeyUp", "keyup"},
{"topBeforeInput", "beforeinput"},
{"topInput", "input"},
{"topCompositionStart", "compositionstart"},
{"topCompositionUpdate", "compositionupdate"},
{"topCompositionEnd", "compositionend"},
{"topDragStart", "dragstart"},
{"topDragEnd", "dragend"},
{"topDragEnter", "dragenter"},
{"topDragLeave", "dragleave"},
{"topDragOver", "dragover"},
{"topDrop", "drop"},
};
using SupportedEventTypeRegistry =
std::unordered_map<StrKey, const char *, StrKeyHash>;

static const SupportedEventTypeRegistry &getSupportedEvents() {
static SupportedEventTypeRegistry SUPPORTED_EVENTS = {
{"topAuxClick", "auxclick"},
{"topClick", "click"},
{"topContextMenu", "contextmenu"},
{"topDblClick", "dblclick"},
{"topMouseDown", "mousedown"},
{"topMouseEnter", "mouseenter"},
{"topMouseLeave", "mouseleave"},
{"topMouseOut", "mouseout"},
{"topMouseOver", "mouseover"},
{"topMouseUp", "mouseup"},
{"topPointerOver", "pointerover"},
{"topPointerEnter", "pointerenter"},
{"topPointerDown", "pointerdown"},
{"topPointerUp", "pointerup"},
{"topPointerCancel", "pointercancel"},
{"topPointerOut", "pointerout"},
{"topPointerLeave", "pointerleave"},
{"topGotPointerCapture", "gotpointercapture"},
{"topLostPointerCapture", "lostpointercapture"},
{"topTouchStart", "touchstart"},
{"topTouchEnd", "touchend"},
{"topTouchCancel", "touchcancel"},
{"topKeyDown", "keydown"},
{"topKeyPress", "keypress"},
{"topKeyUp", "keyup"},
{"topBeforeInput", "beforeinput"},
{"topInput", "input"},
{"topCompositionStart", "compositionstart"},
{"topCompositionUpdate", "compositionupdate"},
{"topCompositionEnd", "compositionend"},
{"topDragStart", "dragstart"},
{"topDragEnd", "dragend"},
{"topDragEnter", "dragenter"},
{"topDragLeave", "dragleave"},
{"topDragOver", "dragover"},
{"topDrop", "drop"},
};
return SUPPORTED_EVENTS;
}

EventTag PerformanceEntryReporter::onEventStart(const char *name) {
if (!isReportingEvents()) {
return 0;
}

auto it = SUPPORTED_EVENTS.find(name);
if (it == SUPPORTED_EVENTS.end()) {
const auto &supportedEvents = getSupportedEvents();
auto it = supportedEvents.find(name);
if (it == supportedEvents.end()) {
return 0;
}

Expand Down

3 comments on commit 3997fc1

@Pranav-yadav
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rshest why aren't we using enums here; instead?
It'll allow more code reusability and make it easier to keep them up-to-date with W3 specs.

PS: Was just going through the recent commits...
PPS: I would like to help with that refactor if it's okay to do so.

@rshest
Copy link
Contributor Author

@rshest rshest commented on 3997fc1 Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pranav-yadav The purpose of this table is purely to map one string to another, so I'm not sure whether using enums would improve anything in this particular case.

If you mean using internally an enum type instead of a string inside the RawEvent data structure - well, perhaps that could make sense indeed. But note that that would be quite a bit bigger change (and completely orthogonal to this PR) :)

@Pranav-yadav
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the background!
Definitely, that would be completely orthogonal... I forgot to mention the context, (general qn).

I am just trying to understand the code and why it's kept/used that way.
Meanwhile, do let me know if there are some beginner-friendly refactors/changes/feat's that I can help with :)

Please sign in to comment.