From 2509b0ba62d42b68d99a08f1de8ef01de0356ca4 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:56:35 -0700 Subject: [PATCH] feat: add binding for `ResourceConstraints::ConfigureDefaults` (#1651) * add binding for `ResourceConstraints::ConfigureDefaults` * fmt * fix types --- src/binding.cc | 6 +++++ src/isolate_create_params.rs | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 8eac7309..0f8c791f 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -385,6 +385,12 @@ void v8__ResourceConstraints__ConfigureDefaultsFromHeapSize( maximum_heap_size_in_bytes); } +void v8__ResourceConstraints__ConfigureDefaults( + v8::ResourceConstraints* constraints, uint64_t physical_memory, + uint64_t virtual_memory_limit) { + constraints->ConfigureDefaults(physical_memory, virtual_memory_limit); +} + void v8__HandleScope__CONSTRUCT(uninit_t* buf, v8::Isolate* isolate) { construct_in_place(buf, isolate); diff --git a/src/isolate_create_params.rs b/src/isolate_create_params.rs index d0fad119..3165218c 100644 --- a/src/isolate_create_params.rs +++ b/src/isolate_create_params.rs @@ -160,6 +160,37 @@ impl CreateParams { self } + /// Configures the constraints with reasonable default values based on the capabilities + /// of the current device the VM is running on. + /// + /// By default V8 starts with a small heap and dynamically grows it to match + /// the set of live objects. This may lead to ineffective garbage collections + /// at startup if the live set is large. Setting the initial heap size avoids + /// such garbage collections. Note that this does not affect young generation + /// garbage collections. + /// + /// When the heap size approaches its maximum, V8 will perform series of + /// garbage collections and invoke the + /// [NearHeapLimitCallback](struct.Isolate.html#method.add_near_heap_limit_callback). + /// If the garbage collections do not help and the callback does not + /// increase the limit, then V8 will crash with V8::FatalProcessOutOfMemory. + /// + /// # Arguments + /// + /// * `physical_memory` - The total amount of physical memory on the current device, in bytes. + /// * `virtual_memory_limit` - The amount of virtual memory on the current device, in bytes, or zero, if there is no limit. + pub fn heap_limits_from_system_memory( + mut self, + physical_memory: u64, + virtual_memory_limit: u64, + ) -> Self { + self + .raw + .constraints + .configure_defaults(physical_memory, virtual_memory_limit); + self + } + /// A CppHeap used to construct the Isolate. V8 takes ownership of the /// CppHeap passed this way. pub fn cpp_heap(mut self, heap: UniqueRef) -> Self { @@ -269,6 +300,11 @@ pub(crate) mod raw { initial_heap_size_in_bytes: usize, maximum_heap_size_in_bytes: usize, ); + fn v8__ResourceConstraints__ConfigureDefaults( + constraints: *mut ResourceConstraints, + physical_memory: u64, + virtual_memory_limit: u64, + ); } impl ResourceConstraints { @@ -285,5 +321,19 @@ pub(crate) mod raw { ) }; } + + pub fn configure_defaults( + &mut self, + physical_memory: u64, + virtual_memory_limit: u64, + ) { + unsafe { + v8__ResourceConstraints__ConfigureDefaults( + self, + physical_memory, + virtual_memory_limit, + ) + } + } } }