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/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/worker.rs')
-rw-r--r-- | runtime/worker.rs | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/runtime/worker.rs b/runtime/worker.rs index a995861c5..a24a22c96 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -21,6 +21,7 @@ use deno_core::FsModuleLoader; use deno_core::GetErrorClassFn; use deno_core::JsRuntime; use deno_core::LocalInspectorSession; +use deno_core::ModuleCode; use deno_core::ModuleId; use deno_core::ModuleLoader; use deno_core::ModuleSpecifier; @@ -364,10 +365,10 @@ impl MainWorker { } /// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script) - pub fn execute_script( + pub fn execute_script<S: Into<ModuleCode>>( &mut self, - script_name: &str, - source_code: &str, + script_name: &'static str, + source_code: S, ) -> Result<v8::Global<v8::Value>, AnyError> { self.js_runtime.execute_script(script_name, source_code) } @@ -502,7 +503,7 @@ impl MainWorker { /// Does not poll event loop, and thus not await any of the "load" event handlers. pub fn dispatch_load_event( &mut self, - script_name: &str, + script_name: &'static str, ) -> Result<(), AnyError> { self.execute_script( script_name, @@ -519,7 +520,7 @@ impl MainWorker { /// Does not poll event loop, and thus not await any of the "unload" event handlers. pub fn dispatch_unload_event( &mut self, - script_name: &str, + script_name: &'static str, ) -> Result<(), AnyError> { self.execute_script( script_name, @@ -536,7 +537,7 @@ impl MainWorker { /// running. pub fn dispatch_beforeunload_event( &mut self, - script_name: &str, + script_name: &'static str, ) -> Result<bool, AnyError> { let value = self.js_runtime.execute_script( script_name, |