1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 15:19:40 -05:00

refactor(core): more control over isolate creation (#8000)

Make JSRuntime::new() accept a custom v8::CreateParams object to tune
the v8::Isolate it creates.

Subsumes the functionality of HeapLimits, which I therefore removed.
This commit is contained in:
Ben Noordhuis 2020-10-17 11:56:15 +02:00 committed by GitHub
parent 943b0980c7
commit 46b892ad37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 40 deletions

View file

@ -48,7 +48,6 @@ pub use crate::ops::OpState;
pub use crate::ops::OpTable;
pub use crate::resources::ResourceTable;
pub use crate::runtime::GetErrorClassFn;
pub use crate::runtime::HeapLimits;
pub use crate::runtime::JsRuntime;
pub use crate::runtime::RuntimeOptions;
pub use crate::runtime::Snapshot;

View file

@ -162,22 +162,6 @@ pub unsafe fn v8_init() {
v8::V8::set_flags_from_command_line(argv);
}
/// Minimum and maximum bytes of heap used in an isolate
pub struct HeapLimits {
/// 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.
pub initial: usize,
/// When the heap size approaches `max`, V8 will perform series of
/// garbage collections and invoke the
/// [NearHeapLimitCallback](TODO).
/// If the garbage collections do not help and the callback does not
/// increase the limit, then V8 will crash with V8::FatalProcessOutOfMemory.
pub max: usize,
}
#[derive(Default)]
pub struct RuntimeOptions {
/// Allows a callback to be set whenever a V8 exception is made. This allows
@ -202,17 +186,12 @@ pub struct RuntimeOptions {
/// Currently can't be used with `startup_snapshot`.
pub will_snapshot: bool,
/// This is useful for controlling memory usage of scripts.
///
/// See [`HeapLimits`](struct.HeapLimits.html) for more details.
///
/// Make sure to use [`add_near_heap_limit_callback`](#method.add_near_heap_limit_callback)
/// to prevent v8 from crashing when reaching the upper limit.
pub heap_limits: Option<HeapLimits>,
/// Isolate creation parameters.
pub create_params: Option<v8::CreateParams>,
}
impl JsRuntime {
pub fn new(options: RuntimeOptions) -> Self {
pub fn new(mut options: RuntimeOptions) -> Self {
static DENO_INIT: Once = Once::new();
DENO_INIT.call_once(|| {
unsafe { v8_init() };
@ -234,7 +213,10 @@ impl JsRuntime {
}
(isolate, Some(creator))
} else {
let mut params = v8::Isolate::create_params()
let mut params = options
.create_params
.take()
.unwrap_or_else(v8::Isolate::create_params)
.external_references(&**bindings::EXTERNAL_REFERENCES);
let snapshot_loaded = if let Some(snapshot) = options.startup_snapshot {
params = match snapshot {
@ -247,10 +229,6 @@ impl JsRuntime {
false
};
if let Some(heap_limits) = options.heap_limits {
params = params.heap_limits(heap_limits.initial, heap_limits.max)
}
let isolate = v8::Isolate::new(params);
let mut isolate = JsRuntime::setup_isolate(isolate);
{
@ -2078,12 +2056,9 @@ pub mod tests {
#[test]
fn test_heap_limits() {
let heap_limits = HeapLimits {
initial: 0,
max: 20 * 1024, // 20 kB
};
let create_params = v8::Isolate::create_params().heap_limits(0, 20 * 1024);
let mut runtime = JsRuntime::new(RuntimeOptions {
heap_limits: Some(heap_limits),
create_params: Some(create_params),
..Default::default()
});
let cb_handle = runtime.v8_isolate().thread_safe_handle();
@ -2124,12 +2099,9 @@ pub mod tests {
#[test]
fn test_heap_limit_cb_multiple() {
let heap_limits = HeapLimits {
initial: 0,
max: 20 * 1024, // 20 kB
};
let create_params = v8::Isolate::create_params().heap_limits(0, 20 * 1024);
let mut runtime = JsRuntime::new(RuntimeOptions {
heap_limits: Some(heap_limits),
create_params: Some(create_params),
..Default::default()
});
let cb_handle = runtime.v8_isolate().thread_safe_handle();