From 1db4874bb33c86ef7cb838126a3cf25d54959149 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Sun, 25 May 2025 04:20:59 -0700 Subject: [PATCH] Use unique_ptr instead of shared_ptr in activeAnimations_ (#51587) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51587 changelog: [internal] use unique_ptr instead of shared_ptr to manage memory of activeAnimations_. Active animations are only owned by NativeAnimatedNodesManager and their ownership isn't shared with any other class. This saves a little bit of C++ binary size. Reviewed By: rshest Differential Revision: D75142643 fbshipit-source-id: c09753b68e70e95fedcb7b2b8fb19a0fd7010059 --- .../renderer/animated/NativeAnimatedNodesManager.cpp | 12 ++++++------ .../renderer/animated/NativeAnimatedNodesManager.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp index 8706e6b1364..9eebc9f62e8 100644 --- a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp @@ -299,30 +299,30 @@ void NativeAnimatedNodesManager::startAnimatingNode( if (auto iter = activeAnimations_.find(animationId); iter != activeAnimations_.end()) { // reset animation config - auto animation = iter->second; + auto& animation = iter->second; animation->updateConfig(config); } else if (animatedNodes_.contains(animatedNodeTag)) { auto type = config["type"].asString(); auto typeEnum = AnimationDriver::getDriverTypeByName(type); - std::shared_ptr animation = nullptr; + std::unique_ptr animation = nullptr; if (typeEnum) { switch (typeEnum.value()) { case AnimationDriverType::Frames: { - animation = std::make_shared( + animation = std::make_unique( animationId, animatedNodeTag, endCallback, config, this); } break; case AnimationDriverType::Spring: { - animation = std::make_shared( + animation = std::make_unique( animationId, animatedNodeTag, endCallback, config, this); } break; case AnimationDriverType::Decay: { - animation = std::make_shared( + animation = std::make_unique( animationId, animatedNodeTag, endCallback, config, this); } break; } if (animation) { - activeAnimations_.insert({animationId, animation}); animation->startAnimation(); + activeAnimations_.insert({animationId, std::move(animation)}); } } else { LOG(ERROR) << "Unknown AnimationDriver type " << type; diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h index f2a34162646..7365990b738 100644 --- a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h @@ -197,7 +197,7 @@ class NativeAnimatedNodesManager std::unordered_map> animatedNodes_; std::unordered_map connectedAnimatedNodes_; - std::unordered_map> activeAnimations_; + std::unordered_map> activeAnimations_; std::unordered_map< EventAnimationDriverKey, std::vector>,