Switched album list to BTreeMap, improved perf

This commit is contained in:
G2_Games 2023-11-03 12:29:04 -05:00
parent f890f1a026
commit d330c5f0bd

View file

@ -1,7 +1,7 @@
use file_format::{FileFormat, Kind}; use file_format::{FileFormat, Kind};
use lofty::{AudioFile, ItemKey, ItemValue, Probe, TagType, TaggedFileExt}; use lofty::{AudioFile, ItemKey, ItemValue, Probe, TagType, TaggedFileExt};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet, BTreeMap};
use std::{error::Error, io::BufReader}; use std::{error::Error, io::BufReader};
use chrono::{serde::ts_seconds_option, DateTime, Utc}; use chrono::{serde::ts_seconds_option, DateTime, Utc};
@ -766,7 +766,7 @@ impl MusicLibrary {
} }
pub fn albums(&self) -> Result<Vec<Album>, Box<dyn Error>> { pub fn albums(&self) -> Result<Vec<Album>, Box<dyn Error>> {
let mut albums: Vec<Album> = Vec::new(); let mut albums: BTreeMap<&String, Album> = BTreeMap::new();
for result in &self.library { for result in &self.library {
let title = match result.get_tag(&Tag::Album){ let title = match result.get_tag(&Tag::Album){
Some(title) => title, Some(title) => title,
@ -774,26 +774,21 @@ impl MusicLibrary {
}; };
normalize(title); normalize(title);
match albums.binary_search_by_key(&title, |album| { match albums.get_mut(&title) {
normalize(&album.title); Some(album) => album.tracks.push(result),
album.title None => {
}) {
Ok(pos) => {
albums[pos].tracks.push(result);
},
Err(pos) => {
let new_album = Album { let new_album = Album {
title, title,
artist: result.get_tag(&Tag::AlbumArtist), artist: result.get_tag(&Tag::AlbumArtist),
tracks: vec![result], tracks: vec![result],
cover: None, cover: None,
}; };
albums.insert(pos, new_album); albums.insert(title, new_album);
} }
} }
} }
Ok(albums) Ok(albums.into_par_iter().map(|album| album.1).collect())
} }
} }