diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp index 7c7c8ded57d3..6844d8c0bd4d 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp @@ -140,16 +140,16 @@ std::shared_ptr ShadowNode::clone( const auto& componentDescriptor = family.componentDescriptor_; std::shared_ptr props; - { - std::lock_guard lock(family.nativePropsMutex); - const auto& nativeProps = family.nativeProps_DEPRECATED; + if (auto* holder = family.getNativePropsHolder(); holder != nullptr) { + std::lock_guard lock(holder->mutex); + const auto& nativeProps = holder->nativeProps; if (nativeProps != nullptr) { if (fragment.props == ShadowNodeFragment::propsPlaceholder()) { - // Props that already include `nativeProps_DEPRECATED` don't need - // them applied again. + // Props that already include the native props don't need them + // applied again. if (nativeProps->appliedProps != props_) { - // Clone existing `props_` with `family.nativeProps_DEPRECATED` to - // apply previously set props via `setNativeProps` API. + // Clone existing `props_` with the family's native props to apply + // previously set props via `setNativeProps` API. auto propsParserContext = PropsParserContext{family_->getSurfaceId(), {}}; props = componentDescriptor.cloneProps( @@ -157,8 +157,8 @@ std::shared_ptr ShadowNode::clone( nativeProps->appliedProps = props; } } else { - // TODO: We might need to merge fragment.props with - // `family.nativeProps_DEPRECATED`. + // TODO: We might need to merge fragment.props with the family's + // native props. } } } diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp index 7f491effbd74..90fe44b6ed14 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp @@ -111,6 +111,28 @@ ShadowNodeFamily::~ShadowNodeFamily() { if (onFamilyDestroyedCallback_ != nullptr) { onFamilyDestroyedCallback_(*this); } + delete nativePropsHolder_.load(std::memory_order_relaxed); +} + +ShadowNodeFamily::NativePropsHolder* ShadowNodeFamily::getNativePropsHolder() + const noexcept { + return nativePropsHolder_.load(std::memory_order_acquire); +} + +ShadowNodeFamily::NativePropsHolder& +ShadowNodeFamily::getOrCreateNativePropsHolder() const { + auto* holder = getNativePropsHolder(); + if (holder == nullptr) { + auto created = std::make_unique(); + if (nativePropsHolder_.compare_exchange_strong( + holder, + created.get(), + std::memory_order_acq_rel, + std::memory_order_acquire)) { + holder = created.release(); + } + } + return *holder; } AncestorList ShadowNodeFamily::getAncestors( diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h index eb1626ccae48..d9c12acd06c5 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h @@ -9,6 +9,7 @@ #include +#include #include #include @@ -135,15 +136,6 @@ class ShadowNodeFamily final : public jsi::NativeState { std::shared_ptr appliedProps = nullptr; }; - /* - * Holds currently applied native props. `nullptr` if setNativeProps API is - * not used. It is used to backport setNativeProps API from the old - * architecture and will be removed in the future. Guarded by - * `nativePropsMutex`. - */ - mutable std::unique_ptr nativeProps_DEPRECATED; - mutable std::mutex nativePropsMutex; - /** * @return tag for the ShadowNodeFamily. */ @@ -161,6 +153,24 @@ class ShadowNodeFamily final : public jsi::NativeState { private: friend ShadowNode; friend State; + friend class UIManager; + + /* + * Holds the props set with `setNativeProps`, which is backported from the + * old architecture and will be removed in the future. The first + * `setNativeProps` call on the family creates the holder, and the holder + * lives until the family is destroyed: clones use it through a raw pointer, + * so nothing may free or replace it earlier. `nativeProps` is guarded by + * `mutex`. + */ + struct NativePropsHolder { + std::mutex mutex; + std::unique_ptr nativeProps; + }; + + NativePropsHolder *getNativePropsHolder() const noexcept; + NativePropsHolder &getOrCreateNativePropsHolder() const; + mutable std::atomic nativePropsHolder_{nullptr}; /* * Returns the most recent state if the given `state` is obsolete, diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp index 6a18881fd844..fa33d1b3ad14 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp @@ -129,26 +129,25 @@ std::shared_ptr UIManager::cloneNode( auto props = ShadowNodeFragment::propsPlaceholder(); if (!rawProps.isEmpty()) { std::optional finalProps; - { - std::lock_guard lock(family.nativePropsMutex); - if (family.nativeProps_DEPRECATED != nullptr) { - // 1. update the nativeProps_DEPRECATED props. + if (auto* holder = family.getNativePropsHolder(); holder != nullptr) { + std::lock_guard lock(holder->mutex); + if (holder->nativeProps != nullptr) { + // 1. update the native props. // // In this step, we want the most recent value for the props // managed by setNativeProps. // Values in `rawProps` patch (take precedence over) - // `nativeProps_DEPRECATED`. For example, if both - // `nativeProps_DEPRECATED` and `rawProps` contain key 'A'. - // Value from `rawProps` overrides what was previously in - // `nativeProps_DEPRECATED`. Notice that the `nativeProps_DEPRECATED` - // patch will not get more props from `rawProps`: if the key is not - // present in `nativeProps_DEPRECATED`, it will not be added. + // `nativeProps`. For example, if both `nativeProps` and `rawProps` + // contain key 'A'. Value from `rawProps` overrides what was + // previously in `nativeProps`. Notice that the `nativeProps` patch + // will not get more props from `rawProps`: if the key is not present + // in `nativeProps`, it will not be added. // - // The result of this operation is the new `nativeProps_DEPRECATED`. - family.nativeProps_DEPRECATED = + // The result of this operation is the new `nativeProps`. + holder->nativeProps = std::make_unique( mergeDynamicProps( - family.nativeProps_DEPRECATED->props, // source + holder->nativeProps->props, // source (folly::dynamic)rawProps, // patch NullValueStrategy::Ignore)); @@ -162,7 +161,7 @@ std::shared_ptr UIManager::cloneNode( // We use these finalProps as source of truth for the component. finalProps = mergeDynamicProps( (folly::dynamic)rawProps, // source - family.nativeProps_DEPRECATED->props, // patch + holder->nativeProps->props, // patch NullValueStrategy::Override); } } @@ -465,20 +464,20 @@ void UIManager::setNativeProps_DEPRECATED( RawProps rawProps) const { auto& family = shadowNode->getFamily(); { - std::lock_guard lock(family.nativePropsMutex); - if (family.nativeProps_DEPRECATED) { - // Values in `rawProps` patch (take precedence over) - // `nativeProps_DEPRECATED`. For example, if both - // `nativeProps_DEPRECATED` and `rawProps` contain key 'A'. Value from - // `rawProps` overrides what was previously in `nativeProps_DEPRECATED`. - family.nativeProps_DEPRECATED = + auto& holder = family.getOrCreateNativePropsHolder(); + std::lock_guard lock(holder.mutex); + if (holder.nativeProps) { + // Values in `rawProps` patch (take precedence over) `nativeProps`. For + // example, if both `nativeProps` and `rawProps` contain key 'A'. Value + // from `rawProps` overrides what was previously in `nativeProps`. + holder.nativeProps = std::make_unique( mergeDynamicProps( - family.nativeProps_DEPRECATED->props, + holder.nativeProps->props, (folly::dynamic)rawProps, NullValueStrategy::Override)); } else { - family.nativeProps_DEPRECATED = + holder.nativeProps = std::make_unique( (folly::dynamic)rawProps); } diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index 3ab4e7cd11e1..2c7fdc672a78 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -2285,7 +2285,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index b520c48af496..d9491020ab69 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -3685,7 +3685,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index d48c22c4efe9..33f567311b43 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -2278,7 +2278,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index d48c22c4efe9..33f567311b43 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -2278,7 +2278,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 6189f1f82908..dd8723727546 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -4518,7 +4518,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index 85668b7d3ccc..0881b2e1f601 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -5864,7 +5864,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index df4f68c31155..ebc887646b9f 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -4504,7 +4504,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 64b0e2327e07..a7f1b6d195ff 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -4511,7 +4511,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index 2d39e71b808c..08ad06973650 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -1077,7 +1077,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api index f68615e9118b..7b5f2bd6f93d 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api @@ -2263,7 +2263,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api index 5ff99aabf8c6..2d6731090e53 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api @@ -1070,7 +1070,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index 5ff99aabf8c6..2d6731090e53 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -1070,7 +1070,6 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; - public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr;