[model-gateway] Optimize HashRing construction to reduce heap allocations (#17575)
This commit is contained in:
@@ -57,11 +57,16 @@ impl HashRing {
|
|||||||
for worker in workers {
|
for worker in workers {
|
||||||
// Create Arc<str> once per worker, share across all virtual nodes
|
// Create Arc<str> once per worker, share across all virtual nodes
|
||||||
let url: Arc<str> = Arc::from(worker.url());
|
let url: Arc<str> = Arc::from(worker.url());
|
||||||
|
let url_bytes = url.as_bytes();
|
||||||
|
|
||||||
// Create multiple virtual nodes per worker
|
// Create multiple virtual nodes per worker
|
||||||
for vnode in 0..VIRTUAL_NODES_PER_WORKER {
|
for vnode in 0..VIRTUAL_NODES_PER_WORKER {
|
||||||
let vnode_key = format!("{}#{}", url, vnode);
|
let mut hasher = blake3::Hasher::new();
|
||||||
let pos = Self::hash_position(&vnode_key);
|
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)));
|
entries.push((pos, Arc::clone(&url)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user