1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

perf: module info cache - avoid MediaType.to_string() allocation (#17699)

Micro optimization because these allocations were coming up on a flame
graph I was looking at (only 0.28% of total).
This commit is contained in:
David Sherret 2023-02-09 09:17:48 -05:00 committed by GitHub
parent 8b0a612e30
commit 717daf4748
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -191,7 +191,7 @@ impl ParsedSourceCacheModuleAnalyzer {
let mut stmt = self.conn.prepare_cached(query)?;
let mut rows = stmt.query(params![
&specifier.as_str(),
&media_type.to_string(),
serialize_media_type(media_type),
&expected_source_hash,
])?;
if let Some(row) = rows.next()? {
@ -218,7 +218,7 @@ impl ParsedSourceCacheModuleAnalyzer {
let mut stmt = self.conn.prepare_cached(sql)?;
stmt.execute(params![
specifier.as_str(),
&media_type.to_string(),
serialize_media_type(media_type),
&source_hash,
&serde_json::to_string(&module_info)?,
])?;
@ -226,6 +226,30 @@ impl ParsedSourceCacheModuleAnalyzer {
}
}
// todo(dsherret): change this to be stored as an integer next time
// the cache version is bumped
fn serialize_media_type(media_type: MediaType) -> &'static str {
use MediaType::*;
match media_type {
JavaScript => "1",
Jsx => "2",
Mjs => "3",
Cjs => "4",
TypeScript => "5",
Mts => "6",
Cts => "7",
Dts => "8",
Dmts => "9",
Dcts => "10",
Tsx => "11",
Json => "12",
Wasm => "13",
TsBuildInfo => "14",
SourceMap => "15",
Unknown => "16",
}
}
impl deno_graph::ModuleAnalyzer for ParsedSourceCacheModuleAnalyzer {
fn analyze(
&self,