Perf improvements

This commit is contained in:
Antoine Gersant 2024-08-09 11:24:53 -07:00
parent e6483cf138
commit a4baa2c792
4 changed files with 28 additions and 18 deletions

View file

@ -3,7 +3,7 @@ use std::{
sync::{Arc, RwLock},
};
use lasso2::ThreadedRodeo;
use lasso2::{RodeoReader, ThreadedRodeo};
use log::{error, info};
use serde::{Deserialize, Serialize};
use tokio::task::spawn_blocking;
@ -200,13 +200,23 @@ impl Manager {
}
}
#[derive(Default, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct Index {
pub strings: ThreadedRodeo,
pub strings: RodeoReader,
pub browser: browser::Browser,
pub collection: collection::Collection,
}
impl Default for Index {
fn default() -> Self {
Self {
strings: ThreadedRodeo::new().into_reader(),
browser: Default::default(),
collection: Default::default(),
}
}
}
pub struct Builder {
strings: ThreadedRodeo,
browser_builder: browser::Builder,
@ -236,7 +246,7 @@ impl Builder {
Index {
browser: self.browser_builder.build(),
collection: self.collection_builder.build(),
strings: self.strings,
strings: self.strings.into_reader(),
}
}
}

View file

@ -5,7 +5,7 @@ use std::{
path::{Path, PathBuf},
};
use lasso2::ThreadedRodeo;
use lasso2::{RodeoReader, ThreadedRodeo};
use serde::{Deserialize, Serialize};
use tinyvec::TinyVec;
use trie_rs::{Trie, TrieBuilder};
@ -40,7 +40,7 @@ impl Default for Browser {
impl Browser {
pub fn browse<P: AsRef<Path>>(
&self,
strings: &ThreadedRodeo,
strings: &RodeoReader,
virtual_path: P,
) -> Result<Vec<File>, Error> {
let path = virtual_path
@ -70,7 +70,7 @@ impl Browser {
pub fn flatten<P: AsRef<Path>>(
&self,
strings: &ThreadedRodeo,
strings: &RodeoReader,
virtual_path: P,
) -> Result<Vec<PathBuf>, Error> {
let path_components = virtual_path
@ -87,7 +87,7 @@ impl Browser {
Ok(self
.flattened
.predictive_search(path_components)
.map(|c: Vec<_>| -> PathBuf {
.map(|c: TinyVec<[_; 8]>| -> PathBuf {
c.into_iter()
.map(|s| strings.resolve(&s))
.collect::<TinyVec<[&str; 8]>>()

View file

@ -4,7 +4,7 @@ use std::{
path::PathBuf,
};
use lasso2::ThreadedRodeo;
use lasso2::{RodeoReader, ThreadedRodeo};
use rand::{rngs::ThreadRng, seq::IteratorRandom};
use serde::{Deserialize, Serialize};
@ -60,7 +60,7 @@ pub struct Collection {
}
impl Collection {
pub fn get_artist(&self, strings: &ThreadedRodeo, artist_key: ArtistKey) -> Option<Artist> {
pub fn get_artist(&self, strings: &RodeoReader, artist_key: ArtistKey) -> Option<Artist> {
self.artists.get(&artist_key).map(|a| {
let albums = {
let mut albums = a
@ -94,7 +94,7 @@ impl Collection {
})
}
pub fn get_album(&self, strings: &ThreadedRodeo, album_key: AlbumKey) -> Option<Album> {
pub fn get_album(&self, strings: &RodeoReader, album_key: AlbumKey) -> Option<Album> {
self.albums.get(&album_key).map(|a| {
let mut songs = a
.songs
@ -130,7 +130,7 @@ impl Collection {
})
}
pub fn get_random_albums(&self, strings: &ThreadedRodeo, count: usize) -> Vec<Album> {
pub fn get_random_albums(&self, strings: &RodeoReader, count: usize) -> Vec<Album> {
self.albums
.keys()
.choose_multiple(&mut ThreadRng::default(), count)
@ -139,7 +139,7 @@ impl Collection {
.collect()
}
pub fn get_recent_albums(&self, strings: &ThreadedRodeo, count: usize) -> Vec<Album> {
pub fn get_recent_albums(&self, strings: &RodeoReader, count: usize) -> Vec<Album> {
self.recent_albums
.iter()
.take(count)
@ -147,7 +147,7 @@ impl Collection {
.collect()
}
pub fn get_song(&self, strings: &ThreadedRodeo, song_key: SongKey) -> Option<Song> {
pub fn get_song(&self, strings: &RodeoReader, song_key: SongKey) -> Option<Song> {
self.songs.get(&song_key).map(|s| fetch_song(strings, s))
}
}

View file

@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};
use lasso2::ThreadedRodeo;
use lasso2::{RodeoReader, ThreadedRodeo};
use log::error;
use serde::{Deserialize, Serialize};
use tinyvec::TinyVec;
@ -149,7 +149,7 @@ pub fn store_song(strings: &mut ThreadedRodeo, song: &scanner::Song) -> Option<S
})
}
pub fn fetch_song(strings: &ThreadedRodeo, song: &Song) -> super::Song {
pub fn fetch_song(strings: &RodeoReader, song: &Song) -> super::Song {
super::Song {
path: PathBuf::from(strings.resolve(&song.path.0)),
virtual_path: PathBuf::from(strings.resolve(&song.virtual_path.0)),
@ -197,7 +197,7 @@ pub fn fetch_song(strings: &ThreadedRodeo, song: &Song) -> super::Song {
pub trait InternPath {
fn get_or_intern(self, strings: &mut ThreadedRodeo) -> Option<PathKey>;
fn get(self, strings: &ThreadedRodeo) -> Option<PathKey>;
fn get(self, strings: &RodeoReader) -> Option<PathKey>;
}
impl<P: AsRef<Path>> InternPath for P {
@ -214,7 +214,7 @@ impl<P: AsRef<Path>> InternPath for P {
id
}
fn get(self, strings: &ThreadedRodeo) -> Option<PathKey> {
fn get(self, strings: &RodeoReader) -> Option<PathKey> {
let id = self
.as_ref()
.as_os_str()