Skip to content

Commit

Permalink
feat: Isolate::memory_pressure_notification() (denoland#1139)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartek Iwańczuk <[email protected]>
  • Loading branch information
ry and bartlomieju committed Nov 30, 2022
1 parent e57c3ec commit 3783a5c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ void v8__Isolate__Enter(v8::Isolate* isolate) { isolate->Enter(); }

void v8__Isolate__Exit(v8::Isolate* isolate) { isolate->Exit(); }

void v8__Isolate__MemoryPressureNotification(v8::Isolate* isolate,
v8::MemoryPressureLevel level) {
isolate->MemoryPressureNotification(level);
}

void v8__Isolate__ClearKeptObjects(v8::Isolate* isolate) {
isolate->ClearKeptObjects();
}
Expand Down
22 changes: 22 additions & 0 deletions src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ pub enum MicrotasksPolicy {
Auto = 2,
}

/// Memory pressure level for the MemoryPressureNotification.
/// None hints V8 that there is no memory pressure.
/// Moderate hints V8 to speed up incremental garbage collection at the cost
/// of higher latency due to garbage collection pauses.
/// Critical hints V8 to free memory as soon as possible. Garbage collection
/// pauses at this level will be large.
pub enum MemoryPressureLevel {
None = 0,
Moderate = 1,
Critical = 2,
}

/// PromiseHook with type Init is called when a new promise is
/// created. When a new promise is created as part of the chain in the
/// case of Promise.then or in the intermediate promises created by
Expand Down Expand Up @@ -348,6 +360,7 @@ extern "C" {
fn v8__Isolate__GetNumberOfDataSlots(this: *const Isolate) -> u32;
fn v8__Isolate__Enter(this: *mut Isolate);
fn v8__Isolate__Exit(this: *mut Isolate);
fn v8__Isolate__MemoryPressureNotification(this: *mut Isolate, level: u8);
fn v8__Isolate__ClearKeptObjects(isolate: *mut Isolate);
fn v8__Isolate__LowMemoryNotification(isolate: *mut Isolate);
fn v8__Isolate__GetHeapStatistics(this: *mut Isolate, s: *mut HeapStatistics);
Expand Down Expand Up @@ -772,6 +785,15 @@ impl Isolate {
v8__Isolate__Exit(self)
}

/// Optional notification that the system is running low on memory.
/// V8 uses these notifications to guide heuristics.
/// It is allowed to call this function from another thread while
/// the isolate is executing long running JavaScript code.
#[inline(always)]
pub fn memory_pressure_notification(&mut self, level: MemoryPressureLevel) {
unsafe { v8__Isolate__MemoryPressureNotification(self, level as u8) }
}

/// Clears the set of objects held strongly by the heap. This set of
/// objects are originally built when a WeakRef is created or
/// successfully dereferenced.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub use isolate::HostImportModuleDynamicallyCallback;
pub use isolate::HostInitializeImportMetaObjectCallback;
pub use isolate::Isolate;
pub use isolate::IsolateHandle;
pub use isolate::MemoryPressureLevel;
pub use isolate::MessageCallback;
pub use isolate::MicrotasksPolicy;
pub use isolate::NearHeapLimitCallback;
Expand Down
9 changes: 9 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6355,6 +6355,15 @@ fn value_serializer_not_implemented() {
);
}

#[test]
fn memory_pressure_notification() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
isolate.memory_pressure_notification(v8::MemoryPressureLevel::Moderate);
isolate.memory_pressure_notification(v8::MemoryPressureLevel::Critical);
isolate.memory_pressure_notification(v8::MemoryPressureLevel::None);
}

// Flaky on aarch64-qemu (Stack corruption).
#[cfg(not(target_os = "android"))]
#[test]
Expand Down

0 comments on commit 3783a5c

Please sign in to comment.