Skip to content

Commit c12480d

Browse files
committed
perf: open from the cache instead of waiting on the art recheck
Just to minimize the time of the loading splash screen
1 parent 1a9878e commit c12480d

2 files changed

Lines changed: 109 additions & 38 deletions

File tree

‎crates/launcher-gui/src/main.rs‎

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ struct Splash {
405405
products: usize,
406406
/// When the window opened, so [`SPLASH_FLOOR`] can be honoured.
407407
opened: Instant,
408+
/// Whether the cache has been read and the splash knows if it waits for
409+
/// the network. Decided once, on the first cache-only pass.
410+
decided: bool,
408411
}
409412

410413
#[derive(Debug, Clone)]
@@ -1900,22 +1903,15 @@ impl Lightforge {
19001903
/// Open the splash and start the fetch it is waiting on.
19011904
fn open_splash(&mut self) -> Task<Message> {
19021905
let codes: Vec<ProductCode> = self.choices.iter().map(|c| c.code.clone()).collect();
1903-
self.splash = Some(Splash { products: codes.len(), opened: Instant::now() });
1906+
self.splash = Some(Splash { products: codes.len(), opened: Instant::now(), decided: false });
1907+
// Nothing is fetched yet: the cache-only pass `reload` starts beside
1908+
// this decides whether anything needs to be. See `splash_decide`.
19041909
Task::batch([
19051910
// The window is the launcher's size but only the card is painted,
19061911
// so the rest of it is a transparent rectangle that would still
19071912
// swallow every click landing on whatever is behind it. On
19081913
// Wayland this sets a real empty input region, not a hint.
19091914
iced::window::oldest().and_then(iced::window::enable_mouse_passthrough),
1910-
Task::perform(
1911-
tasks::prefetch_within(
1912-
SPLASH_BUDGET,
1913-
self.http.clone(),
1914-
self.pipeline.clone(),
1915-
codes,
1916-
),
1917-
Message::SplashDone,
1918-
),
19191915
// Beside the artwork rather than before it: this is the one moment
19201916
// a start is already waiting on the network, and nothing on screen
19211917
// depends on the answer, so it never holds the splash open.
@@ -1926,6 +1922,51 @@ impl Lightforge {
19261922
])
19271923
}
19281924

1925+
/// The splash's one decision, made when the selected game's cache-only
1926+
/// pass lands: close, or wait for the network.
1927+
///
1928+
/// The splash exists so nobody sees a grey launcher, so it closes early
1929+
/// only when the cache already holds everything the first screen draws,
1930+
/// decoded and in state by now. The revalidation `reload` started runs
1931+
/// on behind the launcher and swaps in whatever changed, exactly as on a
1932+
/// product switch. Anything missing, or a first run, whose wizard counts
1933+
/// off the prefetch, waits for the full fetch as it always has.
1934+
fn splash_decide(&mut self, warm: bool) -> Task<Message> {
1935+
let Some(splash) = self.splash.as_mut().filter(|s| !s.decided) else {
1936+
return Task::none();
1937+
};
1938+
splash.decided = true;
1939+
if warm && self.wizard.is_none() {
1940+
trace::mark("splash: cache is warm, not waiting on the network");
1941+
return self.close_splash_after_floor();
1942+
}
1943+
Task::perform(
1944+
tasks::prefetch_within(
1945+
SPLASH_BUDGET,
1946+
self.http.clone(),
1947+
self.pipeline.clone(),
1948+
self.choices.iter().map(|c| c.code.clone()).collect(),
1949+
),
1950+
Message::SplashDone,
1951+
)
1952+
}
1953+
1954+
/// Close the splash once it has been up for [`SPLASH_FLOOR`].
1955+
fn close_splash_after_floor(&mut self) -> Task<Message> {
1956+
let waited = self.splash.as_ref().map(|s| s.opened.elapsed()).unwrap_or_default();
1957+
match SPLASH_FLOOR.checked_sub(waited) {
1958+
Some(left) if !left.is_zero() => {
1959+
Task::perform(tasks::after(left), |()| Message::FinishSplash)
1960+
}
1961+
// Both ways out go through `finish_splash` and nothing else. An
1962+
// earlier version did the work in two places, and the two copies
1963+
// drifted: the one taken whenever the fetch outlasts the floor,
1964+
// which is every start with anything to fetch, missed a step the
1965+
// other had.
1966+
_ => self.finish_splash(),
1967+
}
1968+
}
1969+
19291970
/// Put the release notes up, if this start is the first one on this
19301971
/// version.
19311972
///
@@ -2651,10 +2692,14 @@ impl Lightforge {
26512692
if self.icons.is_empty() {
26522693
self.icons = themed.icons;
26532694
}
2654-
if code == self.selected.code && self.branding.is_none() {
2695+
let selected = code == self.selected.code;
2696+
if selected && self.branding.is_none() {
26552697
self.branding = themed.branding;
26562698
}
2657-
Task::none()
2699+
match selected {
2700+
true => self.splash_decide(themed.warm),
2701+
false => Task::none(),
2702+
}
26582703
}
26592704

26602705
Message::Themed(code, themed) => {
@@ -3164,19 +3209,9 @@ impl Lightforge {
31643209
if let Some(wizard) = self.wizard.as_mut() {
31653210
wizard.prefetched = Some(count);
31663211
}
3167-
let waited =
3168-
self.splash.as_ref().map(|s| s.opened.elapsed()).unwrap_or_default();
3169-
match SPLASH_FLOOR.checked_sub(waited) {
3170-
Some(left) if !left.is_zero() => {
3171-
Task::perform(tasks::after(left), |()| Message::FinishSplash)
3172-
}
3173-
// Both ways out go through `finish_splash` and nothing
3174-
// else. An earlier version did the work here directly as
3175-
// well, and the two copies drifted: the one taken
3176-
// whenever the fetch outlasts the floor, which is every
3177-
// start with anything to fetch, missed a step the other
3178-
// had.
3179-
_ => self.finish_splash(),
3212+
match self.splash.is_some() {
3213+
true => self.close_splash_after_floor(),
3214+
false => Task::none(),
31803215
}
31813216
}
31823217

‎crates/launcher-gui/src/tasks.rs‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,16 @@ pub async fn cached_theme(
238238
Some(manifest) => cached_icons(&pipeline, manifest, &codes),
239239
None => HashMap::new(),
240240
};
241-
Themed { manifest: manifest.map(Arc::new), branding, icons }
241+
let warm = manifest.as_ref().is_some_and(|manifest| {
242+
let wants_art = manifest.slot(&code, |t| t.logo.as_ref()).is_some()
243+
&& manifest.slot(&code, |t| t.background.as_ref()).is_some();
244+
let wants_icons = codes
245+
.iter()
246+
.filter(|c| manifest.own_theme(c).is_some_and(|t| t.icon.is_some()))
247+
.count();
248+
(!wants_art || branding.is_some()) && icons.len() == wants_icons
249+
});
250+
Themed { manifest: manifest.map(Arc::new), branding, icons, warm }
242251
})
243252
.await
244253
.unwrap_or_default()
@@ -271,6 +280,10 @@ pub struct Themed {
271280
pub manifest: Option<Arc<Manifest>>,
272281
pub branding: Option<Art>,
273282
pub icons: HashMap<ProductCode, Mark>,
283+
/// Set only by the cache-only pass: everything the first screen draws
284+
/// was on disk, the selected game's art and every product's mark, so the
285+
/// splash has nothing left to wait for.
286+
pub warm: bool,
274287
}
275288

276289
/// A product's decoded mark, beside the file it came from.
@@ -355,6 +368,9 @@ pub async fn check_for_update(
355368
found
356369
}
357370

371+
/// How many products a prefetch fetches at once.
372+
const PREFETCH_AT_ONCE: usize = 4;
373+
358374
pub async fn prefetch_within(
359375
budget: std::time::Duration,
360376
http: Option<Http>,
@@ -385,17 +401,37 @@ pub async fn prefetch(
385401
return 0;
386402
}
387403
};
388-
let mut cached = 0;
389-
for code in &codes {
390-
// One at a time rather than joined: these are big files off one host,
391-
// and a first run has plenty of other things it would rather have the
392-
// connection for.
393-
if pipeline.resolve_with(&http, &manifest, code).await.is_ok() {
394-
cached += 1;
395-
}
396-
let _ = pipeline.icon(&http, &manifest, code).await;
397-
}
398-
crate::trace::mark(format_args!("prefetched art for {cached}/{} products", codes.len()));
404+
use futures_util::StreamExt;
405+
406+
// A few products at a time. This only runs with art missing, usually a
407+
// first run, where everything is a real download: one at a time made the
408+
// splash the sum of every transfer, and all at once would open a dozen
409+
// connections to one host (no HTTP/2 here) fighting over a slow link.
410+
//
411+
// Each future owns its handles: borrowing them across `buffer_unordered`
412+
// trips the compiler's higher-ranked `Send` check on the task.
413+
let manifest = Arc::new(manifest);
414+
let total = codes.len();
415+
let cached = futures_util::stream::iter(codes)
416+
.map(|code| {
417+
let (http, pipeline, manifest) = (http.clone(), pipeline.clone(), Arc::clone(&manifest));
418+
async move {
419+
match pipeline.resolve_with(&http, &manifest, &code).await {
420+
// Branding carries the icon; asking again is a round trip.
421+
Ok(_) => true,
422+
// A hand-off has a mark and no art.
423+
Err(_) => {
424+
let _ = pipeline.icon(&http, &manifest, &code).await;
425+
false
426+
}
427+
}
428+
}
429+
})
430+
.buffer_unordered(PREFETCH_AT_ONCE)
431+
.filter(|resolved| std::future::ready(*resolved))
432+
.count()
433+
.await;
434+
crate::trace::mark(format_args!("prefetched art for {cached}/{total} products"));
399435
cached
400436
}
401437

@@ -471,7 +507,7 @@ pub async fn theme(
471507
None => None,
472508
};
473509

474-
Themed { manifest: Some(manifest), branding, icons }
510+
Themed { manifest: Some(manifest), branding, icons, warm: false }
475511
}
476512

477513
/// What a running transfer publishes for the UI to read.

0 commit comments

Comments
 (0)