Skip to content

Commit

Permalink
Changed module id map key from AssetIdent to RcStr
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichu Acuña authored and LichuAcu committed Aug 23, 2024
1 parent 15c086c commit c4bd9aa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,11 +31,11 @@ impl ModuleIdStrategy for DevModuleIdStrategy {

#[turbo_tasks::value]
pub struct GlobalModuleIdStrategy {
module_id_map: HashMap<AssetIdent, Vc<ModuleId>>,
module_id_map: HashMap<RcStr, Vc<ModuleId>>,
}

impl GlobalModuleIdStrategy {
pub async fn new(module_id_map: HashMap<AssetIdent, Vc<ModuleId>>) -> Result<Vc<Self>> {
pub async fn new(module_id_map: HashMap<RcStr, Vc<ModuleId>>) -> Result<Vc<Self>> {
Ok(GlobalModuleIdStrategy { module_id_map }.cell())
}
}
Expand All @@ -44,7 +44,8 @@ impl GlobalModuleIdStrategy {
impl ModuleIdStrategy for GlobalModuleIdStrategy {
#[turbo_tasks::function]
async fn get_module_id(self: Vc<Self>, ident: Vc<AssetIdent>) -> Result<Vc<ModuleId>> {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssetIdent, u64>,
modules_idents: HashMap<RcStr, u64>,
}

pub async fn get_children_modules(
Expand Down Expand Up @@ -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())
Expand All @@ -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<Vc<PreprocessedChildrenIdents>>,
) -> Result<HashMap<AssetIdent, Vc<ModuleId>>> {
let mut module_id_map: HashMap<AssetIdent, Vc<ModuleId>> = HashMap::new();
) -> Result<HashMap<RcStr, Vc<ModuleId>>> {
let mut module_id_map: HashMap<RcStr, Vc<ModuleId>> = HashMap::new();
let mut used_ids: HashSet<u64> = HashSet::new();

for prepared_module_ids in prepared_module_ids {
Expand All @@ -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<AssetIdent, Vc<ModuleId>>,
id_map: &mut HashMap<RcStr, Vc<ModuleId>>,
used_ids: &mut HashSet<u64>,
) -> Result<()> {
if id_map.contains_key(&module_ident) {
if id_map.contains_key(&ident_str) {
return Ok(());
}

Expand All @@ -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(())
Expand Down

0 comments on commit c4bd9aa

Please sign in to comment.