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 /cli/module_loader.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 'cli/module_loader.rs')
-rw-r--r-- | cli/module_loader.rs | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/cli/module_loader.rs b/cli/module_loader.rs index c5224a8b9..7f6101d80 100644 --- a/cli/module_loader.rs +++ b/cli/module_loader.rs @@ -14,6 +14,7 @@ use deno_core::error::AnyError; use deno_core::futures::future::FutureExt; use deno_core::futures::Future; use deno_core::resolve_url; +use deno_core::ModuleCode; use deno_core::ModuleLoader; use deno_core::ModuleSource; use deno_core::ModuleSpecifier; @@ -30,7 +31,7 @@ use std::rc::Rc; use std::str; struct ModuleCodeSource { - pub code: String, + pub code: ModuleCode, pub found_url: ModuleSpecifier, pub media_type: MediaType, } @@ -91,7 +92,7 @@ impl CliModuleLoader { specifier, .. })) => Ok(ModuleCodeSource { - code: source.to_string(), + code: source.into(), found_url: specifier.clone(), media_type: *media_type, }), @@ -101,13 +102,15 @@ impl CliModuleLoader { specifier, .. })) => { - let code = match media_type { + let code: ModuleCode = match media_type { MediaType::JavaScript | MediaType::Unknown | MediaType::Cjs | MediaType::Mjs - | MediaType::Json => source.to_string(), - MediaType::Dts | MediaType::Dcts | MediaType::Dmts => "".to_string(), + | MediaType::Json => source.into(), + MediaType::Dts | MediaType::Dcts | MediaType::Dmts => { + Default::default() + } MediaType::TypeScript | MediaType::Mts | MediaType::Cts @@ -191,7 +194,7 @@ impl CliModuleLoader { )? }; ModuleCodeSource { - code, + code: code.into(), found_url: specifier.clone(), media_type: MediaType::from_specifier(specifier), } @@ -208,7 +211,7 @@ impl CliModuleLoader { code_without_source_map(code_source.code) }; Ok(ModuleSource { - code: code.into_bytes().into_boxed_slice(), + code, module_url_specified: specifier.to_string(), module_url_found: code_source.found_url.to_string(), module_type: match code_source.media_type { |