diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-04-04 06:46:31 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-04 06:46:31 -0600 |
commit | a1764f7690cfdc3e42724fcad29ef954b7e576a4 (patch) | |
tree | 1b621ebd7a6ef50687eeb2061740895096136e8a /core/examples/ts_module_loader.rs | |
parent | 2dc20168371e827b86e2ce0d1d7787139fba68f3 (diff) |
refactor(core): Improve ergonomics of managing ASCII strings (#18498)
This is a follow-on to the earlier work in reducing string copies,
mainly focused on ensuring that ASCII strings are easy to provide to the
JS runtime.
While we are replacing a 16-byte reference in a number of places with a
24-byte structure (measured via `std::mem::size_of`), the reduction in
copies wins out over the additional size of the arguments passed into
functions.
Benchmarking shows approximately the same if not slightly less wallclock
time/instructions retired, but I believe this continues to open up
further refactoring opportunities.
Diffstat (limited to 'core/examples/ts_module_loader.rs')
-rw-r--r-- | core/examples/ts_module_loader.rs | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/core/examples/ts_module_loader.rs b/core/examples/ts_module_loader.rs index 4a38073ab..6adb27977 100644 --- a/core/examples/ts_module_loader.rs +++ b/core/examples/ts_module_loader.rs @@ -14,6 +14,7 @@ use anyhow::Error; use deno_ast::MediaType; use deno_ast::ParseParams; use deno_ast::SourceTextInfo; +use deno_core::error::AnyError; use deno_core::resolve_import; use deno_core::resolve_path; use deno_core::JsRuntime; @@ -41,11 +42,12 @@ impl ModuleLoader for TypescriptModuleLoader { fn load( &self, module_specifier: &ModuleSpecifier, - _maybe_referrer: Option<ModuleSpecifier>, + _maybe_referrer: Option<&ModuleSpecifier>, _is_dyn_import: bool, ) -> Pin<Box<ModuleSourceFuture>> { - let module_specifier = module_specifier.clone(); - async move { + fn load( + module_specifier: &ModuleSpecifier, + ) -> Result<ModuleSource, AnyError> { let path = module_specifier .to_file_path() .map_err(|_| anyhow!("Only file:// URLs are supported."))?; @@ -81,15 +83,14 @@ impl ModuleLoader for TypescriptModuleLoader { } else { code }; - let module = ModuleSource { - code: code.into(), + Ok(ModuleSource::new( module_type, - module_url_specified: module_specifier.to_string(), - module_url_found: module_specifier.to_string(), - }; - Ok(module) + code.into(), + module_specifier, + )) } - .boxed_local() + + futures::future::ready(load(module_specifier)).boxed_local() } } |