Skip to content

Commit

Permalink
Reduce script name copies
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed Mar 21, 2023
1 parent b96a592 commit c23773d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub mod _ops {
#[macro_export]
macro_rules! located_script_name {
() => {
format!("[ext:{}:{}:{}]", std::file!(), std::line!(), std::column!());
concat!("[ext:{}:{}:{}]", std::file!(), std::line!(), std::column!());
};
}

Expand Down
94 changes: 83 additions & 11 deletions core/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,32 @@ pub(crate) enum ModuleError {
Other(Error),
}

pub enum ModuleName<'a> {
Static(&'static str),
NotStatic(&'a str),
}

impl<'a> ModuleName<'a> {
pub fn as_ref(&self) -> &'a str {
match self {
ModuleName::Static(s) => s,
ModuleName::NotStatic(s) => s,
}
}
}

impl<'a, S: AsRef<str>> From<&'a S> for ModuleName<'a> {
fn from(s: &'a S) -> Self {
Self::NotStatic(s.as_ref())
}
}

impl From<&'static str> for ModuleName<'static> {
fn from(value: &'static str) -> Self {
Self::Static(value)
}
}

/// A collection of JS modules.
pub(crate) struct ModuleMap {
// Handling of specifiers and v8 objects
Expand Down Expand Up @@ -1485,13 +1511,35 @@ impl ModuleMap {
}
}

fn new_json_module(
fn string_from_module_name<'a>(
scope: &mut v8::HandleScope<'a>,
name: &ModuleName,
) -> Option<v8::Local<'a, v8::String>> {
match name {
ModuleName::Static(s) => {
assert!(s.is_ascii());
v8::String::new_external_onebyte_static(scope, s.as_bytes())
}
ModuleName::NotStatic(s) => v8::String::new(scope, s),
}
}

fn new_json_module<'a, N: Into<ModuleName<'a>>>(
&mut self,
scope: &mut v8::HandleScope,
name: &str,
name: N,
source: &ModuleCode,
) -> Result<ModuleId, ModuleError> {
self.new_json_module_inner(scope, name.into(), source)
}

