diff --git a/src/boot_model.rs b/src/boot_model.rs index 6f92761..77ce82a 100644 --- a/src/boot_model.rs +++ b/src/boot_model.rs @@ -1,5 +1,6 @@ mod boot_change; pub mod boot_origin; +mod boot_projection; mod job_ingestion; mod reached_milestones; mod unit_facts; @@ -9,6 +10,7 @@ use std::{collections::HashMap, time::Duration}; pub use self::{ boot_change::BootChange, + boot_projection::BootProjection, reached_milestones::MilestoneRow, unit_facts::{ActiveState, Activity, JobKind, JobResult, UnitFacts, UnitKind}, waiting_tree::{RowStatus, TreeNode, TreeRow, flatten_forest}, diff --git a/src/boot_model/boot_projection.rs b/src/boot_model/boot_projection.rs new file mode 100644 index 0000000..708989e --- /dev/null +++ b/src/boot_model/boot_projection.rs @@ -0,0 +1,21 @@ +use std::time::Duration; + +use super::{BootState, MilestoneRow, Tally, TreeNode}; + +#[derive(Clone, Debug)] +pub struct BootProjection { + pub forest: Vec, + pub milestones: Vec, + pub tally: Tally, +} + +impl BootState { + #[must_use] + pub fn project(&self, finished_since: Duration) -> BootProjection { + BootProjection { + forest: self.waiting_forest(finished_since), + milestones: self.milestones(), + tally: self.tally(), + } + } +} diff --git a/src/boot_view.rs b/src/boot_view.rs index c3c0a53..a7c7f47 100644 --- a/src/boot_view.rs +++ b/src/boot_view.rs @@ -35,7 +35,8 @@ use self::{ }; use crate::{ boot_model::{ - BootState, RowStatus, TreeRow, UnitKind, boot_origin::BootOrigin, flatten_forest, + BootProjection, BootState, RowStatus, TreeRow, UnitKind, boot_origin::BootOrigin, + flatten_forest, }, screen_settings::{color_scheme::Colors, config_file::Timing, font_glyphs::Glyphs}, }; @@ -58,6 +59,7 @@ const ELAPSED: SpanPrinter = SpanPrinter::new() pub struct BootView<'a> { pub state: &'a BootState, + pub projection: &'a BootProjection, pub colors: &'a Colors, pub timing: &'a Timing, pub glyphs: &'a Glyphs, @@ -90,7 +92,7 @@ impl BootView<'_> { .iter() .map(|stage| stage.reached_at) .collect(), - milestones: self.state.milestones(), + milestones: &self.projection.milestones, finished: self .state .finished_runs @@ -109,9 +111,7 @@ impl BootView<'_> { impl Widget for BootView<'_> { fn render(self, area: Rect, buf: &mut Buffer) { - let forest = self - .state - .waiting_forest(self.now.saturating_sub(self.timing.keep_finished_units_for)); + let forest = &self.projection.forest; let all_rows = flatten_forest(forest.clone()); let pending_count = all_rows.len(); let header = Header { @@ -128,7 +128,7 @@ impl Widget for BootView<'_> { }; let timeline = self.timeline(&all_rows); let footer = Footer { - tally: self.state.tally(), + tally: self.projection.tally, colors: &self.colors.footer, glyphs: self.glyphs, }; @@ -143,7 +143,7 @@ impl Widget for BootView<'_> { if fits_timeline { timeline.height() } else { 0 }, ); let budget = usize::from(heights.tree.saturating_sub(1)); - let rows = rows_within_height(&forest, budget); + let rows = rows_within_height(forest, budget); let tree = UnitTree { phase: stages.current_phase(), rows: &rows, @@ -210,6 +210,11 @@ impl Heights { } } +#[must_use] +pub fn project_boot(state: &BootState, timing: &Timing, now: Duration) -> BootProjection { + state.project(now.saturating_sub(timing.keep_finished_units_for)) +} + fn rule(color: Color, left: Line<'static>) -> Block<'static> { Block::new() .borders(Borders::TOP) diff --git a/src/boot_view/frame_cost_bench.rs b/src/boot_view/frame_cost_bench.rs index 5f16953..edc864d 100644 --- a/src/boot_view/frame_cost_bench.rs +++ b/src/boot_view/frame_cost_bench.rs @@ -6,9 +6,9 @@ use std::{ use ratatui::{Terminal, backend::TestBackend}; -use super::{BootView, screen_render_fixtures::ryzen_origin}; +use super::{BootView, project_boot, screen_render_fixtures::ryzen_origin}; use crate::{ - boot_model::{BootState, JobResult, Phase, flatten_forest}, + boot_model::{BootProjection, BootState, JobResult, Phase, flatten_forest}, demo_script::boot_test_fixtures::{MS, QueuedUnit, queued_boot, running, waiting}, screen_settings::{color_scheme::Colors, config_file::Timing, font_glyphs::Glyphs}, }; @@ -63,29 +63,50 @@ fn report_timed_draws(label: &str, mut work: impl FnMut(Duration)) { println!("{label} over {DRAW_COUNT} draws: median {median:?}, p90 {slowest_tenth:?}"); } +fn draw_boot_frame( + terminal: &mut Terminal, + state: &BootState, + projection: &BootProjection, + now: Duration, +) { + terminal + .draw(|frame| { + frame.render_widget( + BootView { + state, + projection, + colors: &Colors::default(), + timing: &Timing::default(), + glyphs: &Glyphs::default(), + now, + origin: &ryzen_origin(), + password_prompt: None, + }, + frame.area(), + ); + }) + .expect("draw"); +} + #[test] #[ignore = "benchmark: cargo test --release frame_cost -- --ignored --nocapture"] fn frame_cost_of_a_crowded_boot() { let state = crowded_boot(); - let origin = ryzen_origin(); let mut terminal = Terminal::new(TestBackend::new(110, 40)).expect("test backend"); report_timed_draws("whole frame", |now| { - terminal - .draw(|frame| { - frame.render_widget( - BootView { - state: &state, - colors: &Colors::default(), - timing: &Timing::default(), - glyphs: &Glyphs::default(), - now, - origin: &origin, - password_prompt: None, - }, - frame.area(), - ); - }) - .expect("draw"); + let projection = project_boot(&state, &Timing::default(), now); + draw_boot_frame(&mut terminal, &state, &projection, now); + }); +} + +#[test] +#[ignore = "benchmark: cargo test --release frame_cost -- --ignored --nocapture"] +fn frame_cost_with_a_cached_projection() { + let state = crowded_boot(); + let projection = project_boot(&state, &Timing::default(), Duration::from_secs(90)); + let mut terminal = Terminal::new(TestBackend::new(110, 40)).expect("test backend"); + report_timed_draws("cached frame", |now| { + draw_boot_frame(&mut terminal, &state, &projection, now); }); } diff --git a/src/boot_view/run_timeline.rs b/src/boot_view/run_timeline.rs index 058ff9d..2f768cc 100644 --- a/src/boot_view/run_timeline.rs +++ b/src/boot_view/run_timeline.rs @@ -47,7 +47,7 @@ pub struct Timeline<'a> { pub origin: Duration, pub now: Duration, pub root_switches: Vec, - pub milestones: Vec, + pub milestones: &'a [MilestoneRow], pub finished: Vec<&'a FinishedRun>, pub running: Vec>, pub colors: &'a TimelineColors, diff --git a/src/boot_view/screen_render_fixtures.rs b/src/boot_view/screen_render_fixtures.rs index 01470ac..5084272 100644 --- a/src/boot_view/screen_render_fixtures.rs +++ b/src/boot_view/screen_render_fixtures.rs @@ -2,7 +2,7 @@ use std::time::Duration; use ratatui::{Terminal, backend::TestBackend, buffer::Buffer}; -use super::{BootView, PasswordPrompt}; +use super::{BootView, PasswordPrompt, project_boot}; use crate::{ boot_model::{BootState, JobResult, Phase, boot_origin::BootOrigin}, demo_script::boot_test_fixtures::{MS, queued_boot, running, waiting}, @@ -21,13 +21,16 @@ pub fn render_prompted( password_prompt: Option, ) -> Buffer { let mut terminal = Terminal::new(TestBackend::new(width, height)).expect("test backend"); + let timing = Timing::default(); + let projection = project_boot(state, &timing, now); terminal .draw(|frame| { frame.render_widget( BootView { state, + projection: &projection, colors: &Colors::default(), - timing: &Timing::default(), + timing: &timing, glyphs: &Glyphs::default(), now, origin: &ryzen_origin(), diff --git a/src/console_session/watch_screen.rs b/src/console_session/watch_screen.rs index bd4e55a..05eb2f2 100644 --- a/src/console_session/watch_screen.rs +++ b/src/console_session/watch_screen.rs @@ -10,13 +10,14 @@ use ratatui::{ }; use crate::{ - boot_model::{BootChange, BootState, boot_origin::BootOrigin}, - boot_view::{BootView, PasswordPrompt}, + boot_model::{BootChange, BootProjection, BootState, boot_origin::BootOrigin}, + boot_view::{BootView, PasswordPrompt, project_boot}, screen_settings::{color_scheme::Colors, config_file::Timing, font_glyphs::Glyphs}, systemd_link::{link_error::Error, monotonic_clock::monotonic_now}, }; const INPUT_BATCH: usize = 64; +const PROJECTION_REFRESH: Duration = Duration::from_millis(100); pub enum ScreenInput { Boot(Vec), @@ -55,6 +56,11 @@ pub trait ConsoleDesk { } } +struct CachedProjection { + projection: BootProjection, + built_at: Duration, +} + pub struct WatchScreen { pub state: BootState, pub desk: Desk, @@ -63,6 +69,7 @@ pub struct WatchScreen { colors: Colors, timing: Timing, epoch: Duration, + projection: Option, } impl WatchScreen { @@ -82,6 +89,7 @@ impl WatchScreen { colors, timing, epoch, + projection: None, } } @@ -119,6 +127,7 @@ impl WatchScreen { changes .into_iter() .for_each(|change| self.state.apply(change)); + self.projection = None; if self.state.phase != phase_before && let Some(origin) = self.desk.refreshed_origin() { @@ -157,10 +166,25 @@ impl WatchScreen { terminal.clear()?; } let now = self.now(); + if self + .projection + .as_ref() + .is_some_and(|cached| now.saturating_sub(cached.built_at) >= PROJECTION_REFRESH) + { + self.projection = None; + } + let projection = &self + .projection + .get_or_insert_with(|| CachedProjection { + projection: project_boot(&self.state, &self.timing, now), + built_at: now, + }) + .projection; let completed = terminal.draw(|canvas| { canvas.render_widget( BootView { state: &self.state, + projection, colors: &self.colors, timing: &self.timing, glyphs: self.desk.glyphs(),