From f79c9d40944580afa565b8539ead7fd9928f3f20 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Tue, 16 Feb 2021 21:12:13 +0000 Subject: [PATCH] adds an upper bound on cluster-slots size (#15300) https://github.com/solana-labs/solana/issues/14366#issuecomment-769096305 --- core/src/cluster_slots.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/src/cluster_slots.rs b/core/src/cluster_slots.rs index f016169586..2a93354a0f 100644 --- a/core/src/cluster_slots.rs +++ b/core/src/cluster_slots.rs @@ -9,6 +9,10 @@ use std::{ sync::{Arc, RwLock}, }; +// Limit the size of cluster-slots map in case +// of receiving bogus epoch slots values. +const CLUSTER_SLOTS_TRIM_SIZE: usize = 524_288; // 512K + pub type SlotPubkeys = HashMap; #[derive(Default)] @@ -43,6 +47,12 @@ impl ClusterSlots { { let mut cluster_slots = self.cluster_slots.write().unwrap(); *cluster_slots = cluster_slots.split_off(&(root + 1)); + // Trimming is done at 2x size so that amortized it has a constant + // cost. The slots furthest away from the root are discarded. + if cluster_slots.len() > 2 * CLUSTER_SLOTS_TRIM_SIZE { + let key = *cluster_slots.keys().nth(CLUSTER_SLOTS_TRIM_SIZE).unwrap(); + cluster_slots.split_off(&key); + } } *self.since.write().unwrap() = since; }