Remove ticking

We don't use it and it wastes CPU to cancel and re-create most futures on every tick
This commit is contained in:
Val Lorentz 2023-12-17 14:22:01 +01:00
parent b3ab8d734f
commit 143f144e2c
6 changed files with 5 additions and 68 deletions

View File

@ -7,7 +7,6 @@ use serde::{
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Action {
Tick,
/// Apply any pending update to the screen
Render,
/// Notify there is a pending update
@ -46,7 +45,6 @@ impl<'de> Deserialize<'de> for Action {
E: de::Error,
{
match value {
"Tick" => Ok(Action::Tick),
"Render" => Ok(Action::Render),
"Suspend" => Ok(Action::Suspend),
"Resume" => Ok(Action::Resume),

View File

@ -42,7 +42,6 @@ pub struct App {
pub config: Arc<Config>,
pub clients: NonEmpty<matrix_sdk::Client>,
pub commands: RataCommands,
pub tick_rate: f64,
pub frame_rate: f64,
pub components: Vec<Box<dyn Component>>,
pub buffers: Buffers,
@ -53,11 +52,7 @@ pub struct App {
}
impl App {
pub async fn new(
tick_rate: f64,
frame_rate: f64,
log_receiver: mpsc::UnboundedReceiver<String>,
) -> Result<Self> {
pub async fn new(frame_rate: f64, log_receiver: mpsc::UnboundedReceiver<String>) -> Result<Self> {
let config = Config::new()?;
let datadir = config.config._data_dir.join("default");
let future_clients = config.accounts.clone().map(|conf| {
@ -126,7 +121,6 @@ impl App {
let mode = Mode::Home;
let mut app = Self {
tick_rate,
frame_rate,
components: vec![Box::new(home), Box::new(fps)],
should_quit: Arc::new(AtomicBool::new(false)),
@ -147,7 +141,6 @@ impl App {
let (sync_responses_tx, mut sync_responses_rx) = mpsc::unbounded_channel();
let mut tui = tui::Tui::new()?
.tick_rate(self.tick_rate)
.frame_rate(self.frame_rate)
.mouse(self.config.mouse.enable);
tui.enter()?;
@ -232,13 +225,10 @@ impl App {
}
while let Ok(action) = action_rx.try_recv() {
if action != Action::Tick && action != Action::Render {
if action != Action::Render {
log::debug!("{action:?}");
}
match action {
Action::Tick => {
self.last_tick_key_events.borrow_mut().drain(..);
},
Action::Quit => self.should_quit.store(true, Ordering::Release),
Action::Suspend => self.should_suspend = true,
Action::Resume => self.should_suspend = false,
@ -310,7 +300,6 @@ impl App {
tui.suspend()?;
action_tx.send(Action::Resume)?;
tui = tui::Tui::new()?
.tick_rate(self.tick_rate)
.frame_rate(self.frame_rate)
.mouse(self.config.mouse.enable);
tui.enter()?;
@ -330,7 +319,6 @@ impl App {
) -> Result<()> {
match e {
tui::Event::Quit => action_tx.send(Action::Quit)?,
tui::Event::Tick => action_tx.send(Action::Tick)?,
tui::Event::Render => action_tx.send(Action::Render)?,
tui::Event::Resize(x, y) => action_tx.send(Action::Resize(x, y))?,
tui::Event::Mouse(event) => {

View File

@ -7,15 +7,6 @@ use crate::utils::version;
#[derive(Parser, Debug)]
#[command(author, version = version(), about)]
pub struct Cli {
#[arg(
short,
long,
value_name = "FLOAT",
help = "Tick rate, i.e. number of ticks per second",
default_value_t = 1.0
)]
pub tick_rate: f64,
#[arg(
short,
long,

View File

@ -8,10 +8,6 @@ use crate::action::Action;
#[derive(Debug, Clone, PartialEq)]
pub struct FpsCounter {
app_start_time: Instant,
app_frames: u32,
app_fps: f64,
render_start_time: Instant,
render_frames: u32,
render_fps: f64,
@ -26,27 +22,12 @@ impl Default for FpsCounter {
impl FpsCounter {
pub fn new() -> Self {
Self {
app_start_time: Instant::now(),
app_frames: 0,
app_fps: 0.0,
render_start_time: Instant::now(),
render_frames: 0,
render_fps: 0.0,
}
}
fn app_tick(&mut self) -> Result<()> {
self.app_frames += 1;
let now = Instant::now();
let elapsed = (now - self.app_start_time).as_secs_f64();
if elapsed >= 1.0 {
self.app_fps = self.app_frames as f64 / elapsed;
self.app_start_time = now;
self.app_frames = 0;
}
Ok(())
}
fn render_tick(&mut self) -> Result<()> {
self.render_frames += 1;
let now = Instant::now();
@ -62,9 +43,6 @@ impl FpsCounter {
impl Component for FpsCounter {
fn update(&mut self, action: &Action) -> Result<Option<Action>> {
if let Action::Tick = action {
self.app_tick()?
};
if let Action::Render = action {
self.render_tick()?
};
@ -87,10 +65,7 @@ impl Component for FpsCounter {
let rect = rects[0];
let s = format!(
"{:.2} ticks per sec (app) {:.2} frames per sec (render)",
self.app_fps, self.render_fps
);
let s = format!("{:.2} frames per sec (render)", self.render_fps);
let block = Block::default().title(block::Title::from(s.dim()).alignment(Alignment::Right));
f.render_widget(block, rect);
Ok(())

View File

@ -9,7 +9,7 @@ async fn tokio_main() -> Result<()> {
initialize_panic_handler()?;
let args = Cli::parse();
let mut app = App::new(args.tick_rate, args.frame_rate, mem_log).await?;
let mut app = App::new(args.frame_rate, mem_log).await?;
app.run().await?;
Ok(())

View File

@ -32,7 +32,6 @@ pub enum Event {
Quit,
Error,
Closed,
Tick,
Render,
FocusGained,
FocusLost,
@ -49,14 +48,12 @@ pub struct Tui {
pub event_rx: UnboundedReceiver<Event>,
pub event_tx: UnboundedSender<Event>,
pub frame_rate: f64,
pub tick_rate: f64,
pub mouse: bool,
pub paste: bool,
}
impl Tui {
pub fn new() -> Result<Self> {
let tick_rate = 4.0;
let frame_rate = 60.0;
let terminal = ratatui::Terminal::new(Backend::new(io()))?;
let (event_tx, event_rx) = mpsc::unbounded_channel();
@ -71,17 +68,11 @@ impl Tui {
event_rx,
event_tx,
frame_rate,
tick_rate,
mouse,
paste,
})
}
pub fn tick_rate(mut self, tick_rate: f64) -> Self {
self.tick_rate = tick_rate;
self
}
pub fn frame_rate(mut self, frame_rate: f64) -> Self {
self.frame_rate = frame_rate;
self
@ -98,7 +89,6 @@ impl Tui {
}
pub fn start(&mut self) {
let tick_delay = std::time::Duration::from_secs_f64(1.0 / self.tick_rate);
let render_delay = std::time::Duration::from_secs_f64(1.0 / self.frame_rate);
self.cancel();
self.cancellation_token = CancellationToken::new();
@ -106,12 +96,10 @@ impl Tui {
let _event_tx = self.event_tx.clone();
self.task = tokio::spawn(async move {
let mut reader = crossterm::event::EventStream::new();
let mut tick_interval = tokio::time::interval(tick_delay);
let mut render_interval = tokio::time::interval(render_delay);
render_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
render_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
_event_tx.send(Event::Init).unwrap();
loop {
let tick_delay = tick_interval.tick();
let render_delay = render_interval.tick();
let crossterm_event = reader.next().fuse();
tokio::select! {
@ -150,9 +138,6 @@ impl Tui {
None => {},
}
},
_ = tick_delay => {
_event_tx.send(Event::Tick).unwrap();
},
_ = render_delay => {
_event_tx.send(Event::Render).unwrap();
},