From f846fdcc0516127060647f9109f5be0da21bbfa8 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Thu, 14 Aug 2025 22:04:48 -0400 Subject: [PATCH] bug: fix reported usage spike at the start on some OSes (#1788) * bug: fix spike at the start on some OSes * add clean * bake things in better I guess * hmmm no nvm * tiny sleep * update changelog --- CHANGELOG.md | 6 +++++- src/collection.rs | 3 +++ src/lib.rs | 27 ++++++++++++++++++--------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca006864..d5924340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,11 @@ That said, these are more guidelines rather than hardset rules, though the proje ### Bug Fixes - [#1776](https://github.com/ClementTsang/bottom/pull/1776): Fix `disk.columns` being incorrectly interpreted as blank. -- [1787](https://github.com/ClementTsang/bottom/pull/1787): Fix issue with battery widget time and small widths. +- [#1787](https://github.com/ClementTsang/bottom/pull/1787): Fix issue with battery widget time and small widths. + +### Other + +- [#1779](https://github.com/ClementTsang/bottom/pull/1779), [#1788](https://github.com/ClementTsang/bottom/pull/1788): Speed up time between startup and displaying data. ## [0.11.0] - 2025-08-05 diff --git a/src/collection.rs b/src/collection.rs index 973d37b0..113b817c 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -335,6 +335,9 @@ impl DataCollector { } } + /// Update and refresh data. + /// + /// TODO: separate refresh steps and update steps pub fn update_data(&mut self) { self.data.collection_time = Instant::now(); diff --git a/src/lib.rs b/src/lib.rs index cc69d391..9b649c61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,6 +53,8 @@ use tui::{Terminal, backend::CrosstermBackend}; use utils::logging::*; use utils::{cancellation_token::CancellationToken, conversion::*}; +use crate::collection::Data; + // Used for heap allocation debugging purposes. // #[global_allocator] // static ALLOC: dhat::Alloc = dhat::Alloc; @@ -222,12 +224,18 @@ fn create_collection_thread( let update_sleep = app_config_fields.update_rate; thread::spawn(move || { - let mut data_state = collection::DataCollector::new(filters); + let mut data_collector = collection::DataCollector::new(filters); - data_state.set_collection(used_widget_set); - data_state.set_use_current_cpu_total(use_current_cpu_total); - data_state.set_unnormalized_cpu(unnormalized_cpu); - data_state.set_show_average_cpu(show_average_cpu); + data_collector.set_collection(used_widget_set); + data_collector.set_use_current_cpu_total(use_current_cpu_total); + data_collector.set_unnormalized_cpu(unnormalized_cpu); + data_collector.set_show_average_cpu(show_average_cpu); + + data_collector.update_data(); + data_collector.data = Data::default(); + + // Tiny sleep I guess? To go between the first update above and the first update in the loop. + std::thread::sleep(Duration::from_millis(5)); loop { // Check once at the very top... don't block though. @@ -241,12 +249,12 @@ fn create_collection_thread( // trace!("Received message in collection thread: {message:?}"); match message { CollectionThreadEvent::Reset => { - data_state.data.cleanup(); + data_collector.data.cleanup(); } } } - data_state.update_data(); + data_collector.update_data(); // Yet another check to bail if needed... do not block! if let Some(is_terminated) = cancellation_token.try_check() { @@ -255,8 +263,9 @@ fn create_collection_thread( } } - let event = BottomEvent::Update(Box::from(data_state.data)); - data_state.data = collection::Data::default(); + let event = BottomEvent::Update(Box::from(data_collector.data)); + data_collector.data = Data::default(); + if sender.send(event).is_err() { break; }