diff --git a/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.cpp b/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.cpp index 23ee075e2267..8baeee377ea8 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.cpp @@ -83,6 +83,30 @@ const RawValue* RawPropsParser::at( rawProps.keyIndexCursor_ = keyIndex; } + return valueAtCursor(rawProps); +} + +const RawValue* RawPropsParser::at(const RawProps& rawProps, const char* key) + const noexcept { + // In-order reads pass the same string literal that was stored for the next + // key when the parser was prepared. Recognize it by its address before + // measuring it: the terminator check confirms the length in O(1). + if (ready_ && !keys_.empty()) [[likely]] { + auto next = static_cast(rawProps.keyIndexCursor_) + 1; + if (next >= keys_.size()) [[unlikely]] { + next = 0; + } + const auto& expected = keys_[next]; + if (expected.data() == key && key[expected.size()] == '\0') { + rawProps.keyIndexCursor_ = static_cast(next); + return valueAtCursor(rawProps); + } + } + return at(rawProps, std::string_view{key}); +} + +const RawValue* RawPropsParser::valueAtCursor( + const RawProps& rawProps) const noexcept { auto valueIndex = rawProps.keyIndexToValueIndex_[rawProps.keyIndexCursor_]; return valueIndex == kRawPropsValueIndexEmpty ? nullptr : &rawProps.values_[valueIndex]; diff --git a/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.h b/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.h index 5613f794ef06..6e9d419c1478 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.h +++ b/packages/react-native/ReactCommon/react/renderer/core/RawPropsParser.h @@ -73,6 +73,9 @@ class RawPropsParser final { * To be used by `RawProps` only. */ const RawValue *at(const RawProps &rawProps, std::string_view key) const noexcept; + const RawValue *at(const RawProps &rawProps, const char *key) const noexcept; + + const RawValue *valueAtCursor(const RawProps &rawProps) const noexcept; mutable std::vector keys_{}; mutable RawPropsKeyMap nameToIndex_{}; diff --git a/packages/react-native/ReactCommon/react/renderer/core/tests/RawPropsTest.cpp b/packages/react-native/ReactCommon/react/renderer/core/tests/RawPropsTest.cpp index 46774b3d1401..14992dfe1655 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/tests/RawPropsTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/tests/RawPropsTest.cpp @@ -331,6 +331,42 @@ TEST(RawPropsTest, handleRawPropsPrimitiveTypesGetOutOfOrder) { EXPECT_EQ((bool)*raw.at("boolValue"), true); } +TEST(RawPropsTest, handleRawPropsNamesFromOtherStorage) { + ContextContainer contextContainer{}; + PropsParserContext parserContext{-1, contextContainer}; + + auto raw = RawProps( + folly::dynamic::object("intValue", (int)42)("doubleValue", (double)17.42)( + "floatValue", (float)66.67)("stringValue", "helloworld")( + "boolValue", true)); + + auto parser = RawPropsParser(); + parser.prepare(); + raw.parse(parser); + + // Same characters at other addresses than the literals the parser was + // prepared with: they must be compared by value, in order and out of order. + const std::string intValue = "intValue"; + const std::string doubleValue = "doubleValue"; + const std::string floatValue = "floatValue"; + const std::string stringValue = "stringValue"; + const std::string boolValue = "boolValue"; + // A longer name that starts with a prepared one must not match it. + const std::string intValueSuffixed = "intValueX"; + + EXPECT_EQ((int)*raw.at(intValue.c_str()), 42); + EXPECT_NEAR((double)*raw.at(doubleValue.c_str()), 17.42, 0.0001); + EXPECT_NEAR((float)*raw.at(floatValue.c_str()), 66.67, 0.00001); + EXPECT_STREQ( + ((std::string)*raw.at(stringValue.c_str())).c_str(), "helloworld"); + EXPECT_EQ((bool)*raw.at(boolValue.c_str()), true); + + EXPECT_EQ((bool)*raw.at(boolValue.c_str()), true); + EXPECT_EQ((int)*raw.at(intValue.c_str()), 42); + EXPECT_EQ(raw.at(intValueSuffixed.c_str()), nullptr); + EXPECT_NEAR((double)*raw.at(doubleValue.c_str()), 17.42, 0.0001); +} + TEST(RawPropsTest, handleRawPropsPrimitiveTypesIncomplete) { ContextContainer contextContainer{}; PropsParserContext parserContext{-1, contextContainer};