Skip to content

Commit

Permalink
feat: add binding for ResourceConstraints::ConfigureDefaults (#1651)
Browse files Browse the repository at this point in the history
* add binding for `ResourceConstraints::ConfigureDefaults`

* fmt

* fix types
  • Loading branch information
nathanwhit authored Oct 29, 2024
1 parent b0364c0 commit 2509b0b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::HandleScope>* buf,
v8::Isolate* isolate) {
construct_in_place<v8::HandleScope>(buf, isolate);
Expand Down
50 changes: 50 additions & 0 deletions src/isolate_create_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Heap>) -> Self {
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
)
}
}
}
}

0 comments on commit 2509b0b

Please sign in to comment.