diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2022-06-20 13:42:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-20 14:42:20 +0200 |
commit | 79b42808a474c0a049ae17a3b6b1f93ad83d217b (patch) | |
tree | 937c16d6f2c5928a784ed28e60dffe18c3517643 /cli | |
parent | 94d369ebc65a55bd9fbf378a765c8ed88a4efe2c (diff) |
perf(core): Cache source lookups (#14816)
Keep a cache for source maps and source lines.
We sort of already had a cache argument for source map lookup
functions but we just passed an empty map instead of storing it.
Extended it to cache source line lookups as well and plugged it
into runtime state.
Diffstat (limited to 'cli')
-rw-r--r-- | cli/fmt_errors.rs | 5 | ||||
-rw-r--r-- | cli/proc_state.rs | 29 |
2 files changed, 11 insertions, 23 deletions
diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index d30f661a9..37a58364a 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -8,8 +8,6 @@ use deno_core::error::format_file_name; use deno_core::error::JsError; use deno_core::error::JsStackFrame; -const SOURCE_ABBREV_THRESHOLD: usize = 150; - // Keep in sync with `/core/error.js`. pub fn format_location(frame: &JsStackFrame) -> String { let _internal = frame @@ -115,8 +113,7 @@ fn format_maybe_source_line( let source_line = source_line.unwrap(); // sometimes source_line gets set with an empty string, which then outputs // an empty source line when displayed, so need just short circuit here. - // Also short-circuit on error line too long. - if source_line.is_empty() || source_line.len() > SOURCE_ABBREV_THRESHOLD { + if source_line.is_empty() { return "".to_string(); } if source_line.contains("Couldn't format source line: ") { diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 2c454c0ee..9e948643d 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -694,11 +694,9 @@ impl SourceMapGetter for ProcState { _ => return None, } if let Some((code, maybe_map)) = self.get_emit(&specifier) { - let code = String::from_utf8(code).unwrap(); - source_map_from_code(code).or(maybe_map) + source_map_from_code(&code).or(maybe_map) } else if let Ok(source) = self.load(specifier, None, false) { - let code = String::from_utf8(source.code.to_vec()).unwrap(); - source_map_from_code(code) + source_map_from_code(&source.code) } else { None } @@ -756,21 +754,14 @@ pub fn import_map_from_text( Ok(result.import_map) } -fn source_map_from_code(code: String) -> Option<Vec<u8>> { - let lines: Vec<&str> = code.split('\n').collect(); - if let Some(last_line) = lines.last() { - if last_line - .starts_with("//# sourceMappingURL=data:application/json;base64,") - { - let input = last_line.trim_start_matches( - "//# sourceMappingURL=data:application/json;base64,", - ); - let decoded_map = base64::decode(input) - .expect("Unable to decode source map from emitted file."); - Some(decoded_map) - } else { - None - } +fn source_map_from_code(code: &[u8]) -> Option<Vec<u8>> { + static PREFIX: &[u8] = b"//# sourceMappingURL=data:application/json;base64,"; + let last_line = code.rsplitn(2, |u| u == &b'\n').next().unwrap(); + if last_line.starts_with(PREFIX) { + let input = last_line.split_at(PREFIX.len()).1; + let decoded_map = base64::decode(input) + .expect("Unable to decode source map from emitted file."); + Some(decoded_map) } else { None } |