From c4bd9aa25ec626e4e8b9af1308991819b35e8a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lichu=20Acu=C3=B1a?= Date: Mon, 19 Aug 2024 15:38:17 -0700 Subject: [PATCH] Changed module id map key from AssetIdent to RcStr --- .../src/chunk/module_id_strategies.rs | 9 +++---- .../src/global_module_id_strategy.rs | 24 +++++++++---------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/turbopack/crates/turbopack-core/src/chunk/module_id_strategies.rs b/turbopack/crates/turbopack-core/src/chunk/module_id_strategies.rs index 3b03ed9e38a50..f3368bad3eac7 100644 --- a/turbopack/crates/turbopack-core/src/chunk/module_id_strategies.rs +++ b/turbopack/crates/turbopack-core/src/chunk/module_id_strategies.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use anyhow::Result; -use turbo_tasks::{ValueToString, Vc}; +use turbo_tasks::{RcStr, ValueToString, Vc}; use turbo_tasks_hash::hash_xxh3_hash64; use super::ModuleId; @@ -31,11 +31,11 @@ impl ModuleIdStrategy for DevModuleIdStrategy { #[turbo_tasks::value] pub struct GlobalModuleIdStrategy { - module_id_map: HashMap>, + module_id_map: HashMap>, } impl GlobalModuleIdStrategy { - pub async fn new(module_id_map: HashMap>) -> Result> { + pub async fn new(module_id_map: HashMap>) -> Result> { Ok(GlobalModuleIdStrategy { module_id_map }.cell()) } } @@ -44,7 +44,8 @@ impl GlobalModuleIdStrategy { impl ModuleIdStrategy for GlobalModuleIdStrategy { #[turbo_tasks::function] async fn get_module_id(self: Vc, ident: Vc) -> Result> { - if let Some(module_id) = self.await?.module_id_map.get(&ident.await?.clone_value()) { + let ident_string = ident.to_string().await?.clone_value(); + if let Some(module_id) = self.await?.module_id_map.get(&ident_string) { // dbg!(format!("Hit {}", ident.to_string().await?)); return Ok(*module_id); } diff --git a/turbopack/crates/turbopack-ecmascript/src/global_module_id_strategy.rs b/turbopack/crates/turbopack-ecmascript/src/global_module_id_strategy.rs index f373b64b733fe..20596165ca9b1 100644 --- a/turbopack/crates/turbopack-ecmascript/src/global_module_id_strategy.rs +++ b/turbopack/crates/turbopack-ecmascript/src/global_module_id_strategy.rs @@ -3,22 +3,21 @@ use std::collections::{HashMap, HashSet}; use anyhow::Result; use turbo_tasks::{ graph::{AdjacencyMap, GraphTraversal}, - ValueToString, Vc, + RcStr, ValueToString, Vc, }; use turbo_tasks_hash::hash_xxh3_hash64; use turbopack_core::{ chunk::ModuleId, - ident::AssetIdent, module::{Module, Modules}, reference::primary_referenced_modules, }; #[turbo_tasks::value] pub struct PreprocessedChildrenIdents { - // module_id -> full hash + // ident.to_string() -> full hash // We save the full hash to avoid re-hashing in `merge_preprocessed_module_ids` // if this endpoint did not change. - modules_idents: HashMap, + modules_idents: HashMap, } pub async fn get_children_modules( @@ -50,8 +49,9 @@ pub async fn children_modules_idents( for module in children_modules_iter { let module_ident = module.ident(); - let hash = hash_xxh3_hash64(module_ident.to_string().await?); - modules_idents.insert(module_ident.await?.clone_value(), hash); + let ident_str = module_ident.to_string().await?.clone_value(); + let hash = hash_xxh3_hash64(&ident_str); + modules_idents.insert(ident_str, hash); } Ok(PreprocessedChildrenIdents { modules_idents }.cell()) @@ -61,8 +61,8 @@ pub async fn children_modules_idents( // ids and another that generates the final, optimized module ids. Thoughts? pub async fn merge_preprocessed_module_ids( prepared_module_ids: Vec>, -) -> Result>> { - let mut module_id_map: HashMap> = HashMap::new(); +) -> Result>> { + let mut module_id_map: HashMap> = HashMap::new(); let mut used_ids: HashSet = HashSet::new(); for prepared_module_ids in prepared_module_ids { @@ -81,12 +81,12 @@ pub async fn merge_preprocessed_module_ids( } pub async fn process_module( - module_ident: AssetIdent, + ident_str: RcStr, full_hash: u64, - id_map: &mut HashMap>, + id_map: &mut HashMap>, used_ids: &mut HashSet, ) -> Result<()> { - if id_map.contains_key(&module_ident) { + if id_map.contains_key(&ident_str) { return Ok(()); } @@ -102,7 +102,7 @@ pub async fn process_module( let hashed_module_id = ModuleId::String(masked_hash.to_string().into()); - id_map.insert(module_ident, hashed_module_id.cell()); + id_map.insert(ident_str, hashed_module_id.cell()); used_ids.insert(masked_hash); Ok(())