Skip to content

Commit 39ca7cb

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 429a55a commit 39ca7cb

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
@@ -1802,12 +1802,18 @@ where
18021802
{
18031803
/// Creates a new, empty, network graph.
18041804
pub fn new(network: Network, logger: L) -> NetworkGraph<L> {
1805+
let (node_map_cap, chan_map_cap) = if matches!(network, Network::Bitcoin) {
1806+
(NODE_COUNT_ESTIMATE, CHAN_COUNT_ESTIMATE)
1807+
} else {
1808+
(0, 0)
1809+
};
1810+
18051811
Self {
18061812
secp_ctx: Secp256k1::verification_only(),
18071813
chain_hash: ChainHash::using_genesis_block(network),
18081814
logger,
1809-
channels: RwLock::new(IndexedMap::with_capacity(CHAN_COUNT_ESTIMATE)),
1810-
nodes: RwLock::new(IndexedMap::with_capacity(NODE_COUNT_ESTIMATE)),
1815+
channels: RwLock::new(IndexedMap::with_capacity(chan_map_cap)),
1816+
nodes: RwLock::new(IndexedMap::with_capacity(node_map_cap)),
18111817
next_node_counter: AtomicUsize::new(0),
18121818
removed_node_counters: Mutex::new(Vec::new()),
18131819
last_rapid_gossip_sync_timestamp: Mutex::new(None),

0 commit comments

Comments
 (0)