Skip to content

Commit

Permalink
simplifies ContactInfo.cache type to [SocketAddr; SOCKET_CACHE_SIZE] (#…
Browse files Browse the repository at this point in the history
…4311)

#4292
simplified ContactInfo::get_socket to just return Option<SocketAddr>
instead of Result<SocketAddr, Error>.

This commit simplifies ContactInfo.cache type to
    [SocketAddr; SOCKET_CACHE_SIZE]
reducing the size of ContactInfo.
  • Loading branch information
behzadnouri authored Jan 7, 2025
1 parent a2e9762 commit 167bad7
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions gossip/src/contact_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use {

pub const SOCKET_ADDR_UNSPECIFIED: SocketAddr =
SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), /*port:*/ 0u16);
const EMPTY_SOCKET_ADDR_CACHE: [SocketAddr; SOCKET_CACHE_SIZE] =
[SOCKET_ADDR_UNSPECIFIED; SOCKET_CACHE_SIZE];

const SOCKET_TAG_GOSSIP: u8 = 0;
const SOCKET_TAG_RPC: u8 = 2;
Expand Down Expand Up @@ -86,7 +88,7 @@ pub struct ContactInfo {
extensions: Vec<Extension>,
// Only sanitized socket-addrs can be cached!
#[serde(skip_serializing)]
cache: [Result<SocketAddr, Error>; SOCKET_CACHE_SIZE],
cache: [SocketAddr; SOCKET_CACHE_SIZE],
}

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
Expand Down Expand Up @@ -125,7 +127,10 @@ macro_rules! get_socket {
($name:ident, $key:ident) => {
#[inline]
pub fn $name(&self) -> Option<SocketAddr> {
self.cache[usize::from($key)].ok()
let socket = &self.cache[usize::from($key)];
(socket != &SOCKET_ADDR_UNSPECIFIED)
.then_some(socket)
.copied()
}
};
($name:ident, $udp:ident, $quic:ident) => {
Expand All @@ -135,7 +140,10 @@ macro_rules! get_socket {
Protocol::QUIC => $quic,
Protocol::UDP => $udp,
};
self.cache[usize::from(key)].ok()
let socket = &self.cache[usize::from(key)];
(socket != &SOCKET_ADDR_UNSPECIFIED)
.then_some(socket)
.copied()
}
};
}
Expand Down Expand Up @@ -187,7 +195,7 @@ impl ContactInfo {
addrs: Vec::<IpAddr>::default(),
sockets: Vec::<SocketEntry>::default(),
extensions: Vec::<Extension>::default(),
cache: new_empty_cache(),
cache: EMPTY_SOCKET_ADDR_CACHE,
}
}

Expand Down Expand Up @@ -334,7 +342,7 @@ impl ContactInfo {
}
}
if let Some(entry) = self.cache.get_mut(usize::from(key)) {
*entry = Ok(socket); // socket is already sanitized above.
*entry = socket; // socket is already sanitized above.
}
debug_assert_matches!(sanitize_entries(&self.addrs, &self.sockets), Ok(()));
Ok(())
Expand All @@ -349,7 +357,7 @@ impl ContactInfo {
}
self.maybe_remove_addr(entry.index);
if let Some(entry) = self.cache.get_mut(usize::from(key)) {
*entry = Err(Error::SocketNotFound(key));
*entry = SOCKET_ADDR_UNSPECIFIED;
}
}
}
Expand Down Expand Up @@ -463,11 +471,6 @@ impl ContactInfo {
}
}

fn new_empty_cache() -> [Result<SocketAddr, Error>; SOCKET_CACHE_SIZE] {
debug_assert!(SOCKET_CACHE_SIZE < usize::from(u8::MAX));
std::array::from_fn(|key| Err(Error::SocketNotFound(key as u8)))
}

fn get_node_outset() -> u64 {
let now = SystemTime::now();
let elapsed = now.duration_since(UNIX_EPOCH).unwrap();
Expand Down Expand Up @@ -518,7 +521,7 @@ impl TryFrom<ContactInfoLite> for ContactInfo {
addrs,
sockets,
extensions,
cache: new_empty_cache(),
cache: EMPTY_SOCKET_ADDR_CACHE,
};
// Populate node.cache.
// Only sanitized socket-addrs can be cached!
Expand All @@ -532,7 +535,9 @@ impl TryFrom<ContactInfoLite> for ContactInfo {
continue;
};
let socket = SocketAddr::new(addr, port);
*entry = sanitize_socket(&socket).map(|()| socket);
if sanitize_socket(&socket).is_ok() {
*entry = socket;
}
}
Ok(node)
}
Expand Down Expand Up @@ -642,7 +647,7 @@ impl solana_frozen_abi::abi_example::AbiExample for ContactInfo {
addrs: Vec::<IpAddr>::example(),
sockets: Vec::<SocketEntry>::example(),
extensions: vec![],
cache: new_empty_cache(),
cache: EMPTY_SOCKET_ADDR_CACHE,
}
}
}
Expand Down Expand Up @@ -807,7 +812,7 @@ mod tests {
addrs: Vec::default(),
sockets: Vec::default(),
extensions: Vec::default(),
cache: new_empty_cache(),
cache: EMPTY_SOCKET_ADDR_CACHE,
};
let mut sockets = HashMap::<u8, SocketAddr>::new();
for _ in 0..1 << 14 {
Expand All @@ -827,10 +832,10 @@ mod tests {
if usize::from(key) < SOCKET_CACHE_SIZE {
assert_eq!(
node.cache[usize::from(key)],
match socket {
None => Err(Error::SocketNotFound(key)),
Some(&socket) => sanitize_socket(&socket).map(|()| socket),
}
socket
.filter(|socket| sanitize_socket(socket).is_ok())
.copied()
.unwrap_or(SOCKET_ADDR_UNSPECIFIED),
);
}
}
Expand Down

0 comments on commit 167bad7

Please sign in to comment.