Skip to content

Commit 46faf17

Browse files
committed
Represent classic Fabric prop names as static identifiers
The classic Fabric props parser registers each name a component reads and keeps a view of its bytes for later lookups. A `const char*` name leaves that lifetime requirement unstated, and every lookup measures a length the compiler already knows. This change adds `RawPropName` and uses it for the single-name `RawProps::at` and for `convertRawProp`. Its `consteval` constructor accepts string literals and static `constexpr` character arrays, so the compiler computes the length and the name has static storage. A name ends at its first NUL, or at the end of an array that has none. Generated code and calls that pass a literal keep their syntax. Names built at run time go through the new `RawProps::atRuntime`. The parser copies such a name when it registers it, and later lookups compare against the argument without copying it. The three-argument prefix and suffix overload of `RawProps::at` uses `atRuntime`. C++ code that passes a `const char*` or `std::string_view` name to `convertRawProp` or `RawProps::at` no longer compiles. A helper that forwards a declared name takes a `RawPropName` instead; code that builds a name at run time calls `atRuntime` and converts the returned `RawValue`. #60541 proposes making the iterator-based setter the default for components that support it. That path hashes each supplied name and calls `setProp` without the registered names, so this change does not affect it. Components that use the classic constructor keep the registered-name path. `react_renderer_core_tests --gtest_filter='RawPropsTest.*:PropsConceptsTest.*:ComponentDescriptorTest.*'` passes in Debug. New tests look up a name without a terminating NUL and check that the parser copies composed names while it prepares. Compile-time checks accept string literals and reject `std::string`, `const char*` and `std::string_view` names. The Debug View and TextInput prop tests pass. The generated Safe Area props compile, and the Android NDK compiles the TextInput and View prop sources. `FANTOM_FORCE_OSS_BUILD=1 yarn fantom --maxWorkers=4` passes, including the View tests with the iterator flag on and off. `python -m scripts.cxx-api.parser --validate` passes with Doxygen 1.16.1. In an optimized build on an Apple M6, cloning `ViewProps` with six supplied props retires a median of 38,462 instructions on `075deb0d98` and 31,095 with this change on the classic path, 19.15% fewer. With the iterator flag on, the same clone retires 6,924 and 6,905.
1 parent 075deb0 commit 46faf17

20 files changed

Lines changed: 295 additions & 67 deletions

