Skip to content

Commit

Permalink
Change ID generation strategy (#69200)
Browse files Browse the repository at this point in the history
Previously, IDs were generated by hashing and taking the 4 least significant bits, and iteratively taking 4 more bits into account until an available ID was found. Now, the same is done but with digits, i.e. take the first digit of the hash, then first 2, then first 3, etc until an available ID is found.
  • Loading branch information
LichuAcu committed Aug 27, 2024
1 parent f133f28 commit 59183b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,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) {
if power >= u64::MAX / 10 {
// We don't want to take the next power as it would overflow
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(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down

0 comments on commit 59183b9

Please sign in to comment.