summaryrefslogtreecommitdiff
path: root/cli/proc_state.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2022-06-20 13:42:20 +0100
committerGitHub <noreply@github.com>2022-06-20 14:42:20 +0200
commit79b42808a474c0a049ae17a3b6b1f93ad83d217b (patch)
tree937c16d6f2c5928a784ed28e60dffe18c3517643 /cli/proc_state.rs
parent94d369ebc65a55bd9fbf378a765c8ed88a4efe2c (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/proc_state.rs')
-rw-r--r--cli/proc_state.rs29
1 files changed, 10 insertions, 19 deletions
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
}