Skip to content

Commit 8921a2d

Browse files
authored
grpc: use pointers for load_balancing::WeakSubchannel hash/eq operations (#2439)
This is an old change that I found in my client that never got pushed upstream, but is worth having. Upgrading weak to owned before performing these operations is more expensive. Just using the pointer is simpler and quicker and equally valid.
1 parent 64df1fa commit 8921a2d

File tree

1 file changed

+3
-10
lines changed
  • grpc/src/client/load_balancing

1 file changed

+3
-10
lines changed

grpc/src/client/load_balancing/mod.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use std::{
3131
fmt::{Debug, Display},
3232
hash::{Hash, Hasher},
3333
ops::{Add, Sub},
34+
ptr::addr_eq,
3435
sync::{
3536
atomic::{AtomicI64, Ordering::Relaxed},
3637
Arc, Mutex, Weak,
@@ -456,21 +457,13 @@ impl WeakSubchannel {
456457

457458
impl Hash for WeakSubchannel {
458459
fn hash<H: Hasher>(&self, state: &mut H) {
459-
if let Some(strong) = self.upgrade() {
460-
return strong.dyn_hash(&mut Box::new(state as &mut dyn Hasher));
461-
}
462-
panic!("WeakSubchannel is not valid");
460+
(self.0.as_ptr() as *const () as usize).hash(state);
463461
}
464462
}
465463

466464
impl PartialEq for WeakSubchannel {
467465
fn eq(&self, other: &Self) -> bool {
468-
if let Some(strong_self) = self.upgrade() {
469-
if let Some(strong_other) = other.upgrade() {
470-
return strong_self.dyn_eq(&Box::new(&strong_other as &dyn Any));
471-
}
472-
}
473-
false
466+
addr_eq(self.0.as_ptr(), other.0.as_ptr())
474467
}
475468
}
476469

0 commit comments

Comments
 (0)