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;
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},
+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::{
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)
+40 -19
View File
@@ -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<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]
#[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);
});
}
+1 -1
View File
@@ -47,7 +47,7 @@ pub struct Timeline<'a> {
pub origin: Duration,
pub now: Duration,
pub root_switches: Vec<Duration>,
pub milestones: Vec<MilestoneRow>,
pub milestones: &'a [MilestoneRow],
pub finished: Vec<&'a FinishedRun>,
pub running: Vec<RunningRun<'a>>,
pub colors: &'a TimelineColors,
+5 -2
View File
@@ -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<PasswordPrompt>,
) -> 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(),
+26 -2
View File
@@ -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<BootChange>),
@@ -55,6 +56,11 @@ pub trait ConsoleDesk {
}
}
struct CachedProjection {
projection: BootProjection,
built_at: Duration,
}
pub struct WatchScreen<Desk> {
pub state: BootState,
pub desk: Desk,
@@ -63,6 +69,7 @@ pub struct WatchScreen<Desk> {
colors: Colors,
timing: Timing,
epoch: Duration,
projection: Option<CachedProjection>,
}
impl<Desk: ConsoleDesk> WatchScreen<Desk> {
@@ -82,6 +89,7 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
colors,
timing,
epoch,
projection: None,
}
}
@@ -119,6 +127,7 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
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<Desk: ConsoleDesk> WatchScreen<Desk> {
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(),