mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Avoid string copy in event dispatching
Summary: Changelog: [internal] To avoid unnecessary string copy in event pipeline, use move semantics. Event pipeline has ownership of event type. Passing it by reference ends up in a copy when `RawEvent` object is constructed. To avoid this, pass string by value through each layer and use move semantics to avoid extra copies. Reviewed By: javache Differential Revision: D34392608 fbshipit-source-id: c11d221be345665e165d9edbc360ba5a057e3890
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8200063ac6
commit
e97e3499c3
@@ -19,7 +19,7 @@ EventEmitterWrapper::initHybrid(jni::alias_ref<jclass>) {
|
||||
}
|
||||
|
||||
void EventEmitterWrapper::invokeEvent(
|
||||
std::string const &eventName,
|
||||
std::string eventName,
|
||||
NativeMap *payload,
|
||||
int category) {
|
||||
// It is marginal, but possible for this to be constructed without a valid
|
||||
@@ -35,7 +35,7 @@ void EventEmitterWrapper::invokeEvent(
|
||||
}
|
||||
|
||||
void EventEmitterWrapper::invokeUniqueEvent(
|
||||
std::string const &eventName,
|
||||
std::string eventName,
|
||||
NativeMap *payload,
|
||||
int customCoalesceKey) {
|
||||
// TODO: customCoalesceKey currently unused
|
||||
|
||||
@@ -25,10 +25,9 @@ class EventEmitterWrapper : public jni::HybridClass<EventEmitterWrapper> {
|
||||
|
||||
SharedEventEmitter eventEmitter;
|
||||
|
||||
void
|
||||
invokeEvent(std::string const &eventName, NativeMap *params, int category);
|
||||
void invokeEvent(std::string eventName, NativeMap *params, int category);
|
||||
void invokeUniqueEvent(
|
||||
std::string const &eventName,
|
||||
std::string eventName,
|
||||
NativeMap *params,
|
||||
int customCoalesceKey);
|
||||
|
||||
|
||||
@@ -86,11 +86,11 @@ void ScrollViewEventEmitter::onMomentumScrollEnd(
|
||||
}
|
||||
|
||||
void ScrollViewEventEmitter::dispatchScrollViewEvent(
|
||||
const std::string &name,
|
||||
std::string name,
|
||||
const ScrollViewMetrics &scrollViewMetrics,
|
||||
EventPriority priority) const {
|
||||
dispatchEvent(
|
||||
name,
|
||||
std::move(name),
|
||||
[scrollViewMetrics](jsi::Runtime &runtime) {
|
||||
return scrollViewMetricsPayload(runtime, scrollViewMetrics);
|
||||
},
|
||||
|
||||
@@ -38,7 +38,7 @@ class ScrollViewEventEmitter : public ViewEventEmitter {
|
||||
|
||||
private:
|
||||
void dispatchScrollViewEvent(
|
||||
const std::string &name,
|
||||
std::string name,
|
||||
const ScrollViewMetrics &scrollViewMetrics,
|
||||
EventPriority priority = EventPriority::AsynchronousBatched) const;
|
||||
};
|
||||
|
||||
@@ -60,12 +60,12 @@ static jsi::Value touchEventPayload(
|
||||
}
|
||||
|
||||
void TouchEventEmitter::dispatchTouchEvent(
|
||||
std::string const &type,
|
||||
std::string type,
|
||||
TouchEvent const &event,
|
||||
EventPriority priority,
|
||||
RawEvent::Category category) const {
|
||||
dispatchEvent(
|
||||
type,
|
||||
std::move(type),
|
||||
[event](jsi::Runtime &runtime) {
|
||||
return touchEventPayload(runtime, event);
|
||||
},
|
||||
|
||||
@@ -31,7 +31,7 @@ class TouchEventEmitter : public EventEmitter {
|
||||
|
||||
private:
|
||||
void dispatchTouchEvent(
|
||||
std::string const &type,
|
||||
std::string type,
|
||||
TouchEvent const &event,
|
||||
EventPriority priority,
|
||||
RawEvent::Category category) const;
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace react {
|
||||
|
||||
#pragma mark - Accessibility
|
||||
|
||||
void ViewEventEmitter::onAccessibilityAction(const std::string &name) const {
|
||||
void ViewEventEmitter::onAccessibilityAction(std::string const &name) const {
|
||||
dispatchEvent("accessibilityAction", [name](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
payload.setProperty(runtime, "actionName", name);
|
||||
|
||||
@@ -28,7 +28,7 @@ class ViewEventEmitter : public TouchEventEmitter {
|
||||
|
||||
#pragma mark - Accessibility
|
||||
|
||||
void onAccessibilityAction(const std::string &name) const;
|
||||
void onAccessibilityAction(std::string const &name) const;
|
||||
void onAccessibilityTap() const;
|
||||
void onAccessibilityMagicTap() const;
|
||||
void onAccessibilityEscape() const;
|
||||
|
||||
@@ -22,9 +22,9 @@ namespace react {
|
||||
* Capitalizes the first letter of the event type and adds "top" prefix if
|
||||
* necessary (e.g. "layout" becames "topLayout").
|
||||
*/
|
||||
static std::string normalizeEventType(const std::string &type) {
|
||||
auto prefixedType = type;
|
||||
if (type.find("top", 0) != 0) {
|
||||
static std::string normalizeEventType(std::string type) {
|
||||
auto prefixedType = std::move(type);
|
||||
if (prefixedType.find("top", 0) != 0) {
|
||||
prefixedType.insert(0, "top");
|
||||
prefixedType[3] = static_cast<char>(toupper(prefixedType[3]));
|
||||
}
|
||||
@@ -50,12 +50,12 @@ EventEmitter::EventEmitter(
|
||||
eventDispatcher_(std::move(eventDispatcher)) {}
|
||||
|
||||
void EventEmitter::dispatchEvent(
|
||||
const std::string &type,
|
||||
std::string type,
|
||||
const folly::dynamic &payload,
|
||||
EventPriority priority,
|
||||
RawEvent::Category category) const {
|
||||
dispatchEvent(
|
||||
type,
|
||||
std::move(type),
|
||||
[payload](jsi::Runtime &runtime) {
|
||||
return valueFromDynamic(runtime, payload);
|
||||
},
|
||||
@@ -64,15 +64,15 @@ void EventEmitter::dispatchEvent(
|
||||
}
|
||||
|
||||
void EventEmitter::dispatchUniqueEvent(
|
||||
const std::string &type,
|
||||
std::string type,
|
||||
const folly::dynamic &payload) const {
|
||||
dispatchUniqueEvent(type, [payload](jsi::Runtime &runtime) {
|
||||
dispatchUniqueEvent(std::move(type), [payload](jsi::Runtime &runtime) {
|
||||
return valueFromDynamic(runtime, payload);
|
||||
});
|
||||
}
|
||||
|
||||
void EventEmitter::dispatchEvent(
|
||||
const std::string &type,
|
||||
std::string type,
|
||||
const ValueFactory &payloadFactory,
|
||||
EventPriority priority,
|
||||
RawEvent::Category category) const {
|
||||
@@ -85,12 +85,15 @@ void EventEmitter::dispatchEvent(
|
||||
|
||||
eventDispatcher->dispatchEvent(
|
||||
RawEvent(
|
||||
normalizeEventType(type), payloadFactory, eventTarget_, category),
|
||||
normalizeEventType(std::move(type)),
|
||||
payloadFactory,
|
||||
eventTarget_,
|
||||
category),
|
||||
priority);
|
||||
}
|
||||
|
||||
void EventEmitter::dispatchUniqueEvent(
|
||||
const std::string &type,
|
||||
std::string type,
|
||||
const ValueFactory &payloadFactory) const {
|
||||
SystraceSection s("EventEmitter::dispatchUniqueEvent");
|
||||
|
||||
@@ -100,7 +103,7 @@ void EventEmitter::dispatchUniqueEvent(
|
||||
}
|
||||
|
||||
eventDispatcher->dispatchUniqueEvent(RawEvent(
|
||||
normalizeEventType(type),
|
||||
normalizeEventType(std::move(type)),
|
||||
payloadFactory,
|
||||
eventTarget_,
|
||||
RawEvent::Category::Continuous));
|
||||
|
||||
@@ -68,24 +68,23 @@ class EventEmitter {
|
||||
* Is used by particular subclasses only.
|
||||
*/
|
||||
void dispatchEvent(
|
||||
const std::string &type,
|
||||
std::string type,
|
||||
const ValueFactory &payloadFactory =
|
||||
EventEmitter::defaultPayloadFactory(),
|
||||
EventPriority priority = EventPriority::AsynchronousBatched,
|
||||
RawEvent::Category category = RawEvent::Category::Unspecified) const;
|
||||
|
||||
void dispatchEvent(
|
||||
const std::string &type,
|
||||
std::string type,
|
||||
const folly::dynamic &payload,
|
||||
EventPriority priority = EventPriority::AsynchronousBatched,
|
||||
RawEvent::Category category = RawEvent::Category::Unspecified) const;
|
||||
|
||||
void dispatchUniqueEvent(
|
||||
const std::string &type,
|
||||
const folly::dynamic &payload) const;
|
||||
void dispatchUniqueEvent(std::string type, const folly::dynamic &payload)
|
||||
const;
|
||||
|
||||
void dispatchUniqueEvent(
|
||||
const std::string &type,
|
||||
std::string type,
|
||||
const ValueFactory &payloadFactory =
|
||||
EventEmitter::defaultPayloadFactory()) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user