diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-02-09 09:17:48 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-09 09:17:48 -0500 |
commit | 717daf47482d370ed2bd868a36a33de93f1cf8b6 (patch) | |
tree | 8f86d8f9fcc7957d2029a8c02d35b5cfbc540180 /cli/cache/parsed_source.rs | |
parent | 8b0a612e308d593205690729e23dafe0bce02eb9 (diff) |
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).
Diffstat (limited to 'cli/cache/parsed_source.rs')
-rw-r--r-- | cli/cache/parsed_source.rs | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/cli/cache/parsed_source.rs b/cli/cache/parsed_source.rs index 6385e7384..fc1b54ddb 100644 --- a/cli/cache/parsed_source.rs +++ b/cli/cache/parsed_source.rs @@ -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, |