perf: rebuild the waiting tree only when the boot changed

The screen keeps the projected tree, milestones and tally and rebuilds them
after boot changes, or every 100 ms so finished units still fade on time.
Release, 200 units, under load: a frame from the cached projection costs
30-40% of a full frame (0.35-0.53 ms against 1.2-1.9 ms).
This commit is contained in:
Willem Vanhulle committed 2026-10-21 13:24:35 +02:00
1 parent 092ed41ffb
commit 552d1170ed
7 files changed
+107 -31

No files matched your search

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