Skip to content

Commit cddfe4a

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
use if statement instead of unordered_map for mapping (#51705)
Summary: Pull Request resolved: #51705 changelog: [internal] use if/else instead of unordered_map to map types of nodes/drivers from string to a class. This is faster and improves binary size. Reviewed By: javache Differential Revision: D75676701 fbshipit-source-id: be9b8b646ebd9472382e6f692768b8fe9703d88f
1 parent 6a7cea2 commit cddfe4a

2 files changed

Lines changed: 38 additions & 26 deletions

File tree

packages/react-native/ReactCxxPlatform/react/renderer/animated/drivers/AnimationDriver.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ namespace facebook::react {
2020

2121
std::optional<AnimationDriverType> AnimationDriver::getDriverTypeByName(
2222
const std::string& driverTypeName) {
23-
static std::unordered_map<std::string, AnimationDriverType> typeNames = {
24-
{"frames", AnimationDriverType::Frames},
25-
{"spring", AnimationDriverType::Spring},
26-
{"decay", AnimationDriverType::Decay}};
27-
if (auto iter = typeNames.find(driverTypeName); iter != typeNames.end()) {
28-
return iter->second;
23+
if (driverTypeName == "frames") {
24+
return AnimationDriverType::Frames;
25+
} else if (driverTypeName == "spring") {
26+
return AnimationDriverType::Spring;
27+
} else if (driverTypeName == "decay") {
28+
return AnimationDriverType::Decay;
29+
} else {
30+
return std::nullopt;
2931
}
30-
return std::nullopt;
3132
}
3233

3334
AnimationDriver::AnimationDriver(

packages/react-native/ReactCxxPlatform/react/renderer/animated/nodes/AnimatedNode.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,37 @@ AnimatedNode* AnimatedNode::getChildNode(Tag tag) {
4545

4646
std::optional<AnimatedNodeType> AnimatedNode::getNodeTypeByName(
4747
const std::string& nodeTypeName) {
48-
static std::unordered_map<std::string, AnimatedNodeType> typeNames = {
49-
{"style", AnimatedNodeType::Style},
50-
{"value", AnimatedNodeType::Value},
51-
{"color", AnimatedNodeType::Color},
52-
{"props", AnimatedNodeType::Props},
53-
{"interpolation", AnimatedNodeType::Interpolation},
54-
{"addition", AnimatedNodeType::Addition},
55-
{"subtraction", AnimatedNodeType::Subtraction},
56-
{"division", AnimatedNodeType::Division},
57-
{"multiplication", AnimatedNodeType::Multiplication},
58-
{"modulus", AnimatedNodeType::Modulus},
59-
{"diffclamp", AnimatedNodeType::Diffclamp},
60-
{"transform", AnimatedNodeType::Transform},
61-
{"tracking", AnimatedNodeType::Tracking},
62-
{"round", AnimatedNodeType::Round}};
63-
64-
if (auto iter = typeNames.find(nodeTypeName); iter != typeNames.end()) {
65-
return iter->second;
48+
if (nodeTypeName == "style") {
49+
return AnimatedNodeType::Style;
50+
} else if (nodeTypeName == "value") {
51+
return AnimatedNodeType::Value;
52+
} else if (nodeTypeName == "color") {
53+
return AnimatedNodeType::Color;
54+
} else if (nodeTypeName == "props") {
55+
return AnimatedNodeType::Props;
56+
} else if (nodeTypeName == "interpolation") {
57+
return AnimatedNodeType::Interpolation;
58+
} else if (nodeTypeName == "addition") {
59+
return AnimatedNodeType::Addition;
60+
} else if (nodeTypeName == "subtraction") {
61+
return AnimatedNodeType::Subtraction;
62+
} else if (nodeTypeName == "division") {
63+
return AnimatedNodeType::Division;
64+
} else if (nodeTypeName == "multiplication") {
65+
return AnimatedNodeType::Multiplication;
66+
} else if (nodeTypeName == "modulus") {
67+
return AnimatedNodeType::Modulus;
68+
} else if (nodeTypeName == "diffclamp") {
69+
return AnimatedNodeType::Diffclamp;
70+
} else if (nodeTypeName == "transform") {
71+
return AnimatedNodeType::Transform;
72+
} else if (nodeTypeName == "tracking") {
73+
return AnimatedNodeType::Tracking;
74+
} else if (nodeTypeName == "round") {
75+
return AnimatedNodeType::Round;
76+
} else {
77+
return std::nullopt;
6678
}
67-
return std::nullopt;
6879
}
6980

7081
} // namespace facebook::react

0 commit comments

Comments
 (0)