From ee950c61f61570a9af44bfca3647890979c06d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Toma=C5=9Bko?= Date: Tue, 15 Oct 2024 04:17:19 +0200 Subject: [PATCH] Make minute sorting faster --- endsong_web/src/lib.rs | 6 +++--- src/gather.rs | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/endsong_web/src/lib.rs b/endsong_web/src/lib.rs index 371de05..70189db 100644 --- a/endsong_web/src/lib.rs +++ b/endsong_web/src/lib.rs @@ -181,14 +181,14 @@ pub async fn top_artists( ) }) .collect(), - Sorting::Minutes => gather::artists(entries) + Sorting::Minutes => gather::artists_with_duration(entries) .into_iter() - .map(|(artist, plays)| { + .map(|(artist, (plays, duration))| { ( format!("/artist/{artist}"), artist.clone(), plays, - gather::listening_time(entries, &artist).num_minutes(), + duration.num_minutes(), ) }) .sorted_unstable_by(|a, b| b.3.cmp(&a.3).then_with(|| a.1.cmp(&b.1))) diff --git a/src/gather.rs b/src/gather.rs index 74ec285..cac7426 100644 --- a/src/gather.rs +++ b/src/gather.rs @@ -207,6 +207,21 @@ pub fn artists(entries: &[SongEntry]) -> HashMap { entries.iter().map(Artist::from).counts() } +/// Returns a map with all [`Artists`][Artist], their playcount and time listened +#[must_use] +pub fn artists_with_duration(entries: &[SongEntry]) -> HashMap { + let mut map = HashMap::with_capacity(entries.len() / 100); + + for entry in entries { + let artist = Artist::from(entry); + let e: &mut (usize, TimeDelta) = map.entry(artist).or_default(); + e.0 += 1; + e.1 += entry.time_played; + } + + map +} + /// Counts up the plays of an [`Artist`], [`Album`] or [`Song`] #[must_use] pub fn plays(entries: &[SongEntry], aspect: &Asp) -> usize {