Skip to content

Commit

Permalink
Improve performance of functions counting the number of plays
Browse files Browse the repository at this point in the history
See #22
<#22 (comment)>

It looks worse and I think I've done this the first time, then went
to creating aspects, dervied Eq for them, and compared those directly
for it to look nicer. But seems like allocating a new aspect every time
is very time-consuming. Comparing Strings directly without re-allocating
anything is the way to go apparently.

For normal single usage it doesn't matter. But I discovered this while
working on plots and there I have to call such a function for every data
point (bc I don't want to lower resolution it's equivalent to every time
the aspect exists in the dataset heh). There it makes the code 6x faster
  • Loading branch information
fsktom committed Mar 29, 2023
1 parent 57be173 commit 8b0fcdc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
19 changes: 6 additions & 13 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,7 @@ fn gather_artist(entries: &Vec<SongEntry>, art: &Artist) -> ArtistPlays {
let mut artist_asp = ArtistPlays(art.clone(), 0);

for entry in entries {
let artist = Artist::new(entry.artist.clone());

if artist == *art {
if entry.artist.eq(&art.name) {
artist_asp.1 += 1;
}
}
Expand All @@ -392,9 +390,7 @@ fn gather_album(entries: &Vec<SongEntry>, alb: &Album) -> AlbumPlays {
let mut album_asp = AlbumPlays(alb.clone(), 0);

for entry in entries {
let album = Album::new(entry.album.clone(), entry.artist.clone());

if album == *alb {
if entry.artist.eq(&alb.artist.name) && entry.album.eq(&alb.name) {
album_asp.1 += 1;
}
}
Expand All @@ -407,13 +403,10 @@ fn gather_song(entries: &Vec<SongEntry>, son: &Song) -> SongPlays {
let mut song_asp = SongPlays(son.clone(), 0);

for entry in entries {
let song = Song::new(
entry.track.clone(),
entry.album.clone(),
entry.artist.clone(),
);

if song == *son {
if entry.artist.eq(&son.album.artist.name)
&& entry.album.eq(&son.album.name)
&& entry.track.eq(&son.name)
{
song_asp.1 += 1;
}
}
Expand Down
25 changes: 12 additions & 13 deletions src/display/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ fn gather_artist_date(
let mut artist_asp = ArtistPlays(art.clone(), 0);

for entry in entries {
let artist = Artist::new(entry.artist.clone());

if entry.timestamp.ge(start) && entry.timestamp.le(end) && artist == *art {
if entry.timestamp.ge(start) && entry.timestamp.le(end) && entry.artist.eq(&art.name) {
artist_asp.1 += 1;
}
}
Expand All @@ -119,9 +117,11 @@ fn gather_album_date(
let mut album_asp = AlbumPlays(alb.clone(), 0);

for entry in entries {
let album = Album::new(entry.album.clone(), entry.artist.clone());

if entry.timestamp.ge(start) && entry.timestamp.le(end) && album == *alb {
if entry.timestamp.ge(start)
&& entry.timestamp.le(end)
&& entry.artist.eq(&alb.artist.name)
&& entry.album.eq(&alb.name)
{
album_asp.1 += 1;
}
}
Expand All @@ -141,13 +141,12 @@ fn gather_song_date(
let mut song_asp = SongPlays(son.clone(), 0);

for entry in entries {
let song = Song::new(
entry.track.clone(),
entry.album.clone(),
entry.artist.clone(),
);

if entry.timestamp.ge(start) && entry.timestamp.le(end) && song == *son {
if entry.timestamp.ge(start)
&& entry.timestamp.le(end)
&& entry.artist.eq(&son.album.artist.name)
&& entry.album.eq(&son.album.name)
&& entry.track.eq(&son.name)
{
song_asp.1 += 1;
}
}
Expand Down

0 comments on commit 8b0fcdc

Please sign in to comment.