Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,25 @@ std::shared_ptr<ShadowNode> ShadowNode::clone(
const auto& componentDescriptor = family.componentDescriptor_;

std::shared_ptr<const Props> props;
{
std::lock_guard<std::mutex> lock(family.nativePropsMutex);
const auto& nativeProps = family.nativeProps_DEPRECATED;
if (auto* holder = family.getNativePropsHolder(); holder != nullptr) {
std::lock_guard<std::mutex> 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(
propsParserContext, props_, RawProps(nativeProps->props));
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.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NativePropsHolder>();
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <react/cxxstableapi/UmbrellaGuard.h>

#include <atomic>
#include <memory>
#include <mutex>

Expand Down Expand Up @@ -135,15 +136,6 @@ class ShadowNodeFamily final : public jsi::NativeState {
std::shared_ptr<const Props> 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> nativeProps_DEPRECATED;
mutable std::mutex nativePropsMutex;

/**
* @return tag for the ShadowNodeFamily.
*/
Expand All @@ -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 {
Comment thread
chrfalch marked this conversation as resolved.
Resolved
std::mutex mutex;
std::unique_ptr<NativeProps_DEPRECATED> nativeProps;
};

NativePropsHolder *getNativePropsHolder() const noexcept;
NativePropsHolder &getOrCreateNativePropsHolder() const;
mutable std::atomic<NativePropsHolder *> nativePropsHolder_{nullptr};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not store this as unique_ptr? so we can avoid the manual delete

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pointer is atomic so that a clone can check it without the lock while setNativeProps creates the holder on another thread. A std::unique_ptr cannot be loaded atomically. If you prefer no delete, I can keep ownership in a unique_ptr next to the atomic pointer, which adds 8 bytes per family.


/*
* Returns the most recent state if the given `state` is obsolete,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,25 @@ std::shared_ptr<ShadowNode> UIManager::cloneNode(
auto props = ShadowNodeFragment::propsPlaceholder();
if (!rawProps.isEmpty()) {
std::optional<folly::dynamic> finalProps;
{
std::lock_guard<std::mutex> lock(family.nativePropsMutex);
if (family.nativeProps_DEPRECATED != nullptr) {
// 1. update the nativeProps_DEPRECATED props.
if (auto* holder = family.getNativePropsHolder(); holder != nullptr) {
std::lock_guard<std::mutex> 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<ShadowNodeFamily::NativeProps_DEPRECATED>(
mergeDynamicProps(
family.nativeProps_DEPRECATED->props, // source
holder->nativeProps->props, // source
(folly::dynamic)rawProps, // patch
NullValueStrategy::Ignore));

Expand All @@ -162,7 +161,7 @@ std::shared_ptr<ShadowNode> 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);
}
}
Expand Down Expand Up @@ -465,20 +464,20 @@ void UIManager::setNativeProps_DEPRECATED(
RawProps rawProps) const {
auto& family = shadowNode->getFamily();
{
std::lock_guard<std::mutex> 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<std::mutex> 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<ShadowNodeFamily::NativeProps_DEPRECATED>(
mergeDynamicProps(
family.nativeProps_DEPRECATED->props,
holder.nativeProps->props,
(folly::dynamic)rawProps,
NullValueStrategy::Override));
} else {
family.nativeProps_DEPRECATED =
holder.nativeProps =
std::make_unique<ShadowNodeFamily::NativeProps_DEPRECATED>(
(folly::dynamic)rawProps);
}
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
1 change: 0 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -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<const facebook::react::State> getMostRecentState() const;
public using AncestorList = std::vector<std::pair<std::reference_wrapper<const facebook::react::ShadowNode>, int>>;
public using Shared = std::shared_ptr<facebook::react::ShadowNodeFamily>;
Expand Down
Loading