Skip to content

Commit 717daf4

Browse files
authored
perf: module info cache - avoid MediaType.to_string() allocation (denoland#17699)
Micro optimization because these allocations were coming up on a flame graph I was looking at (only 0.28% of total).
1 parent 8b0a612 commit 717daf4

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

cli/cache/parsed_source.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl ParsedSourceCacheModuleAnalyzer {
191191
let mut stmt = self.conn.prepare_cached(query)?;
192192
let mut rows = stmt.query(params![
193193
&specifier.as_str(),
194-
&media_type.to_string(),
194+
serialize_media_type(media_type),
195195
&expected_source_hash,
196196
])?;
197197
if let Some(row) = rows.next()? {
@@ -218,14 +218,38 @@ impl ParsedSourceCacheModuleAnalyzer {
218218
let mut stmt = self.conn.prepare_cached(sql)?;
219219
stmt.execute(params![
220220
specifier.as_str(),
221-
&media_type.to_string(),
221+
serialize_media_type(media_type),
222222
&source_hash,
223223
&serde_json::to_string(&module_info)?,
224224
])?;
225225
Ok(())
226226
}
227227
}
228228

229+
// todo(dsherret): change this to be stored as an integer next time
230+
// the cache version is bumped
231+
fn serialize_media_type(media_type: MediaType) -> &'static str {
232+
use MediaType::*;
233+
match media_type {
234+
JavaScript => "1",
235+
Jsx => "2",
236+
Mjs => "3",
237+
Cjs => "4",
238+
TypeScript => "5",
239+
Mts => "6",
240+
Cts => "7",
241+
Dts => "8",
242+
Dmts => "9",
243+
Dcts => "10",
244+
Tsx => "11",
245+
Json => "12",
246+
Wasm => "13",
247+
TsBuildInfo => "14",
248+
SourceMap => "15",
249+
Unknown => "16",
250+
}
251+
}
252+
229253
impl deno_graph::ModuleAnalyzer for ParsedSourceCacheModuleAnalyzer {
230254
fn analyze(
231255
&self,

0 commit comments

Comments
 (0)