Skip to content

Commit 9138bb2

Browse files
committed
NetworkGraph: One pre-allocate memory on mainnet
Previously, we'd always pre-allocate memory for the node and channel maps based on mainnet numbers, even if we're on another network like `Regest`. Here, we only apply the estimates if we're actually on `Network::Bitcoin`, which should reduce the `NetworkGraph`'s memory footprint considerably in tests.
1 parent 313f69c commit 9138bb2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lightning/src/routing/gossip.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,12 +1805,18 @@ where
18051805
{
18061806
/// Creates a new, empty, network graph.
18071807
pub fn new(network: Network, logger: L) -> NetworkGraph<L> {
1808+
let (node_map_cap, chan_map_cap) = if matches!(network, Network::Bitcoin) {
1809+
(NODE_COUNT_ESTIMATE, CHAN_COUNT_ESTIMATE)
1810+
} else {
1811+
(0, 0)
1812+
};
1813+
18081814
Self {
18091815
secp_ctx: Secp256k1::verification_only(),
18101816
chain_hash: ChainHash::using_genesis_block(network),
18111817
logger,
1812-
channels: RwLock::new(IndexedMap::with_capacity(CHAN_COUNT_ESTIMATE)),
1813-
nodes: RwLock::new(IndexedMap::with_capacity(NODE_COUNT_ESTIMATE)),
1818+
channels: RwLock::new(IndexedMap::with_capacity(chan_map_cap)),
1819+
nodes: RwLock::new(IndexedMap::with_capacity(node_map_cap)),
18141820
next_node_counter: AtomicUsize::new(0),
18151821
removed_node_counters: Mutex::new(Vec::new()),
18161822
last_rapid_gossip_sync_timestamp: Mutex::new(None),

0 commit comments

Comments
 (0)