summaryrefslogtreecommitdiff
path: root/cli/util/text_encoding.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-03-21 16:33:12 -0600
committerGitHub <noreply@github.com>2023-03-21 22:33:12 +0000
commit0b4770fa7daf274ab01923fb09fd604aeb27e417 (patch)
treebee10a226239c2787caa87944a5f80783048d9f9 /cli/util/text_encoding.rs
parent253b556e6f430012c3094d47838fe397fa588028 (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/util/text_encoding.rs')
-rw-r--r--cli/util/text_encoding.rs36
1 files changed, 21 insertions, 15 deletions
diff --git a/cli/util/text_encoding.rs b/cli/util/text_encoding.rs
index 87067e909..0111ec82f 100644
--- a/cli/util/text_encoding.rs
+++ b/cli/util/text_encoding.rs
@@ -1,5 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+use deno_core::ModuleCode;
use encoding_rs::*;
use std::borrow::Cow;
use std::io::Error;
@@ -53,11 +54,12 @@ pub fn strip_bom(text: &str) -> &str {
}
}
-static SOURCE_MAP_PREFIX: &str =
- "//# sourceMappingURL=data:application/json;base64,";
+static SOURCE_MAP_PREFIX: &[u8] =
+ b"//# sourceMappingURL=data:application/json;base64,";
-pub fn source_map_from_code(code: &str) -> Option<Vec<u8>> {
- let last_line = code.rsplit(|u| u == '\n').next()?;
+pub fn source_map_from_code(code: &ModuleCode) -> Option<Vec<u8>> {
+ let bytes = code.as_bytes();
+ let last_line = bytes.rsplit(|u| *u == b'\n').next()?;
if last_line.starts_with(SOURCE_MAP_PREFIX) {
let input = last_line.split_at(SOURCE_MAP_PREFIX.len()).1;
let decoded_map = base64::decode(input)
@@ -68,17 +70,18 @@ pub fn source_map_from_code(code: &str) -> Option<Vec<u8>> {
}
}
-pub fn code_without_source_map(mut code: String) -> String {
- if let Some(last_line_index) = code.rfind('\n') {
- if code[last_line_index + 1..].starts_with(SOURCE_MAP_PREFIX) {
- code.truncate(last_line_index + 1);
- code
- } else {
- code
+/// Truncate the source code before the source map.
+pub fn code_without_source_map(mut code: ModuleCode) -> ModuleCode {
+ let bytes = code.as_bytes();
+ for i in (0..bytes.len()).rev() {
+ if bytes[i] == b'\n' {
+ if bytes[i + 1..].starts_with(SOURCE_MAP_PREFIX) {
+ code.truncate(i + 1);
+ }
+ return code;
}
- } else {
- code
}
+ code
}
#[cfg(test)]
@@ -155,8 +158,11 @@ mod tests {
"\n",
);
- fn run_test(input: &str, output: &str) {
- assert_eq!(code_without_source_map(input.to_string()), output);
+ fn run_test(input: &'static str, output: &'static str) {
+ assert_eq!(
+ code_without_source_map(input.into()).take_as_string(),
+ output
+ );
}
}
}