‎packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace facebook::react {
1616

1717
static bool
18-
hasValue(const RawProps& rawProps, bool defaultValue, const char* name) {
18+
hasValue(const RawProps& rawProps, bool defaultValue, RawPropName name) {
1919
auto rawValue = rawProps.at(name);
2020

2121
// No change to prop - use default

‎packages/react-native/ReactCommon/react/renderer/components/view/propsConversions.h‎

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -374,37 +374,37 @@ convertRawProp(const PropsParserContext &context, const RawProps &rawProps, cons
374374
}
375375

376376
struct CascadedRectangleCornersNames {
377-
const char *topLeft;
378-
const char *topRight;
379-
const char *bottomLeft;
380-
const char *bottomRight;
381-
const char *topStart;
382-
const char *topEnd;
383-
const char *bottomStart;
384-
const char *bottomEnd;
385-
const char *endEnd;
386-
const char *endStart;
387-
const char *startEnd;
388-
const char *startStart;
389-
const char *all;
377+
RawPropName topLeft;
378+
RawPropName topRight;
379+
RawPropName bottomLeft;
380+
RawPropName bottomRight;
381+
RawPropName topStart;
382+
RawPropName topEnd;
383+
RawPropName bottomStart;
384+
RawPropName bottomEnd;
385+
RawPropName endEnd;
386+
RawPropName endStart;
387+
RawPropName startEnd;
388+
RawPropName startStart;
389+
RawPropName all;
390390
};
391391

392392
struct CascadedRectangleEdgesNames {
393-
const char *left;
394-
const char *right;
395-
const char *top;
396-
const char *bottom;
397-
const char *start;
398-
const char *end;
399-
const char *horizontal;
400-
const char *vertical;
401-
const char *block;
402-
const char *blockEnd;
403-
const char *blockStart;
404-
const char *inline_;
405-
const char *inlineEnd;
406-
const char *inlineStart;
407-
const char *all;
393+
RawPropName left;
394+
RawPropName right;
395+
RawPropName top;
396+
RawPropName bottom;
397+
RawPropName start;
398+
RawPropName end;
399+
RawPropName horizontal;
400+
RawPropName vertical;
401+
RawPropName block;
402+
RawPropName blockEnd;
403+
RawPropName blockStart;
404+
RawPropName inline_;
405+
RawPropName inlineEnd;
406+
RawPropName inlineStart;
407+
RawPropName all;
408408
};
409409

410410
// This can be deleted when non-iterator ViewProp parsing is deleted

‎packages/react-native/ReactCommon/react/renderer/core/RawProps.cpp‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,26 @@ bool RawProps::isEmpty() const noexcept {
109109
* Returns a const unowning pointer to `RawValue` of a prop with a given name.
110110
* Returns `nullptr` if a prop with the given name does not exist.
111111
*/
112-
const RawValue* RawProps::at(const char* name) const noexcept {
112+
const RawValue* RawProps::at(RawPropName name) const noexcept {
113113
react_native_assert(
114114
parser_ &&
115115
"The object is not parsed. `parse` must be called before `at`.");
116-
return parser_->at(*this, name);
116+
return parser_->at(*this, name.view(), /* copyKey */ false);
117+
}
118+
119+
const RawValue* RawProps::atRuntime(std::string_view name) const noexcept {
120+
react_native_assert(
121+
parser_ &&
122+
"The object is not parsed. `parse` must be called before `atRuntime`.");
123+
return parser_->at(*this, name, /* copyKey */ true);
117124
}
118125

119126
const RawValue* RawProps::at(
120127
const char* name,
121128
const char* prefix,
122129
const char* suffix) const noexcept {
123130
if (prefix == nullptr && suffix == nullptr) {
124-
return at(name);
131+
return atRuntime(name);
125132
}
126133

127134
std::string concatenated;
@@ -132,10 +139,7 @@ const RawValue* RawProps::at(
132139
if (suffix != nullptr) {
133140
concatenated += suffix;
134141
}
135-
react_native_assert(
136-
parser_ &&
137-
"The object is not parsed. `parse` must be called before `at`.");
138-
return parser_->at(*this, concatenated);
142+
return atRuntime(concatenated);
139143
}
140144

141145
} // namespace facebook::react

‎packages/react-native/ReactCommon/react/renderer/core/RawProps.h‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
#include <react/cxxstableapi/UmbrellaGuard.h>
1111

12+
#include <cstddef>
1213
#include <limits>
1314
#include <optional>
1415
#include <string>
16+
#include <string_view>
1517

1618
#include <folly/dynamic.h>
1719
#include <jsi/JSIDynamic.h>
@@ -25,6 +27,26 @@ namespace facebook::react {
2527

2628
class RawPropsParser;
2729

30+
// A classic prop name backed by a string literal or static constexpr array.
31+
// Immediate construction rejects arrays whose addresses cannot escape constant
32+
// evaluation and determines the length at the call site.
33+
class RawPropName final {
34+
public:
35+
template <std::size_t Size>
36+
consteval RawPropName(const char (&name)[Size]) : name_(name, Size)
37+
{
38+
name_ = name_.substr(0, name_.find('\0'));
39+
}
40+
41+
constexpr std::string_view view() const noexcept
42+
{
43+
return name_;
44+
}
45+
46+
private:
47+
std::string_view name_;
48+
};
49+
2850
/*
2951
* `RawProps` represents an untyped map of props comes from JavaScript side.
3052
* `RawProps` stores JSI (or `folly::dynamic`) primitives inside and abstract
@@ -99,10 +121,14 @@ class RawProps final {
99121
* Returns a const unowning pointer to `RawValue` of a prop with a given name.
100122
* Returns `nullptr` if a prop with the given name does not exist.
101123
*/
102-
const RawValue *at(const char *name) const noexcept;
124+
const RawValue *at(RawPropName name) const noexcept;
125+
126+
// Looks up a name composed at runtime. The name must also be requested while
127+
// preparing the parser, which copies its bytes so they need only live through
128+
// the call.
129+
const RawValue *atRuntime(std::string_view name) const noexcept;
103130

104-
// Deprecated: Use at(name) instead. This overload exists for backwards
105-
// compatibility with callers that pass prefix/suffix separately.
131+
// Compatibility for callers that still pass prefix and suffix separately.
106132
const RawValue *at(const char *name, const char *prefix, const char *suffix) const noexcept;
107133

108134
/*

‎packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ inline bool keysEqual(std::string_view a, std::string_view b) noexcept {
3030
// which props are accessed during parsing, and in which order.
3131
const RawValue* RawPropsParser::at(
3232
const RawProps& rawProps,
33-
std::string_view key) const noexcept {
33+
std::string_view key,
34+
bool copyKey) const noexcept {
3435
if (!ready_) [[unlikely]] {
3536
// Check against the same key being inserted more than once.
3637
// This happens commonly with nested Props structs, where the higher-level
@@ -50,6 +51,10 @@ const RawValue* RawPropsParser::at(
5051
}
5152
// This is not thread-safe part; this happens only during initialization of
5253
// a `ComponentDescriptor` where it is actually safe.
54+
if (copyKey) {
55+
ownedKeys_.push_back(std::make_shared<const std::string>(key));
56+
key = *ownedKeys_.back();
57+
}
5358
keys_.push_back(key);
5459
react_native_assert(size < std::numeric_limits<RawPropsValueIndex>::max());
5560
nameToIndex_.insert(key, static_cast<RawPropsValueIndex>(size));
@@ -65,6 +70,9 @@ const RawValue* RawPropsParser::at(
6570

6671
if (static_cast<size_t>(rawProps.keyIndexCursor_) >= keys_.size())
6772
[[unlikely]] {
73+
if (keys_.empty()) {
74+
return nullptr;
75+
}
6876
rawProps.keyIndexCursor_ = 0;
6977
}
7078

‎packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <react/renderer/core/RawPropsKeyMap.h>
1616
#include <react/renderer/core/RawPropsPrimitives.h>
1717
#include <react/renderer/core/RawValue.h>
18+
#include <memory>
19+
#include <string>
1820
#include <string_view>
1921

2022
namespace facebook::react {
@@ -72,8 +74,10 @@ class RawPropsParser final {
7274
/*
7375
* To be used by `RawProps` only.
7476
*/
75-
const RawValue *at(const RawProps &rawProps, std::string_view key) const noexcept;
77+
const RawValue *at(const RawProps &rawProps, std::string_view key, bool copyKey) const noexcept;
7678

79+
// Keep the backing storage of runtime keys stable across parser copies.
80+
mutable std::vector<std::shared_ptr<const std::string>> ownedKeys_{};
7781
mutable std::vector<std::string_view> keys_{};
7882
mutable RawPropsKeyMap nameToIndex_{};
7983
mutable bool ready_{false};

‎packages/react-native/ReactCommon/react/renderer/core/propsConversions.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ template <typename T, typename U = T>
172172
T convertRawProp(
173173
const PropsParserContext &context,
174174
const RawProps &rawProps,
175-
const char *name,
175+
RawPropName name,
176176
const T &sourceValue,
177177
const U &defaultValue)
178178
{
@@ -194,7 +194,7 @@ T convertRawProp(
194194
} catch (const std::exception &e) {
195195
// In case of errors, log the error and fall back to the default
196196
// TODO: report this using ErrorUtils so it's more visible to the user
197-
LOG(ERROR) << "Error while converting prop '" << name << "': " << e.what();
197+
LOG(ERROR) << "Error while converting prop '" << name.view() << "': " << e.what();
198198
return defaultValue;
199199
}
200200
}

‎packages/react-native/ReactCommon/react/renderer/core/tests/RawPropsTest.cpp‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,112 @@
1616
#include <react/renderer/core/RawPropsParser.h>
1717
#include <react/renderer/core/ShadowNode.h>
1818
#include <react/renderer/core/propsConversions.h>
19+
#include <string>
20+
#include <string_view>
21+
#include <utility>
1922

2023
using namespace facebook;
2124
using namespace facebook::react;
2225

26+
namespace {
27+
28+
template <typename Name>
29+
concept CanLookUpProp = requires(const RawProps& props, Name&& name) {
30+
props.at(std::forward<Name>(name));
31+
};
32+
33+
template <typename Name>
34+
concept CanConvertProp = requires(
35+
const PropsParserContext& context, const RawProps& props, Name&& name) {
36+
convertRawProp(context, props, std::forward<Name>(name), 0, {});
37+
};
38+
39+
static_assert(CanLookUpProp<decltype("value")>);
40+
static_assert(CanConvertProp<decltype("value")>);
41+
static_assert(!CanLookUpProp<std::string>);
42+
static_assert(!CanConvertProp<std::string>);
43+
static_assert(!CanLookUpProp<const char*>);
44+
static_assert(!CanConvertProp<const char*>);
45+
static_assert(!CanLookUpProp<std::string_view>);
46+
static_assert(!CanConvertProp<std::string_view>);
47+
48+
constexpr char unterminatedName[] = {'v', 'a', 'l', 'u', 'e'};
49+
constexpr char paddedName[12] = "value";
50+
51+
struct PropsWithUnterminatedName : Props {
52+
PropsWithUnterminatedName(
53+
const PropsParserContext& context,
54+
const PropsWithUnterminatedName& source,
55+
const RawProps& rawProps)
56+
: value(convertRawProp(
57+
context, rawProps, unterminatedName, source.value, {})) {}
58+
59+
PropsWithUnterminatedName() = default;
60+
int value{};
61+
};
62+
63+
struct PropsWithComposedNames : Props {
64+
PropsWithComposedNames(
65+
const PropsParserContext&,
66+
const PropsWithComposedNames&,
67+
const RawProps& rawProps) {
68+
std::string name = "test";
69+
name += "PropA";
70+
if (const auto* rawValue = rawProps.atRuntime(name)) {
71+
first = static_cast<int>(*rawValue);
72+
}
73+
name.back() = 'B';
74+
if (const auto* rawValue = rawProps.atRuntime(name)) {
75+
second = static_cast<int>(*rawValue);
76+
}
77+
if (const auto* rawValue = rawProps.at("Prop", "compat", "Value")) {
78+
composed = static_cast<int>(*rawValue);
79+
}
80+
}
81+
82+
PropsWithComposedNames() = default;
83+
int first{};
84+
int second{};
85+
int composed{};
86+
};
87+
88+
} // namespace
89+
90+
TEST(RawPropsTest, looksUpAnUnterminatedName) {
91+
RawPropsParser parser;
92+
parser.prepare<PropsWithUnterminatedName>();
93+
RawProps raw(folly::dynamic::object("value", 42)("valueExtra", 19));
94+
raw.parse(parser);
95+
96+
ContextContainer contextContainer;
97+
PropsParserContext context{-1, contextContainer};
98+
EXPECT_EQ(PropsWithUnterminatedName(context, {}, raw).value, 42);
99+
const auto* value = raw.at(paddedName);
100+
ASSERT_NE(value, nullptr);
101+
EXPECT_EQ(static_cast<int>(*value), 42);
102+
}
103+
104+
TEST(RawPropsTest, copiesComposedNamesWhilePreparing) {
105+
const auto parser = [] {
106+
RawPropsParser original;
107+
original.prepare<PropsWithComposedNames>();
108+
return RawPropsParser(original);
109+
}();
110+
RawProps raw(folly::dynamic::object("testPropA", 42)("testPropB", 19)(
111+
"compatPropValue", 7));
112+
raw.parse(parser);
113+
114+
ContextContainer contextContainer;
115+
PropsParserContext context{-1, contextContainer};
116+
PropsWithComposedNames props(context, {}, raw);
117+
EXPECT_EQ(props.first, 42);
118+
EXPECT_EQ(props.second, 19);
119+
EXPECT_EQ(props.composed, 7);
120+
const auto* value = raw.at("compatPropValue", nullptr, nullptr);
121+
ASSERT_NE(value, nullptr);
122+
EXPECT_EQ(static_cast<int>(*value), 7);
123+
}
124+
23125
TEST(RawPropsTest, dynamicSourceIsBorrowed) {
24126
const auto rawProps = RawProps(folly::dynamic::object("opacity", 0.5));
25127
const auto* dynamic = rawProps.getDynamic();

‎scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ facebook::react::ComponentDescriptorProvider facebook::react::concreteComponentD
579579
template <typename DestinationTimePointT, typename SourceTimePointT, typename DestnationClockT = typename DestinationTimePointT::clock, typename SourceClockT = typename SourceTimePointT::clock>
580580
DestinationTimePointT facebook::react::clockCast(SourceTimePointT timePoint);
581581
template <typename T, typename U = T>
582-
T facebook::react::convertRawProp(const facebook::react::PropsParserContext& context, const facebook::react::RawProps& rawProps, const char* name, const T& sourceValue, const U& defaultValue);
582+
T facebook::react::convertRawProp(const facebook::react::PropsParserContext& context, const facebook::react::RawProps& rawProps, facebook::react::RawPropName name, const T& sourceValue, const U& defaultValue);
583583
template <typename T>
584584
folly::dynamic facebook::react::toDynamic(const std::vector<T>& arrayValue);
585585
template <typename T>
@@ -2130,15 +2130,22 @@ class facebook::react::PullToRefreshViewProps : public facebook::react::HostPlat
21302130
public virtual folly::dynamic getDiffProps(const facebook::react::Props* prevProps) const override;
21312131
}
21322132

2133+
class facebook::react::RawPropName {
2134+
public constexpr std::string_view view() const noexcept;
2135+
template <std::size_t Size>
2136+
public consteval RawPropName(const char(&name)[Size]);
2137+
}
2138+
21332139
class facebook::react::RawProps {
21342140
public RawProps();
21352141
public RawProps(const facebook::react::RawProps& other) noexcept;
21362142
public RawProps(facebook::jsi::Runtime& runtime, const facebook::jsi::Value& value) noexcept;
21372143
public RawProps(facebook::react::RawProps&& other) noexcept = default;
21382144
public RawProps(folly::dynamic dynamic) noexcept;
21392145
public bool isEmpty() const noexcept;
2140-
public const facebook::react::RawValue* at(const char* name) const noexcept;
21412146
public const facebook::react::RawValue* at(const char* name, const char* prefix, const char* suffix) const noexcept;
2147+
public const facebook::react::RawValue* at(facebook::react::RawPropName name) const noexcept;
2148+
public const facebook::react::RawValue* atRuntime(std::string_view name) const noexcept;
21422149
public const folly::dynamic* getDynamic() const noexcept;
21432150
public facebook::react::RawProps& operator=(const facebook::react::RawProps& other) noexcept = delete;
21442151
public facebook::react::RawProps& operator=(facebook::react::RawProps&& other) noexcept = delete;

0 commit comments

Comments
 (0)