Skip to content

Commit

Permalink
Add JSI method for setting external memory size
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#41436

Add a JSI API for associating some native memory with a JS object. This
is intended to provide a mechanism to trigger more frequent garbage
collection when JS retains large external memory allocations, in order
to avoid memory buildup.

This diff just adds the JSI method, without any implementations.

Changelog:
[General][Added] - Added JSI method for reporting native memory to the GC.

Reviewed By: tmikov

Differential Revision: D50524912

fbshipit-source-id: c8df0e18b0415d9523e0a00f6d0ed2faa648ac68
  • Loading branch information
neildhar authored and facebook-github-bot committed Nov 30, 2023
1 parent 7d93640 commit a1c1687
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions API/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ class HermesRuntimeImpl final : public HermesRuntime,
const jsi::Object &) override;
void setNativeState(const jsi::Object &, std::shared_ptr<jsi::NativeState>)
override;
void setExternalMemoryPressure(const jsi::Object &, size_t) override;
jsi::Value getProperty(const jsi::Object &, const jsi::PropNameID &name)
override;
jsi::Value getProperty(const jsi::Object &, const jsi::String &name) override;
Expand Down Expand Up @@ -1835,6 +1836,9 @@ std::shared_ptr<jsi::NativeState> HermesRuntimeImpl::getNativeState(
*reinterpret_cast<std::shared_ptr<jsi::NativeState> *>(ns->context()));
}

void HermesRuntimeImpl::setExternalMemoryPressure(const jsi::Object &, size_t) {
}

jsi::Value HermesRuntimeImpl::getProperty(
const jsi::Object &obj,
const jsi::String &name) {
Expand Down
4 changes: 4 additions & 0 deletions API/jsi/jsi/decorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
plain_.setNativeState(o, state);
}

void setExternalMemoryPressure(const Object& obj, size_t amt) override {
plain_.setExternalMemoryPressure(obj, amt);
}

Value getProperty(const Object& o, const PropNameID& name) override {
return plain_.getProperty(o, name);
};
Expand Down
5 changes: 5 additions & 0 deletions API/jsi/jsi/jsi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ inline void Object::setNativeState(
runtime.setNativeState(*this, state);
}

inline void Object::setExternalMemoryPressure(Runtime& runtime, size_t amt)
const {
runtime.setExternalMemoryPressure(*this, amt);
}

inline Array Object::getPropertyNames(Runtime& runtime) const {
return runtime.getPropertyNames(*this);
}
Expand Down
15 changes: 15 additions & 0 deletions API/jsi/jsi/jsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ class JSI_EXPORT Runtime {

virtual bool instanceOf(const Object& o, const Function& f) = 0;

/// See Object::setExternalMemoryPressure.
virtual void setExternalMemoryPressure(
const jsi::Object& obj,
size_t amount) = 0;

// These exist so derived classes can access the private parts of
// Value, Symbol, String, and Object, which are all friends of Runtime.
template <typename T>
Expand Down Expand Up @@ -834,6 +839,16 @@ class JSI_EXPORT Object : public Pointer {
/// works. I only need it in one place.)
Array getPropertyNames(Runtime& runtime) const;

/// Inform the runtime that there is additional memory associated with a given
/// JavaScript object that is not visible to the GC. This can be used if an
/// object is known to retain some native memory, and may be used to guide
/// decisions about when to run garbage collection.
/// This method may be invoked multiple times on an object, and subsequent
/// calls will overwrite any previously set value. Once the object is garbage
/// collected, the associated external memory will be considered freed and may
/// no longer factor into GC decisions.
void setExternalMemoryPressure(Runtime& runtime, size_t amt) const;

protected:
void setPropertyValue(
Runtime& runtime,
Expand Down

0 comments on commit a1c1687

Please sign in to comment.