diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-03-21 16:33:12 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-21 22:33:12 +0000 |
commit | 0b4770fa7daf274ab01923fb09fd604aeb27e417 (patch) | |
tree | bee10a226239c2787caa87944a5f80783048d9f9 /runtime/web_worker.rs | |
parent | 253b556e6f430012c3094d47838fe397fa588028 (diff) |
perf(core) Reduce script name and script code copies (#18298)
Reduce the number of copies and allocations of script code by carrying
around ownership/reference information from creation time.
As an advantage, this allows us to maintain the identity of `&'static
str`-based scripts and use v8's external 1-byte strings (to avoid
incorrectly passing non-ASCII strings, debug `assert!`s gate all string
reference paths).
Benchmark results:
Perf improvements -- ~0.1 - 0.2ms faster, but should reduce garbage
w/external strings and reduces data copies overall. May also unlock some
more interesting optimizations in the future.
This requires adding some generics to functions, but manual
monomorphization has been applied (outer/inner function) to avoid code
bloat.
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r-- | runtime/web_worker.rs | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index c6fbb8370..0aa142da8 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -25,6 +25,7 @@ use deno_core::CompiledWasmModuleStore; use deno_core::Extension; use deno_core::GetErrorClassFn; use deno_core::JsRuntime; +use deno_core::ModuleCode; use deno_core::ModuleId; use deno_core::ModuleLoader; use deno_core::ModuleSpecifier; @@ -575,16 +576,16 @@ impl WebWorker { "#; let poll_for_messages_fn = self .js_runtime - .execute_script(&located_script_name!(), script) + .execute_script(located_script_name!(), script) .expect("Failed to execute worker bootstrap script"); self.poll_for_messages_fn = Some(poll_for_messages_fn); } /// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script) - pub fn execute_script( + pub fn execute_script<S: Into<ModuleCode>>( &mut self, - name: &str, - source_code: &str, + name: &'static str, + source_code: S, ) -> Result<(), AnyError> { self.js_runtime.execute_script(name, source_code)?; Ok(()) @@ -744,7 +745,7 @@ fn print_worker_error( pub fn run_web_worker( worker: WebWorker, specifier: ModuleSpecifier, - maybe_source_code: Option<String>, + 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>>, @@ -772,8 +773,8 @@ pub fn run_web_worker( }; // Execute provided source code immediately - let result = if let Some(source_code) = maybe_source_code { - let r = worker.execute_script(&located_script_name!(), &source_code); + let result = if let Some(source_code) = maybe_source_code.take() { + let r = worker.execute_script(located_script_name!(), source_code); worker.start_polling_for_messages(); r } else { |