Skip to content

Commit 521cdd5

Browse files
jxsdapplion
authored andcommitted
avoid double iteration by using lifetimes
1 parent 2a7f0f8 commit 521cdd5

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

beacon_node/lighthouse_network/src/service/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl<E: EthSpec> Network<E> {
711711
for kind in fork_core_topics::<E>(
712712
&new_fork,
713713
&self.fork_context.spec,
714-
&self.network_globals.topic_config(),
714+
&self.network_globals.as_topic_config(),
715715
) {
716716
let topic = GossipTopic::new(kind, GossipEncoding::default(), new_fork_digest);
717717
self.subscribe(topic);

beacon_node/lighthouse_network/src/types/globals.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ impl<E: EthSpec> NetworkGlobals<E> {
185185
}
186186

187187
/// Returns the TopicConfig to compute the set of Gossip topics for a given fork
188-
pub fn topic_config(&self) -> TopicConfig {
188+
pub fn as_topic_config(&self) -> TopicConfig {
189189
TopicConfig {
190190
subscribe_all_data_column_subnets: self.config.subscribe_all_data_column_subnets,
191-
sampling_subnets: self.sampling_subnets.iter().copied().collect(),
191+
sampling_subnets: &self.sampling_subnets,
192192
}
193193
}
194194

beacon_node/lighthouse_network/src/types/topics.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use gossipsub::{IdentTopic as Topic, TopicHash};
22
use serde::{Deserialize, Serialize};
3+
use std::collections::HashSet;
34
use strum::AsRefStr;
45
use types::{ChainSpec, DataColumnSubnetId, EthSpec, ForkName, SubnetId, SyncSubnetId, Unsigned};
56

@@ -41,9 +42,10 @@ pub const LIGHT_CLIENT_GOSSIP_TOPICS: [GossipKind; 2] = [
4142
GossipKind::LightClientOptimisticUpdate,
4243
];
4344

44-
pub struct TopicConfig {
45+
#[derive(Debug)]
46+
pub struct TopicConfig<'a> {
4547
pub subscribe_all_data_column_subnets: bool,
46-
pub sampling_subnets: Vec<DataColumnSubnetId>,
48+
pub sampling_subnets: &'a HashSet<DataColumnSubnetId>,
4749
}
4850

4951
/// Returns the core topics associated with each fork that are new to the previous fork
@@ -82,7 +84,7 @@ pub fn fork_core_topics<E: EthSpec>(
8284
)));
8385
}
8486
} else {
85-
for column_subnet in &topic_config.sampling_subnets {
87+
for column_subnet in topic_config.sampling_subnets {
8688
topics.push(GossipKind::DataColumnSidecar(*column_subnet));
8789
}
8890
}
@@ -499,9 +501,10 @@ mod tests {
499501
type E = MainnetEthSpec;
500502
let spec = E::default_spec();
501503
let mut all_topics = Vec::new();
504+
let sampling_subnets = HashSet::new();
502505
let topic_config = TopicConfig {
503506
subscribe_all_data_column_subnets: false,
504-
sampling_subnets: vec![],
507+
sampling_subnets: &sampling_subnets,
505508
};
506509
let mut electra_core_topics =
507510
fork_core_topics::<E>(&ForkName::Electra, &spec, &topic_config);

beacon_node/network/src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ impl<T: BeaconChainTypes> NetworkService<T> {
714714
for topic_kind in core_topics_to_subscribe::<T::EthSpec>(
715715
self.fork_context.current_fork(),
716716
&self.fork_context.spec,
717-
&self.network_globals.topic_config(),
717+
&self.network_globals.as_topic_config(),
718718
) {
719719
for fork_digest in self.required_gossip_fork_digests() {
720720
let topic = GossipTopic::new(
@@ -910,7 +910,7 @@ impl<T: BeaconChainTypes> NetworkService<T> {
910910
let core_topics = core_topics_to_subscribe::<T::EthSpec>(
911911
self.fork_context.current_fork(),
912912
&self.fork_context.spec,
913-
&self.network_globals.topic_config(),
913+
&self.network_globals.as_topic_config(),
914914
);
915915
let core_topics: HashSet<&GossipKind> = HashSet::from_iter(&core_topics);
916916
let subscriptions = self.network_globals.gossipsub_subscriptions.read();

0 commit comments

Comments
 (0)