perf: remove unnecessary wally mutex

This commit is contained in:
daimond113 2024-12-28 18:09:20 +01:00
parent 8e6d877241
commit a091d06f36
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C

View file

@ -18,7 +18,7 @@ use crate::{
Project, Project,
}; };
use fs_err::tokio as fs; use fs_err::tokio as fs;
use futures::{future::try_join_all, StreamExt}; use futures::StreamExt;
use gix::Url; use gix::Url;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use reqwest::header::AUTHORIZATION; use reqwest::header::AUTHORIZATION;
@ -281,14 +281,14 @@ impl PackageSource for WallyPackageSource {
}); });
let mut bytes = StreamReader::new(bytes); let mut bytes = StreamReader::new(bytes);
let mut buf = vec![]; let mut buf = Vec::with_capacity(total_len as usize);
bytes.read_to_end(&mut buf).await?; bytes.read_to_end(&mut buf).await?;
let archive = let mut archive =
async_zip::tokio::read::seek::ZipFileReader::with_tokio(std::io::Cursor::new(&mut buf)) async_zip::tokio::read::seek::ZipFileReader::with_tokio(std::io::Cursor::new(&mut buf))
.await?; .await?;
let entries = (0..archive.file().entries().len()) let archive_entries = (0..archive.file().entries().len())
.map(|index| { .map(|index| {
let entry = archive.file().entries().get(index).unwrap(); let entry = archive.file().entries().get(index).unwrap();
let relative_path = RelativePathBuf::from_path(entry.filename().as_str()?).unwrap(); let relative_path = RelativePathBuf::from_path(entry.filename().as_str()?).unwrap();
@ -296,57 +296,40 @@ impl PackageSource for WallyPackageSource {
}) })
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
let archive = Arc::new(Mutex::new(archive)); let mut entries = BTreeMap::new();
// todo: remove this asyncification, since the Mutex makes it sequential anyway for (index, is_dir, relative_path) in archive_entries {
let entries = try_join_all( let name = relative_path.file_name().unwrap_or("");
entries if if is_dir {
.into_iter() IGNORED_DIRS.contains(&name)
.filter(|(_, is_dir, relative_path)| { } else {
let name = relative_path.file_name().unwrap_or(""); IGNORED_FILES.contains(&name)
} {
continue;
}
if *is_dir { let path = relative_path.to_path(tempdir.path());
return !IGNORED_DIRS.contains(&name);
}
!IGNORED_FILES.contains(&name) if is_dir {
}) fs::create_dir_all(&path).await?;
.map(|(index, is_dir, relative_path)| { entries.insert(relative_path, FSEntry::Directory);
let tempdir_path = tempdir.path().to_path_buf(); continue;
let archive = archive.clone(); }
async move { let entry_reader = archive.reader_without_entry(index).await?;
let path = relative_path.to_path(tempdir_path); if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await?;
}
if is_dir { let writer = Arc::new(Mutex::new(fs::File::create(&path).await?));
fs::create_dir_all(&path).await?; let hash = store_in_cas(project.cas_dir(), entry_reader.compat(), |bytes| {
return Ok::<_, errors::DownloadError>(( let writer = writer.clone();
relative_path, async move { writer.lock().await.write_all(&bytes).await }
FSEntry::Directory, })
)); .await?;
}
let mut archive = archive.lock().await; entries.insert(relative_path, FSEntry::File(hash));
let entry_reader = archive.reader_without_entry(index).await?; }
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await?;
}
let writer = Arc::new(Mutex::new(fs::File::create(&path).await?));
let hash =
store_in_cas(project.cas_dir(), entry_reader.compat(), |bytes| {
let writer = writer.clone();
async move { writer.lock().await.write_all(&bytes).await }
})
.await?;
Ok((relative_path, FSEntry::File(hash)))
}
}),
)
.await?
.into_iter()
.collect::<BTreeMap<_, _>>();
let fs = PackageFS::CAS(entries); let fs = PackageFS::CAS(entries);