[model-gateway] Optimize HashRing construction to reduce heap allocations (#17575)
This commit is contained in:
committed by
GitHub
parent
897c35b457
commit
6f009961bb
@@ -57,11 +57,16 @@ impl HashRing {
|
||||
for worker in workers {
|
||||
// Create Arc<str> once per worker, share across all virtual nodes
|
||||
let url: Arc<str> = Arc::from(worker.url());
|
||||
let url_bytes = url.as_bytes();
|
||||
|
||||
// Create multiple virtual nodes per worker
|
||||
for vnode in 0..VIRTUAL_NODES_PER_WORKER {
|
||||
let vnode_key = format!("{}#{}", url, vnode);
|
||||
let pos = Self::hash_position(&vnode_key);
|
||||
let mut hasher = blake3::Hasher::new();
|
||||
hasher.update(url_bytes);
|
||||
hasher.update(b"#");
|
||||
hasher.update(&(vnode as u64).to_le_bytes());
|
||||
let hash = hasher.finalize();
|
||||
let pos = u64::from_le_bytes(hash.as_bytes()[..8].try_into().unwrap());
|
||||
entries.push((pos, Arc::clone(&url)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user