app: read the history a page at a time and open clips on demand

Every launch parsed all 313 timelines, raw responses and all, which put
2.1 GB of JSON on the heap at startup.

The list now holds `Summary` rows: `history::summaries` parses a file,
keeps the champion, queue, scoreline, LP change and event count, then
drops the timeline. Files are listed by write time, and `poll` reads the
next 20 when the list is scrolled near its end or when the filtered rows
do not fill the screen. Selecting a row hands its id to the review
screen, which reads that one timeline on a thread.

Peak memory over 313 recordings: 165 MB.
This commit is contained in:
vhaudiquet committed 2026-11-16 23:48:58 +01:00
1 parent 4efc2f3968
commit 6584b2f92f
7 files changed
+318 -86

No files matched your search

+128 -22
View File
@@ -1,12 +1,13 @@
//! App state, the frame hook and the screen dispatch.
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, TryRecvError, channel};
use std::time::Duration;
use rune::tok::type_;
use rune::{Cx, El, Key, KeyEvent, Tx, col, el, row, text};
use crate::history::{self, QueueCategory, Timeline};
use crate::history::{self, QueueCategory, Summary, Timeline};
use crate::ipc::{self, Daemon};
use crate::os;
use crate::portraits;
@@ -92,6 +93,16 @@ pub struct AudioDevice {
pub is_default: bool,
}
/// How many rows the history reads at a time.
const GAMES_PAGE: usize = 20;
/// One page of history rows, with the file list when it is the first page.
pub struct GamesPage {
paths: Vec<PathBuf>,
next: usize,
games: Vec<Summary>,
}
/// A message banner shown above the screen until dismissed.
#[derive(Debug, Clone)]
pub struct Notice {
@@ -110,9 +121,16 @@ pub struct App {
pub form: screens::settings::Form,
pub encoders: Encoders,
pub devices: AudioDevices,
pub games: Vec<Timeline>,
pub games_rx: Option<Receiver<Vec<Timeline>>>,
/// One row per recording on disk, a page at a time.
pub games: Vec<Summary>,
/// Every timeline file, most recent first: `games` holds the first `games_next` of them.
pub games_paths: Vec<PathBuf>,
pub games_rx: Option<Receiver<GamesPage>>,
pub games_loaded: bool,
/// How many of `games_paths` have been turned into rows.
pub games_next: usize,
/// The timeline the review is waiting for, once a row is opened.
pub review_rx: Option<Receiver<Option<Timeline>>>,
pub filter: Option<QueueCategory>,
pub review: screens::review::Review,
pub portraits: Option<portraits::Portraits>,
@@ -155,8 +173,11 @@ impl App {
encoders: Encoders::default(),
devices: AudioDevices::default(),
games: Vec::new(),
games_paths: Vec::new(),
games_rx: None,
games_loaded: false,
games_next: 0,
review_rx: None,
filter: None,
review: screens::review::Review::default(),
portraits: None,
@@ -192,7 +213,10 @@ impl App {
current_game_id: None,
client_connected: true,
};
self.games = history::sample();
self.games = history::sample()
.iter()
.map(|game| game.summary())
.collect();
self.games_loaded = true;
self.devices = AudioDevices {
input_devices: vec![AudioDevice {
@@ -360,23 +384,58 @@ impl App {
self.processes_rx = Some(rx);
}
/// Read the timelines on a thread of their own; `poll` picks the list up.
/// List the timelines on disk and read the first page of rows on a thread of their own.
/// `poll` picks them up.
pub fn refresh_games(&mut self) {
if self.games_rx.is_some() {
return;
}
let (tx, rx) = channel();
std::thread::spawn(move || {
let games = history::load_all();
let _ = tx.send(games);
let paths = history::timeline_paths();
let end = paths.len().min(GAMES_PAGE);
let games = history::summaries(&paths[..end]);
let _ = tx.send(GamesPage {
paths,
next: end,
games,
});
});
self.games_rx = Some(rx);
}
/// Read the next page of rows, if the list has one and none is already on its way.
///
/// Called from `poll` while the history list is scrolled near its end, and while the
/// filtered list is shorter than the screen.
pub fn load_more_games(&mut self) {
if self.games_rx.is_some() || self.games_next >= self.games_paths.len() {
return;
}
let start = self.games_next;
let end = (start + GAMES_PAGE).min(self.games_paths.len());
let page = self.games_paths[start..end].to_vec();
let (tx, rx) = channel();
std::thread::spawn(move || {
let games = history::summaries(&page);
let _ = tx.send(GamesPage {
paths: Vec::new(),
next: end,
games,
});
});
self.games_next = end;
self.games_rx = Some(rx);
}
/// Whether more rows are waiting to be read.
pub fn more_games(&self) -> bool {
self.games_next < self.games_paths.len()
}
/// The game the review screen is showing, if any.
pub fn reviewed(&self) -> Option<&Timeline> {
let id = self.review.game.as_deref()?;
self.games.iter().find(|game| game.recording_id == id)
self.review.game.as_ref()
}
}
@@ -579,8 +638,17 @@ impl App {
if let Some(rx) = &self.games_rx {
match rx.try_recv() {
Ok(games) => {
self.games = games;
Ok(page) => {
if !page.paths.is_empty() {
self.games = page.games;
self.games_paths = page.paths;
} else {
self.games.extend(page.games);
}
// Pages arrive in write-time order; the list reads by start time.
self.games
.sort_by_key(|game| std::cmp::Reverse(game.start_time));
self.games_next = page.next;
self.games_loaded = true;
self.games_rx = None;
changed = true;
@@ -593,6 +661,43 @@ impl App {
}
}
if let Some(rx) = &self.review_rx {
match rx.try_recv() {
Ok(None) => {
self.review_rx = None;
self.review.loading = false;
self.review.error = Some("That recording's timeline could not be read.".into());
changed = true;
}
Ok(Some(game)) => {
self.review_rx = None;
self.review.loading = false;
screens::review::show(self, game);
changed = true;
}
Err(TryRecvError::Empty) => {}
Err(TryRecvError::Disconnected) => {
self.review_rx = None;
self.review.loading = false;
}
}
}
// The history reads its rows a page at a time. Fetch the next page when the list is
// scrolled near its end, or when what it shows does not fill the screen: a filter can
// leave three rows on a page of twenty.
if self.screen == Screen::History
&& self.games_loaded
&& self.more_games()
&& self.games_rx.is_none()
{
// Nothing has reported where the viewport is until a frame has laid it out.
let at = self.scroll.position();
let room_left = at.max[1] - (at.offset[1] + at.viewport[1]);
if at.viewport[1] > 0.0 && room_left < 400.0 {
self.load_more_games();
}
}
let mut tray_events = Vec::new();
if let Some(rx) = &self.tray_events {
while let Ok(event) = rx.try_recv() {
@@ -906,15 +1011,22 @@ pub fn demo_review(app: &mut App) {
app.pending_review = false;
let live = std::env::args().any(|arg| arg == "--live");
if live {
let index = app
let row = app
.games
.iter()
.position(|game| !game.events.is_empty())
.unwrap_or(0);
screens::review::open(app, index);
.find(|game| game.event_count > 0)
.map(|game| game.recording_id.clone());
if let Some(id) = row {
screens::review::open(app, &id);
}
return;
}
app.review.game = app.games.first().map(|game| game.recording_id.clone());
let mut sample = crate::history::sample();
if sample.is_empty() {
return;
}
screens::review::show(app, sample.remove(0));
app.review.error = Some("Sample recording: no clip on disk.".to_string());
// Snapshot hook: draw a real clip when one is handed in.
if let Ok(path) = std::env::var("LR_DEMO_VIDEO") {
@@ -923,10 +1035,4 @@ pub fn demo_review(app: &mut App) {
app.review.player = Some(player);
app.review.error = None;
}
if let Some(game) = app.games.first() {
app.review.highlights =
crate::highlight::compute(game, game.summoner().as_deref(), Default::default());
app.review.duration = game.duration_secs.max(0) as f64;
}
}