summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-03-21 16:33:12 -0600
committerGitHub <noreply@github.com>2023-03-21 22:33:12 +0000
commit0b4770fa7daf274ab01923fb09fd604aeb27e417 (patch)
treebee10a226239c2787caa87944a5f80783048d9f9 /runtime
parent253b556e6f430012c3094d47838fe397fa588028 (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')
-rw-r--r--runtime/build.rs7
-rw-r--r--runtime/web_worker.rs15
-rw-r--r--runtime/worker.rs13
3 files changed, 19 insertions, 16 deletions
diff --git a/runtime/build.rs b/runtime/build.rs
index 016ece810..ec7c9642c 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -17,11 +17,12 @@ mod startup_snapshot {
use deno_core::snapshot_util::*;
use deno_core::Extension;
use deno_core::ExtensionFileSource;
+ use deno_core::ModuleCode;
use std::path::Path;
fn transpile_ts_for_snapshotting(
file_source: &ExtensionFileSource,
- ) -> Result<String, AnyError> {
+ ) -> Result<ModuleCode, AnyError> {
let media_type = MediaType::from_path(Path::new(&file_source.specifier));
let should_transpile = match media_type {
@@ -41,7 +42,7 @@ mod startup_snapshot {
let parsed = deno_ast::parse_module(ParseParams {
specifier: file_source.specifier.to_string(),
- text_info: SourceTextInfo::from_string(code),
+ text_info: SourceTextInfo::from_string(code.take_as_string()),
media_type,
capture_tokens: false,
scope_analysis: false,
@@ -53,7 +54,7 @@ mod startup_snapshot {
..Default::default()
})?;
- Ok(transpiled_source.text)
+ Ok(transpiled_source.text.into())
}
#[derive(Clone)]
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 {
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,