fix(tui): allocate graph history lazily
This commit is contained in:
1 file changed
+11
-1
@@ -550,7 +550,7 @@ impl TuiTargetState {
|
|||||||
session: None,
|
session: None,
|
||||||
status: TargetStatus::Opening,
|
status: TargetStatus::Opening,
|
||||||
negotiated: None,
|
negotiated: None,
|
||||||
graph_history: VecDeque::with_capacity(HISTORY_LIMIT),
|
graph_history: VecDeque::new(),
|
||||||
last_sample: None,
|
last_sample: None,
|
||||||
last_warning: None,
|
last_warning: None,
|
||||||
stats: StatsCollector::new(stats_config),
|
stats: StatsCollector::new(stats_config),
|
||||||
@@ -1019,6 +1019,11 @@ pub(super) struct LastSample {
|
|||||||
fn push_bounded<T>(items: &mut VecDeque<T>, item: T, limit: usize) {
|
fn push_bounded<T>(items: &mut VecDeque<T>, item: T, limit: usize) {
|
||||||
if items.len() == limit {
|
if items.len() == limit {
|
||||||
items.pop_front();
|
items.pop_front();
|
||||||
|
} else if items.len() == items.capacity() && items.capacity() >= limit / 2 {
|
||||||
|
// Growth is naturally exponential; once we're within reach of `limit`,
|
||||||
|
// reserve exactly up to it instead of letting the next doubling step
|
||||||
|
// overshoot it for long-running, heavily populated buffers.
|
||||||
|
items.reserve_exact(limit - items.len());
|
||||||
}
|
}
|
||||||
items.push_back(item);
|
items.push_back(item);
|
||||||
}
|
}
|
||||||
@@ -2225,6 +2230,11 @@ mod tests {
|
|||||||
target.stats.snapshot().packets.unique_replies,
|
target.stats.snapshot().packets.unique_replies,
|
||||||
HISTORY_LIMIT as u64 + 3
|
HISTORY_LIMIT as u64 + 3
|
||||||
);
|
);
|
||||||
|
assert!(
|
||||||
|
target.graph_history.capacity() < HISTORY_LIMIT + HISTORY_LIMIT / 10,
|
||||||
|
"expected saturated history capacity to stay close to the limit, got {}",
|
||||||
|
target.graph_history.capacity()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in new issue
Block a user