fn new_json_module_inner(
&mut self,
scope: &mut v8::HandleScope,
name: ModuleName,
source: &ModuleCode,
) -> Result<ModuleId, ModuleError> {
let name_str = v8::String::new(scope, name).unwrap();
let name_str = Self::string_from_module_name(scope, &name).unwrap();
let source_str = v8::String::new_from_utf8(
scope,
strip_bom(source.as_bytes()),
Expand Down Expand Up @@ -1523,22 +1571,46 @@ impl ModuleMap {
let value_handle = v8::Global::<v8::Value>::new(tc_scope, parsed_json);
self.json_value_store.insert(handle.clone(), value_handle);

let id =
self.create_module_info(name, ModuleType::Json, handle, false, vec![]);
let id = self.create_module_info(
name.as_ref(),
ModuleType::Json,
handle,
false,
vec![],
);

Ok(id)
}

// Create and compile an ES module.
pub(crate) fn new_es_module(
pub(crate) fn new_es_module<'a, N: Into<ModuleName<'a>>>(
&mut self,
scope: &mut v8::HandleScope,
main: bool,
name: &str,
name: N,
source: &ModuleCode,
is_dynamic_import: bool,
) -> Result<ModuleId, ModuleError> {
self.new_es_module_inner(
scope,
main,
name.into(),
source,
is_dynamic_import,
)
}

// Create and compile an ES module.
fn new_es_module_inner(
&mut self,
scope: &mut v8::HandleScope,
main: bool,
name: ModuleName,
source: &ModuleCode,
is_dynamic_import: bool,
) -> Result<ModuleId, ModuleError> {
let name_str = v8::String::new(scope, name).unwrap();
assert!(name.is_ascii());
let name_str = Self::string_from_module_name(scope, &name).unwrap();
let source_str = Self::string_from_code(scope, source).unwrap();

let origin = bindings::module_origin(scope, name_str);
Expand Down Expand Up @@ -1589,7 +1661,7 @@ impl ModuleMap {
self.snapshot_loaded_and_not_snapshotting,
self.loader.clone(),
&import_specifier,
name,
name.as_ref(),
if is_dynamic_import {
ResolutionKind::DynamicImport
} else {
Expand All @@ -1613,15 +1685,15 @@ impl ModuleMap {
if let Some(main_module) = maybe_main_module {
return Err(ModuleError::Other(generic_error(
format!("Trying to create \"main\" module ({:?}), when one already exists ({:?})",
name,
name.as_ref(),
main_module.name,
))));
}
}

let handle = v8::Global::<v8::Module>::new(tc_scope, module);
let id = self.create_module_info(
name,
name.as_ref(),
ModuleType::JavaScript,
handle,
main,
Expand Down
28 changes: 11 additions & 17 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ impl JsRuntime {
#[inline(always)]
pub fn execute_script<S: Into<ModuleCode>>(
&mut self,
name: &str,
name: &'static str,
source_code: S,
) -> Result<v8::Global<v8::Value>, Error> {
self.execute_module_code(name, source_code.into())
Expand All @@ -940,7 +940,7 @@ impl JsRuntime {
/// `Error` can usually be downcast to `JsError`.
pub fn execute_module_code(
&mut self,
name: &str,
name: &'static str,
source_code: ModuleCode,
) -> Result<v8::Global<v8::Value>, Error> {
self
Expand Down Expand Up @@ -2091,12 +2091,8 @@ impl JsRuntime {
module_map_rc
.borrow_mut()
.new_es_module(
scope,
// main module
true,
specifier.as_str(),
&code,
false,
scope, // main module
true, specifier, &code, false,
)
.map_err(|e| match e {
ModuleError::Exception(exception) => {
Expand Down Expand Up @@ -2151,12 +2147,8 @@ impl JsRuntime {
module_map_rc
.borrow_mut()
.new_es_module(
scope,
// not main module
false,
specifier.as_str(),
&code,
false,
scope, // not main module
false, specifier, &code, false,
)
.map_err(|e| match e {
ModuleError::Exception(exception) => {
Expand Down Expand Up @@ -2534,7 +2526,7 @@ impl JsRealm {
pub fn execute_script<S: Into<ModuleCode>>(
&self,
isolate: &mut v8::Isolate,
name: &str,
name: &'static str,
source_code: S,
) -> Result<v8::Global<v8::Value>, Error> {
self.execute_module_code(isolate, name, source_code.into())
Expand All @@ -2543,13 +2535,15 @@ impl JsRealm {
fn execute_module_code(
&self,
isolate: &mut v8::Isolate,
name: &str,
name: &'static str,
source_code: ModuleCode,
) -> Result<v8::Global<v8::Value>, Error> {
let scope = &mut self.handle_scope(isolate);

let source = Self::string_from_code(scope, &source_code).unwrap();
let name = v8::String::new(scope, name).unwrap();
assert!(name.is_ascii());
let name =
v8::String::new_external_onebyte_static(scope, name.as_bytes()).unwrap();
let origin = bindings::script_origin(scope, name);

let tc_scope = &mut v8::TryCatch::new(scope);
Expand Down
2 changes: 1 addition & 1 deletion runtime/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ impl WebWorker {
/// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script)
pub fn execute_script<S: Into<ModuleCode>>(
&mut self,
name: &str,
name: &'static str,
source_code: S,
) -> Result<(), AnyError> {
self.js_runtime.execute_script(name, source_code)?;
Expand Down
8 changes: 4 additions & 4 deletions runtime/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl MainWorker {
/// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script)
pub fn execute_script<S: Into<ModuleCode>>(
&mut self,
script_name: &str,
script_name: &'static str,
source_code: S,
) -> Result<v8::Global<v8::Value>, AnyError> {
self.js_runtime.execute_script(script_name, source_code)
Expand Down Expand Up @@ -503,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,
Expand All @@ -520,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,
Expand All @@ -537,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,
Expand Down

0 comments on commit c23773d

Please sign in to comment.