mirror of
https://github.com/denoland/deno.git
synced 2024-11-28 16:20:57 -05:00
0b4770fa7d
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.
48 lines
1.5 KiB
Rust
48 lines
1.5 KiB
Rust
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use crate::cache::EmitCache;
|
|
use crate::cache::FastInsecureHasher;
|
|
use crate::cache::ParsedSourceCache;
|
|
|
|
use deno_core::error::AnyError;
|
|
use deno_core::ModuleCode;
|
|
use deno_core::ModuleSpecifier;
|
|
use deno_graph::MediaType;
|
|
use std::sync::Arc;
|
|
|
|
/// A hashing function that takes the source code and emit options
|
|
/// hash then generates a string hash which can be stored to
|
|
/// determine if the cached emit is valid or not.
|
|
pub fn get_source_hash(source_text: &str, emit_options_hash: u64) -> u64 {
|
|
FastInsecureHasher::new()
|
|
.write_str(source_text)
|
|
.write_u64(emit_options_hash)
|
|
.finish()
|
|
}
|
|
|
|
pub fn emit_parsed_source(
|
|
emit_cache: &EmitCache,
|
|
parsed_source_cache: &ParsedSourceCache,
|
|
specifier: &ModuleSpecifier,
|
|
media_type: MediaType,
|
|
source: &Arc<str>,
|
|
emit_options: &deno_ast::EmitOptions,
|
|
emit_config_hash: u64,
|
|
) -> Result<ModuleCode, AnyError> {
|
|
let source_hash = get_source_hash(source, emit_config_hash);
|
|
|
|
if let Some(emit_code) = emit_cache.get_emit_code(specifier, source_hash) {
|
|
Ok(emit_code.into())
|
|
} else {
|
|
// this will use a cached version if it exists
|
|
let parsed_source = parsed_source_cache.get_or_parse_module(
|
|
specifier,
|
|
source.clone(),
|
|
media_type,
|
|
)?;
|
|
let transpiled_source = parsed_source.transpile(emit_options)?;
|
|
debug_assert!(transpiled_source.source_map.is_none());
|
|
emit_cache.set_emit_code(specifier, source_hash, &transpiled_source.text);
|
|
Ok(transpiled_source.text.into())
|
|
}
|
|
}
|