perf: use available system memory for v8 isolate memory limit (#26868)

Instead of using the default 1.4Gb limit (which was meant for browser
tabs) configure V8 to
set the heap limit to the amount of memory available in the system.

Closes https://github.com/denoland/deno/issues/23424
Closes https://github.com/denoland/deno/issues/26435
Closes https://github.com/denoland/deno/issues/21226
This commit is contained in:
Bartek Iwańczuk 2024-11-15 09:33:03 +00:00 committed by GitHub
parent dcc75d5685
commit c9baf3849f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 23 additions and 3 deletions

View File

@ -34,6 +34,7 @@ use crate::util::path::relative_specifier;
use crate::util::path::to_percent_decoded_str; use crate::util::path::to_percent_decoded_str;
use crate::util::result::InfallibleResultExt; use crate::util::result::InfallibleResultExt;
use crate::util::v8::convert; use crate::util::v8::convert;
use crate::worker::create_isolate_create_params;
use deno_core::convert::Smi; use deno_core::convert::Smi;
use deno_core::convert::ToV8; use deno_core::convert::ToV8;
use deno_core::error::StdAnyError; use deno_core::error::StdAnyError;
@ -4760,6 +4761,7 @@ fn run_tsc_thread(
specifier_map, specifier_map,
request_rx, request_rx,
)], )],
create_params: create_isolate_create_params(),
startup_snapshot: Some(tsc::compiler_snapshot()), startup_snapshot: Some(tsc::compiler_snapshot()),
inspector: has_inspector_server, inspector: has_inspector_server,
..Default::default() ..Default::default()

View File

@ -9,6 +9,7 @@ use crate::npm::CliNpmResolver;
use crate::resolver::CjsTracker; use crate::resolver::CjsTracker;
use crate::util::checksum; use crate::util::checksum;
use crate::util::path::mapped_specifier_for_tsc; use crate::util::path::mapped_specifier_for_tsc;
use crate::worker::create_isolate_create_params;
use deno_ast::MediaType; use deno_ast::MediaType;
use deno_core::anyhow::anyhow; use deno_core::anyhow::anyhow;
@ -1104,6 +1105,7 @@ pub fn exec(request: Request) -> Result<Response, AnyError> {
root_map, root_map,
remapped_specifiers, remapped_specifiers,
)], )],
create_params: create_isolate_create_params(),
..Default::default() ..Default::default()
}); });

View File

@ -555,6 +555,7 @@ impl CliMainWorkerFactory {
permissions, permissions,
v8_code_cache: shared.code_cache.clone(), v8_code_cache: shared.code_cache.clone(),
}; };
let options = WorkerOptions { let options = WorkerOptions {
bootstrap: BootstrapOptions { bootstrap: BootstrapOptions {
deno_version: crate::version::DENO_VERSION_INFO.deno.to_string(), deno_version: crate::version::DENO_VERSION_INFO.deno.to_string(),
@ -585,7 +586,7 @@ impl CliMainWorkerFactory {
}, },
extensions: custom_extensions, extensions: custom_extensions,
startup_snapshot: crate::js::deno_isolate_init(), startup_snapshot: crate::js::deno_isolate_init(),
create_params: None, create_params: create_isolate_create_params(),
unsafely_ignore_certificate_errors: shared unsafely_ignore_certificate_errors: shared
.options .options
.unsafely_ignore_certificate_errors .unsafely_ignore_certificate_errors
@ -786,6 +787,7 @@ fn create_web_worker_callback(
}, },
extensions: vec![], extensions: vec![],
startup_snapshot: crate::js::deno_isolate_init(), startup_snapshot: crate::js::deno_isolate_init(),
create_params: create_isolate_create_params(),
unsafely_ignore_certificate_errors: shared unsafely_ignore_certificate_errors: shared
.options .options
.unsafely_ignore_certificate_errors .unsafely_ignore_certificate_errors
@ -806,6 +808,17 @@ fn create_web_worker_callback(
}) })
} }
/// By default V8 uses 1.4Gb heap limit which is meant for browser tabs.
/// Instead probe for the total memory on the system and use it instead
/// as a default.
pub fn create_isolate_create_params() -> Option<v8::CreateParams> {
let maybe_mem_info = deno_runtime::sys_info::mem_info();
maybe_mem_info.map(|mem_info| {
v8::CreateParams::default()
.heap_limits_from_system_memory(mem_info.total, 0)
})
}
#[allow(clippy::print_stdout)] #[allow(clippy::print_stdout)]
#[allow(clippy::print_stderr)] #[allow(clippy::print_stderr)]
#[cfg(test)] #[cfg(test)]

View File

@ -35,6 +35,7 @@ pub mod js;
pub mod ops; pub mod ops;
pub mod permissions; pub mod permissions;
pub mod snapshot; pub mod snapshot;
pub mod sys_info;
pub mod tokio_util; pub mod tokio_util;
pub mod web_worker; pub mod web_worker;
pub mod worker; pub mod worker;

View File

@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use crate::sys_info;
use crate::worker::ExitCode; use crate::worker::ExitCode;
use deno_core::op2; use deno_core::op2;
use deno_core::v8; use deno_core::v8;
@ -11,8 +12,6 @@ use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
mod sys_info;
deno_core::extension!( deno_core::extension!(
deno_os, deno_os,
ops = [ ops = [

View File

@ -361,6 +361,8 @@ pub struct WebWorkerOptions {
pub extensions: Vec<Extension>, pub extensions: Vec<Extension>,
pub startup_snapshot: Option<&'static [u8]>, pub startup_snapshot: Option<&'static [u8]>,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>, pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
/// Optional isolate creation parameters, such as heap limits.
pub create_params: Option<v8::CreateParams>,
pub seed: Option<u64>, pub seed: Option<u64>,
pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>, pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>,
pub format_js_error_fn: Option<Arc<FormatJsErrorFn>>, pub format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
@ -563,6 +565,7 @@ impl WebWorker {
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(services.module_loader), module_loader: Some(services.module_loader),
startup_snapshot: options.startup_snapshot, startup_snapshot: options.startup_snapshot,
create_params: options.create_params,
get_error_class_fn: options.get_error_class_fn, get_error_class_fn: options.get_error_class_fn,
shared_array_buffer_store: services.shared_array_buffer_store, shared_array_buffer_store: services.shared_array_buffer_store,
compiled_wasm_module_store: services.compiled_wasm_module_store, compiled_wasm_module_store: services.compiled_wasm_module_store,