Skip to content

Commit

Permalink
feat: Inform JS GC about memory pressure if it is a HybridObject
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 22, 2024
1 parent 47800ec commit 2e503ab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public:
public:
// Get memory pressure
constexpr virtual size_t getMemorySize() noexcept override {
virtual size_t getMemorySize() noexcept override {
return sizeof(${name.HybridTSwift}) + _swiftPart.getMemorySize();
}
Expand Down
8 changes: 7 additions & 1 deletion packages/react-native-nitro-modules/cpp/jsi/JSIConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,13 @@ template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_h
throw jsi::JSError(runtime, "Cannot convert nullptr to HostObject<" + typeName + ">!");
}
#endif
return jsi::Object::createFromHostObject(runtime, arg);
jsi::Object object = jsi::Object::createFromHostObject(runtime, arg);
if constexpr (std::is_base_of_v<HybridObject, TPointee>) {
// HybridObjects expose their memory size (+ heap allocations), so inform JS GC about it!
size_t memorySize = arg->getMemorySize();
object.setExternalMemoryPressure(runtime, memorySize);
}
return object;
}
};

Expand Down

0 comments on commit 2e503ab

Please sign in to comment.