Fabric: Changing ComponentName type alias from std::string to char *

Summary:
ComponentName is used by many core component of React Native, such as ComponentDescriptor, ShadowNode, ShadowView and so on. In all those cases this value represents the actual name of the component which came from `concreteComponentName` template parameter of ConcreteShadowNode. In all of those cases, it's raw `char const *` type. So, we don't need to use owning representation of the string (std::string) in all those places.

The only exception from this is a part where we receive the name of the component from JS side. In this case, the source string comes from JS and has to be analyzed as a character sequence to find corresponding ComponentDescriptor.

In my experiments, 20% of the time during diffing is spent on copying (this) `std::string`.

Reviewed By: mdvacca

Differential Revision: D15844407

fbshipit-source-id: a2e71505e22d09107e001bdf661d4a826bcf2dea
This commit is contained in:
Valentin Shergin
2019-06-18 11:31:20 -07:00
committed by Facebook Github Bot
parent 4a5060f4cb
commit 6f97733bb8
9 changed files with 20 additions and 21 deletions
@@ -41,7 +41,7 @@ using ComponentHandle = int64_t;
* String identifier for components used for addressing them from
* JavaScript side.
*/
using ComponentName = std::string;
using ComponentName = char const *;
} // namespace react
} // namespace facebook
@@ -22,7 +22,7 @@ namespace react {
* with many handy features.
*/
template <
const char *concreteComponentName,
ComponentName concreteComponentName,
typename PropsT,
typename EventEmitterT = EventEmitter,
typename StateDataT = StateData>
@@ -16,9 +16,8 @@ TEST(ComponentDescriptorTest, createShadowNode) {
std::make_shared<TestComponentDescriptor>(nullptr);
ASSERT_EQ(descriptor->getComponentHandle(), TestShadowNode::Handle());
ASSERT_STREQ(
descriptor->getComponentName().c_str(), TestShadowNode::Name().c_str());
ASSERT_STREQ(descriptor->getComponentName().c_str(), "Test");
ASSERT_STREQ(descriptor->getComponentName(), TestShadowNode::Name());
ASSERT_STREQ(descriptor->getComponentName(), "Test");
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
SharedProps props = descriptor->cloneProps(nullptr, raw);
@@ -30,9 +29,8 @@ TEST(ComponentDescriptorTest, createShadowNode) {
});
ASSERT_EQ(node->getComponentHandle(), TestShadowNode::Handle());
ASSERT_STREQ(
node->getComponentName().c_str(), TestShadowNode::Name().c_str());
ASSERT_STREQ(node->getComponentName().c_str(), "Test");
ASSERT_STREQ(node->getComponentName(), TestShadowNode::Name());
ASSERT_STREQ(node->getComponentName(), "Test");
ASSERT_EQ(node->getTag(), 9);
ASSERT_EQ(node->getSurfaceId(), 1);
ASSERT_STREQ(node->getProps()->nativeId.c_str(), "abc");
@@ -52,7 +50,7 @@ TEST(ComponentDescriptorTest, cloneShadowNode) {
});
SharedShadowNode cloned = descriptor->cloneShadowNode(*node, {});
ASSERT_STREQ(cloned->getComponentName().c_str(), "Test");
ASSERT_STREQ(cloned->getComponentName(), "Test");
ASSERT_EQ(cloned->getTag(), 9);
ASSERT_EQ(cloned->getSurfaceId(), 1);
ASSERT_STREQ(cloned->getProps()->nativeId.c_str(), "abc");
@@ -42,7 +42,7 @@ TEST(ShadowNodeTest, handleShadowNodeCreation) {
componentDescriptor);
ASSERT_FALSE(node->getSealed());
ASSERT_STREQ(node->getComponentName().c_str(), "Test");
ASSERT_STREQ(node->getComponentName(), "Test");
ASSERT_EQ(node->getTag(), 9);
ASSERT_EQ(node->getSurfaceId(), 1);
ASSERT_EQ(node->getEventEmitter(), nullptr);
@@ -66,7 +66,7 @@ TEST(ShadowNodeTest, handleShadowNodeSimpleCloning) {
componentDescriptor);
auto node2 = std::make_shared<TestShadowNode>(*node, ShadowNodeFragment{});
ASSERT_STREQ(node->getComponentName().c_str(), "Test");
ASSERT_STREQ(node->getComponentName(), "Test");
ASSERT_EQ(node->getTag(), 9);
ASSERT_EQ(node->getSurfaceId(), 1);
ASSERT_EQ(node->getEventEmitter(), nullptr);
@@ -34,7 +34,8 @@ void ComponentDescriptorRegistry::add(
sharedComponentDescriptor;
_registryByName[componentDescriptorProvider.name] = sharedComponentDescriptor;
if (componentDescriptorProvider.name == "UnimplementedNativeView") {
if (strcmp(componentDescriptorProvider.name, "UnimplementedNativeView") ==
0) {
auto *self = const_cast<ComponentDescriptorRegistry *>(this);
self->setFallbackComponentDescriptor(sharedComponentDescriptor);
}
@@ -64,7 +65,7 @@ void ComponentDescriptorRegistry::registerComponentDescriptor(
_registryByName[componentName] = componentDescriptor;
}
static ComponentName componentNameByReactViewName(ComponentName viewName) {
static std::string componentNameByReactViewName(std::string viewName) {
// We need this function only for the transition period;
// eventually, all names will be unified.
@@ -114,7 +115,7 @@ static ComponentName componentNameByReactViewName(ComponentName viewName) {
}
ComponentDescriptor const &ComponentDescriptorRegistry::at(
ComponentName const &componentName) const {
std::string const &componentName) const {
std::shared_lock<better::shared_mutex> lock(mutex_);
auto unifiedComponentName = componentNameByReactViewName(componentName);
@@ -140,7 +141,7 @@ ComponentDescriptor const &ComponentDescriptorRegistry::at(
SharedShadowNode ComponentDescriptorRegistry::createNode(
Tag tag,
ComponentName const &viewName,
std::string const &viewName,
SurfaceId surfaceId,
folly::dynamic const &props,
SharedEventTarget const &eventTarget) const {
@@ -45,12 +45,12 @@ class ComponentDescriptorRegistry {
void registerComponentDescriptor(
SharedComponentDescriptor componentDescriptor) const;
ComponentDescriptor const &at(ComponentName const &componentName) const;
ComponentDescriptor const &at(std::string const &componentName) const;
ComponentDescriptor const &at(ComponentHandle componentHandle) const;
SharedShadowNode createNode(
Tag tag,
ComponentName const &viewName,
std::string const &viewName,
SurfaceId surfaceId,
folly::dynamic const &props,
SharedEventTarget const &eventTarget) const;
@@ -73,7 +73,7 @@ class ComponentDescriptorRegistry {
mutable better::shared_mutex mutex_;
mutable better::map<ComponentHandle, SharedComponentDescriptor>
_registryByHandle;
mutable better::map<ComponentName, SharedComponentDescriptor> _registryByName;
mutable better::map<std::string, SharedComponentDescriptor> _registryByName;
ComponentDescriptor::Shared _fallbackComponentDescriptor;
ComponentDescriptorParameters parameters_{};
};
+1 -1
View File
@@ -10,7 +10,7 @@ namespace react {
SharedShadowNode UIManager::createNode(
Tag tag,
const ComponentName &name,
std::string const &name,
SurfaceId surfaceId,
const RawProps &rawProps,
SharedEventTarget eventTarget) const {
+1 -1
View File
@@ -36,7 +36,7 @@ class UIManager {
SharedShadowNode createNode(
Tag tag,
const std::string &name,
std::string const &componentName,
SurfaceId surfaceId,
const RawProps &props,
SharedEventTarget eventTarget) const;
+1 -1
View File
@@ -83,7 +83,7 @@ inline static SurfaceId surfaceIdFromValue(
return (SurfaceId)value.getNumber();
}
inline static ComponentName componentNameFromValue(
inline static std::string componentNameFromValue(
jsi::Runtime &runtime,
const jsi::Value &value) {
return value.getString(runtime).utf8(runtime);