`NetworkGraph`: One pre-allocate memory on mainnet
What changed, and why it matters
This commit is a memory-use optimization, not a security fix. It changes the Lightning network graph so that it only pre-allocates large hash-map capacity when running on the real Bitcoin mainnet. On test networks, it starts with zero pre-allocated capacity, reducing memory consumption during tests. There is no security-relevant behavior change.
No security action needed. Treat as a normal performance/memory improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff modifies NetworkGraph::new in lightning/src/routing/gossip.rs. Previously, IndexedMap storage for channels and nodes was pre-sized using CHAN_COUNT_ESTIMATE and NODE_COUNT_ESTIMATE regardless of the Network variant. The patch now uses those estimates only when network == Network::Bitcoin; otherwise it passes (0, 0) as initial capacity. This is a pure resource-usage optimization for non-mainnet networks and does not alter validation, serialization, or consensus logic.
Changed components
lightning/src/routing/gossip.rsNetworkGraph::newInspect captured patch +8 / −2
diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs
index 42eab6d..534bebe 100644
--- a/lightning/src/routing/gossip.rs
+++ b/lightning/src/routing/gossip.rs
@@ -1802,12 +1802,18 @@ where
{
/// Creates a new, empty, network graph.
pub fn new(network: Network, logger: L) -> NetworkGraph<L> {
+ let (node_map_cap, chan_map_cap) = if matches!(network, Network::Bitcoin) {
+ (NODE_COUNT_ESTIMATE, CHAN_COUNT_ESTIMATE)
+ } else {
+ (0, 0)
+ };
+
Self {
secp_ctx: Secp256k1::verification_only(),
chain_hash: ChainHash::using_genesis_block(network),
logger,
- channels: RwLock::new(IndexedMap::with_capacity(CHAN_COUNT_ESTIMATE)),
- nodes: RwLock::new(IndexedMap::with_capacity(NODE_COUNT_ESTIMATE)),
+ channels: RwLock::new(IndexedMap::with_capacity(chan_map_cap)),
+ nodes: RwLock::new(IndexedMap::with_capacity(node_map_cap)),
next_node_counter: AtomicUsize::new(0),
removed_node_counters: Mutex::new(Vec::new()),
last_rapid_gossip_sync_timestamp: Mutex::new(None),
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.