From 5e497b8c08468bc5eb3b3ca3093ee52b52f4845d Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 16 Oct 2020 14:50:10 +0200 Subject: [PATCH] refactor(core): more control over isolate creation 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. --- core/lib.rs | 1 - core/runtime.rs | 50 +++++++++++-------------------------------------- 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/core/lib.rs b/core/lib.rs index e03ce2a7d7b6a..48968fa97d332 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -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; diff --git a/core/runtime.rs b/core/runtime.rs index 0748abc1daaf4..8bc8cda082d2a 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -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, + /// Isolate creation parameters. + pub create_params: Option, } 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();