Skip to content

Commit cfe129c

Browse files
neildharfacebook-github-bot
authored andcommitted
Add JSI method for setting external memory size (#41436)
Summary: Pull Request resolved: #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
1 parent b41937e commit cfe129c

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎packages/react-native/ReactCommon/jsc/JSCRuntime.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class JSCRuntime : public jsi::Runtime {
233233
bool strictEquals(const jsi::String& a, const jsi::String& b) const override;
234234
bool strictEquals(const jsi::Object& a, const jsi::Object& b) const override;
235235
bool instanceOf(const jsi::Object& o, const jsi::Function& f) override;
236+
void setExternalMemoryPressure(const jsi::Object&, size_t) override;
236237

237238
private:
238239
// Basically convenience casts
@@ -1392,6 +1393,8 @@ bool JSCRuntime::instanceOf(const jsi::Object& o, const jsi::Function& f) {
13921393
return res;
13931394
}
13941395

1396+
void JSCRuntime::setExternalMemoryPressure(const jsi::Object&, size_t) {}
1397+
13951398
jsi::Runtime::PointerValue* JSCRuntime::makeSymbolValue(
13961399
JSValueRef symbolRef) const {
13971400
#ifndef NDEBUG

‎packages/react-native/ReactCommon/jsi/jsi/decorator.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
252252
plain_.setNativeState(o, state);
253253
}
254254

255+
void setExternalMemoryPressure(const Object& obj, size_t amt) override {
256+
plain_.setExternalMemoryPressure(obj, amt);
257+
}
258+
255259
Value getProperty(const Object& o, const PropNameID& name) override {
256260
return plain_.getProperty(o, name);
257261
};

‎packages/react-native/ReactCommon/jsi/jsi/jsi-inl.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ inline void Object::setNativeState(
227227
runtime.setNativeState(*this, state);
228228
}
229229

230+
inline void Object::setExternalMemoryPressure(Runtime& runtime, size_t amt)
231+
const {
232+
runtime.setExternalMemoryPressure(*this, amt);
233+
}
234+
230235
inline Array Object::getPropertyNames(Runtime& runtime) const {
231236
return runtime.getPropertyNames(*this);
232237
}

‎packages/react-native/ReactCommon/jsi/jsi/jsi.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ class JSI_EXPORT Runtime {
387387

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

390+
/// See Object::setExternalMemoryPressure.
391+
virtual void setExternalMemoryPressure(
392+
const jsi::Object& obj,
393+
size_t amount) = 0;
394+
390395
// These exist so derived classes can access the private parts of
391396
// Value, Symbol, String, and Object, which are all friends of Runtime.
392397
template <typename T>
@@ -834,6 +839,16 @@ class JSI_EXPORT Object : public Pointer {
834839
/// works. I only need it in one place.)
835840
Array getPropertyNames(Runtime& runtime) const;
836841

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

0 commit comments

Comments
 (0)