summaryrefslogtreecommitdiff
path: root/runtime/web_worker.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-08-15 13:36:36 -0600
committerGitHub <noreply@github.com>2023-08-16 04:36:36 +0900
commit4380a09a0598c73aa434e2f0f3a34555e0bd55cb (patch)
tree671bba82325653ed188efb49e099c4d61ec318cc /runtime/web_worker.rs
parent41cad2179fb36c2371ab84ce587d3460af64b5fb (diff)
feat(ext/node): eagerly bootstrap node (#20153)
To fix bugs around detection of when node emulation is required, we will just eagerly initialize it. The improvements we make to reduce the impact of the startup time: - [x] Process stdin/stdout/stderr are lazily created - [x] node.js global proxy no longer allocates on each access check - [x] Process checks for `beforeExit` listeners before doing expensive shutdown work - [x] Process should avoid adding global event handlers until listeners are added Benchmarking this PR (`89de7e1ff`) vs main (`41cad2179`) ``` 12:36 $ third_party/prebuilt/mac/hyperfine --warmup 100 -S none './deno-41cad2179 run ./empty.js' './deno-89de7e1ff run ./empty.js' Benchmark 1: ./deno-41cad2179 run ./empty.js Time (mean ± σ): 24.3 ms ± 1.6 ms [User: 16.2 ms, System: 6.0 ms] Range (min … max): 21.1 ms … 29.1 ms 115 runs Benchmark 2: ./deno-89de7e1ff run ./empty.js Time (mean ± σ): 24.0 ms ± 1.4 ms [User: 16.3 ms, System: 5.6 ms] Range (min … max): 21.3 ms … 28.6 ms 126 runs ``` Fixes https://github.com/denoland/deno/issues/20142 Fixes https://github.com/denoland/deno/issues/15826 Fixes https://github.com/denoland/deno/issues/20028
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r--runtime/web_worker.rs36
1 files changed, 2 insertions, 34 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs
index a3b93836c..0c4e95140 100644
--- a/runtime/web_worker.rs
+++ b/runtime/web_worker.rs
@@ -338,8 +338,6 @@ pub struct WebWorkerOptions {
pub module_loader: Rc<dyn ModuleLoader>,
pub npm_resolver: Option<Arc<dyn deno_node::NpmResolver>>,
pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>,
- pub preload_module_cb: Arc<ops::worker_host::WorkerEventCb>,
- pub pre_execute_module_cb: Arc<ops::worker_host::WorkerEventCb>,
pub format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
pub source_map_getter: Option<Box<dyn SourceMapGetter>>,
pub worker_type: WebWorkerType,
@@ -460,8 +458,6 @@ impl WebWorker {
ops::runtime::deno_runtime::init_ops_and_esm(main_module.clone()),
ops::worker_host::deno_worker_host::init_ops_and_esm(
options.create_web_worker_cb.clone(),
- options.preload_module_cb.clone(),
- options.pre_execute_module_cb.clone(),
options.format_js_error_fn.clone(),
),
ops::fs_events::deno_fs_events::init_ops_and_esm(),
@@ -600,7 +596,7 @@ impl WebWorker {
.unwrap()
.into();
bootstrap_fn
- .call(scope, undefined.into(), &[args.into(), name_str, id_str])
+ .call(scope, undefined.into(), &[args, name_str, id_str])
.unwrap();
}
// TODO(bartlomieju): this could be done using V8 API, without calling `execute_script`.
@@ -782,11 +778,9 @@ fn print_worker_error(
/// This function should be called from a thread dedicated to this worker.
// TODO(bartlomieju): check if order of actions is aligned to Worker spec
pub fn run_web_worker(
- worker: WebWorker,
+ mut worker: WebWorker,
specifier: ModuleSpecifier,
mut maybe_source_code: Option<String>,
- preload_module_cb: Arc<ops::worker_host::WorkerEventCb>,
- pre_execute_module_cb: Arc<ops::worker_host::WorkerEventCb>,
format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
) -> Result<(), AnyError> {
let name = worker.name.to_string();
@@ -796,20 +790,6 @@ pub fn run_web_worker(
let fut = async move {
let internal_handle = worker.internal_handle.clone();
- let result = (preload_module_cb)(worker).await;
-
- let mut worker = match result {
- Ok(worker) => worker,
- Err(e) => {
- print_worker_error(&e, &name, format_js_error_fn.as_deref());
- internal_handle
- .post_event(WorkerControlEvent::TerminalError(e))
- .expect("Failed to post message to host");
-
- // Failure to execute script is a terminal error, bye, bye.
- return Ok(());
- }
- };
// Execute provided source code immediately
let result = if let Some(source_code) = maybe_source_code.take() {
@@ -821,18 +801,6 @@ pub fn run_web_worker(
// script instead of module
match worker.preload_main_module(&specifier).await {
Ok(id) => {
- worker = match (pre_execute_module_cb)(worker).await {
- Ok(worker) => worker,
- Err(e) => {
- print_worker_error(&e, &name, format_js_error_fn.as_deref());
- internal_handle
- .post_event(WorkerControlEvent::TerminalError(e))
- .expect("Failed to post message to host");
-
- // Failure to execute script is a terminal error, bye, bye.
- return Ok(());
- }
- };
worker.start_polling_for_messages();
worker.execute_main_module(id).await
}