Intern strings in flattened

This commit is contained in:
Antoine Gersant 2024-08-04 19:25:39 -07:00
parent 6b1133e27c
commit f0fa985f8a

View file

@ -23,7 +23,7 @@ pub enum File {
pub struct Browser { pub struct Browser {
strings: Arc<ThreadedRodeo>, strings: Arc<ThreadedRodeo>,
directories: HashMap<PathID, Vec<storage::File>>, directories: HashMap<PathID, Vec<storage::File>>,
flattened: Trie<String>, flattened: Trie<lasso2::Spur>,
} }
impl Browser { impl Browser {
@ -40,9 +40,11 @@ impl Browser {
.as_ref() .as_ref()
.get(&self.strings) .get(&self.strings)
.ok_or_else(|| Error::DirectoryNotFound(virtual_path.as_ref().to_owned()))?; .ok_or_else(|| Error::DirectoryNotFound(virtual_path.as_ref().to_owned()))?;
let Some(files) = self.directories.get(&path_id) else { let Some(files) = self.directories.get(&path_id) else {
return Err(Error::DirectoryNotFound(virtual_path.as_ref().to_owned())); return Err(Error::DirectoryNotFound(virtual_path.as_ref().to_owned()));
}; };
Ok(files Ok(files
.iter() .iter()
.map(|f| { .map(|f| {
@ -63,8 +65,9 @@ impl Browser {
let path_components = virtual_path let path_components = virtual_path
.as_ref() .as_ref()
.components() .components()
.map(|c| c.as_os_str().to_string_lossy().to_string()) .map(|c| c.as_os_str().to_str().unwrap_or_default())
.collect::<Vec<String>>(); .filter_map(|c| self.strings.get(c))
.collect::<Vec<_>>();
if !self.flattened.is_prefix(&path_components) { if !self.flattened.is_prefix(&path_components) {
return Err(Error::DirectoryNotFound(virtual_path.as_ref().to_owned())); return Err(Error::DirectoryNotFound(virtual_path.as_ref().to_owned()));
@ -73,7 +76,13 @@ impl Browser {
Ok(self Ok(self
.flattened .flattened
.predictive_search(path_components) .predictive_search(path_components)
.map(|c: Vec<String>| -> PathBuf { c.join(std::path::MAIN_SEPARATOR_STR).into() }) .map(|c: Vec<_>| -> PathBuf {
c.into_iter()
.map(|s| self.strings.resolve(&s))
.collect::<Vec<_>>()
.join(std::path::MAIN_SEPARATOR_STR)
.into()
})
.collect::<Vec<_>>()) .collect::<Vec<_>>())
} }
} }
@ -81,7 +90,7 @@ impl Browser {
pub struct Builder { pub struct Builder {
strings: Arc<ThreadedRodeo>, strings: Arc<ThreadedRodeo>,
directories: HashMap<PathID, Vec<storage::File>>, directories: HashMap<PathID, Vec<storage::File>>,
flattened: TrieBuilder<String>, flattened: TrieBuilder<lasso2::Spur>,
} }
impl Builder { impl Builder {
@ -130,14 +139,17 @@ impl Builder {
self.flattened.push( self.flattened.push(
song.virtual_path song.virtual_path
.components() .components()
.map(|c| c.as_os_str().to_string_lossy().to_string()) .map(|c| self.strings.get_or_intern(c.as_os_str().to_str().unwrap()))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
); );
} }
pub fn build(mut self) -> Browser { pub fn build(mut self) -> Browser {
for directory in self.directories.values_mut() { for directory in self.directories.values_mut() {
directory.sort(); directory.sort_by_key(|f| match f {
storage::File::Directory(p) => self.strings.resolve(&p.0),
storage::File::Song(p) => self.strings.resolve(&p.0),
});
} }
Browser { Browser {
strings: self.strings, strings: self.strings,