Skip to content

Allocate Fabric native props storage on first use - #60542

Open
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/fabric-family-clones
Open

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/fabric-family-clones

Conversation

@ide

@ide ide commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Every ShadowNodeFamily carries a mutex for setNativeProps. Allocate the mutex and its props storage together on first use. Clones check whether the storage exists and acquire the mutex only when it does.

The storage lives until the family is destroyed, and clones use it through a raw pointer, so it is private to ShadowNodeFamily and reached only by ShadowNode and UIManager. C++ code that reads ShadowNodeFamily::nativeProps_DEPRECATED or ShadowNodeFamily::nativePropsMutex no longer compiles. Such code sets native props with UIManager::setNativeProps_DEPRECATED, which commits a clone with the new values, and reads them from the shadow node's props.

Changelog:

[General] [Breaking] - Make ShadowNodeFamily::nativeProps_DEPRECATED and ShadowNodeFamily::nativePropsMutex private; set native props with UIManager::setNativeProps_DEPRECATED and read the applied values from the shadow node's props

Test Plan:

The C++ ShadowNode, ShadowNodeFamily and UIManager suites pass. The Fantom Mounting and ReactNativeElement suites also pass, including their setNativeProps cases. python -m scripts.cxx-api.parser --validate passes with Doxygen 1.16.1.

In a Release build of the C++ tests on Apple M6, sizeof(ShadowNodeFamily) falls from 296 to 232 bytes, and on Android arm64 from 272 to 240 bytes. Across three processes, cloning a View without native props retires a median of 1,766 instructions before the change and 1,668 after it, and creating and destroying a View retires 4,761 to 4,829 before and 4,549 to 4,551 after.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 4, 2026
@ide
ide force-pushed the @ide/fabric-family-clones branch from 6071dca to f3f282b Compare December 4, 2026 11:33
@ide ide changed the title Allocate native props storage only when a Fabric family uses it Allocate Fabric native props storage on first use Dec 4, 2026
@ide
ide force-pushed the @ide/fabric-family-clones branch 2 times, most recently from 928574a to 4bd295c Compare December 4, 2026 14:51
@ide
ide marked this pull request as ready for review December 4, 2026 16:24

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.

@javache

javache commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Is this really costly? we only recently changed this to be unique_ptr and a mutex should be constinit

@ide
ide force-pushed the @ide/fabric-family-clones branch from 4bd295c to eb35272 Compare December 4, 2026 20:17
@ide

ide commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

I measured it in a Release build of the C++ tests on Apple M6, as median retired instructions over three processes:

  • Cloning a View without native props: 1,766 before, 1,668 after. The clone no longer locks and unlocks the mutex.
  • Creating and destroying a View: 4,761 to 4,829 before, 4,549 to 4,551 after.
  • sizeof(ShadowNodeFamily): 296 to 232 bytes on Apple, and 272 to 240 bytes on Android arm64.

Constructing the mutex costs nothing, as you say. The clone path is where most of the difference is. I updated the test plan with these numbers in place of the Debug ones.

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The lazy holder is correct as written. The compare-exchange in getOrCreateNativePropsHolder frees the losing allocation, the acquire load pairs with the acq_rel publish, and a clone that sees no holder behaves the same as a clone that took the old mutex just before setNativeProps. One point needs a decision: nativeProps_DEPRECATED and nativePropsMutex were public members of a public header, and this change makes them private. Any third-party C++ code that reads them stops compiling, so the PR needs a changelog entry, which it does not have yet, and that entry should say the change is breaking for C++ consumers. Two comments in ShadowNode::clone, outside the diff, still name family.nativeProps_DEPRECATED and are now stale.

Every `ShadowNodeFamily` carries a mutex for `setNativeProps`. Allocate the mutex and its props storage together on first use. Clones check whether the storage exists and acquire the mutex only when it does.

The storage lives until the family is destroyed, and clones use it through a raw pointer, so it is private to `ShadowNodeFamily` and reached only by `ShadowNode` and `UIManager`. C++ code that reads `ShadowNodeFamily::nativeProps_DEPRECATED` or `ShadowNodeFamily::nativePropsMutex` no longer compiles. Such code sets native props with `UIManager::setNativeProps_DEPRECATED`, which commits a clone with the new values, and reads them from the shadow node's props.

The C++ `ShadowNode`, `ShadowNodeFamily` and `UIManager` suites pass. The Fantom Mounting and ReactNativeElement suites also pass, including their `setNativeProps` cases. `python -m scripts.cxx-api.parser --validate` passes with Doxygen 1.16.1.

In a Release build of the C++ tests on Apple M6, `sizeof(ShadowNodeFamily)` falls from 296 to 232 bytes, and on Android arm64 from 272 to 240 bytes. Across three processes, cloning a View without native props retires a median of 1,766 instructions before the change and 1,668 after it, and creating and destroying a View retires 4,761 to 4,829 before and 4,549 to 4,551 after.

Signed-off-by: James Ide <ide@expo.dev>
@ide
ide force-pushed the @ide/fabric-family-clones branch from eb35272 to a38c7ff Compare December 5, 2026 08:30
@ide

ide commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I kept the two members private: a public pointer to the lazily created holder would let outside code free or replace it while a clone holds it. The changelog line is now [General] [Breaking]: "Make ShadowNodeFamily::nativeProps_DEPRECATED and ShadowNodeFamily::nativePropsMutex private; set native props with UIManager::setNativeProps_DEPRECATED and read the applied values from the shadow node's props". The summary also says which C++ code stops compiling. The stale comments in ShadowNode::clone now say "the family's native props", and the comments in UIManager::cloneNode and UIManager::setNativeProps_DEPRECATED name nativeProps, the holder's field, in place of the old member.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants