mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 18:38:33 +00:00
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.
This commit is contained in:
parent
253b556e6f
commit
0b4770fa7d
19 changed files with 457 additions and 176 deletions
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue