From 4a1cbb0ce578458169c74fb6edb10fbdb9282f5c Mon Sep 17 00:00:00 2001 From: LichuAcu Date: Thu, 22 Aug 2024 11:30:00 -0700 Subject: [PATCH] Change ID generation strategy --- .../src/global_module_id_strategy.rs | 23 +++++++++++-------- .../src/references/external_module.rs | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) 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 e6c1f82c827a32..2404abfda8681b 100644 --- a/turbopack/crates/turbopack-ecmascript/src/global_module_id_strategy.rs +++ b/turbopack/crates/turbopack-ecmascript/src/global_module_id_strategy.rs @@ -191,20 +191,25 @@ pub async fn process_module( return Ok(()); } - let mut masked_hash = full_hash & 0xF; - let mut mask = 0xF; - while used_ids.contains(&masked_hash) { - if mask == 0xFFFFFFFFFFFFFFFF { - return Err(anyhow::anyhow!("This is a... 64-bit hash collision?")); + let mut trimmed_hash = full_hash % 10; + let mut power = 10; + while used_ids.contains(&trimmed_hash) { + // TODO(LichuAcu): Fix this hack with a constant. + if power >= std::u64::MAX / 10 { + if used_ids.contains(&full_hash) { + return Err(anyhow::anyhow!("This is a... 64-bit hash collision?")); + } + trimmed_hash = full_hash; + break; } - mask = (mask << 4) | 0xF; - masked_hash = full_hash & mask; + power *= 10; + trimmed_hash = full_hash % power; } - let hashed_module_id = ModuleId::String(masked_hash.to_string().into()); + let hashed_module_id = ModuleId::String(trimmed_hash.to_string().into()); id_map.insert(ident_str, hashed_module_id.cell()); - used_ids.insert(masked_hash); + used_ids.insert(trimmed_hash); Ok(()) } diff --git a/turbopack/crates/turbopack-ecmascript/src/references/external_module.rs b/turbopack/crates/turbopack-ecmascript/src/references/external_module.rs index 89211f9ba94cda..65e16b175fbce9 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/external_module.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/external_module.rs @@ -245,7 +245,7 @@ impl EcmascriptChunkItem for CachedExternalModuleChunkItem { } } -/// A module that only has an ident and no content. This is used +/// A module that only has an ident and no content nor references. It is used /// to include a module's ident in the module graph before the module /// itself is resolved, as is the case with NextServerComponentModule's /// "client modules" and "client modules ssr".