From 3997fc1c66a5fb1357c33fc33cb7f469005f41a4 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Wed, 1 Mar 2023 00:36:07 -0800 Subject: [PATCH] Lazy init for event name mapping registry 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 --- .../PerformanceEntryReporter.cpp | 89 ++++++++++--------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/Libraries/WebPerformance/PerformanceEntryReporter.cpp b/Libraries/WebPerformance/PerformanceEntryReporter.cpp index fcf78c6f5f935e..4864200abbb37b 100644 --- a/Libraries/WebPerformance/PerformanceEntryReporter.cpp +++ b/Libraries/WebPerformance/PerformanceEntryReporter.cpp @@ -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 - 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; + +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; }