Skip to content

Commit

Permalink
Avoid allocating empty map for events without payload
Browse files Browse the repository at this point in the history
Summary:
It's cheaper to pass nullptr over JNI, and allocate an empty folly::dynamic::object on the stack.

Changelog: [Internal]

Reviewed By: genkikondo

Differential Revision: D43117216

fbshipit-source-id: e127cce7a1ccf50395073ca9c1c0469d0faa9f82
  • Loading branch information
javache authored and facebook-github-bot committed Feb 9, 2023
1 parent 23607ae commit 7535399
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.NativeMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.fabric.FabricSoLoader;
import com.facebook.react.uimanager.events.EventCategoryDef;

Expand Down Expand Up @@ -56,8 +55,7 @@ public synchronized void invoke(
if (!isValid()) {
return;
}
NativeMap payload = params == null ? new WritableNativeMap() : (NativeMap) params;
invokeEvent(eventName, payload, eventCategory);
invokeEvent(eventName, (NativeMap) params, eventCategory);
}

/**
Expand All @@ -72,8 +70,7 @@ public synchronized void invokeUnique(
if (!isValid()) {
return;
}
NativeMap payload = params == null ? new WritableNativeMap() : (NativeMap) params;
invokeUniqueEvent(eventName, payload, customCoalesceKey);
invokeUniqueEvent(eventName, (NativeMap) params, customCoalesceKey);
}

public synchronized void destroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void EventEmitterWrapper::invokeEvent(
if (eventEmitter != nullptr) {
eventEmitter->dispatchEvent(
eventName,
payload->consume(),
payload ? payload->consume() : folly::dynamic::object(),
EventPriority::AsynchronousBatched,
static_cast<RawEvent::Category>(category));
}
Expand All @@ -43,7 +43,8 @@ void EventEmitterWrapper::invokeUniqueEvent(
// EventEmitter. In those cases, make sure we noop/blackhole events instead of
// crashing.
if (eventEmitter != nullptr) {
eventEmitter->dispatchUniqueEvent(eventName, payload->consume());
eventEmitter->dispatchUniqueEvent(
eventName, payload ? payload->consume() : folly::dynamic::object());
}
}

Expand Down

0 comments on commit 7535399

Please sign in to comment.