rel make: Avoid EMFILE when loading many modules (#122)

This commit is contained in:
2025-11-12 12:26:20 -07:00
committed by GitHub
parent 2a3eaf9d4f
commit 8e417a4ad6
2 changed files with 17 additions and 6 deletions

View File

@@ -300,7 +300,15 @@ fn make(args: MakeArgs) -> Result<()> {
}
// Load all modules
let mut files = paths.iter().map(|p| open_file(p, true)).collect::<Result<Vec<_>>>()?;
let mut files = paths
.iter()
.map(|p| {
let mut file = open_file(p, true)?;
// Immediately map to avoid keeping file handles open
file.map()?;
Ok(file)
})
.collect::<Result<Vec<_>>>()?;
let modules = files
.par_iter_mut()
.enumerate()

View File

@@ -86,14 +86,17 @@ impl Seek for StdFile {
impl VfsFile for StdFile {
fn map(&mut self) -> io::Result<&[u8]> {
let file = match self.file {
Some(ref mut file) => file,
None => self.file.insert(BufReader::new(fs::File::open(&self.path)?)),
};
let mmap = match self.mmap {
Some(ref mmap) => mmap,
None => self.mmap.insert(unsafe { memmap2::Mmap::map(file.get_ref())? }),
None => {
let file = match self.file {
Some(ref mut file) => file,
None => self.file.insert(BufReader::new(fs::File::open(&self.path)?)),
};
self.mmap.insert(unsafe { memmap2::Mmap::map(file.get_ref())? })
}
};
self.file = None; // Drop the BufReader to avoid holding the file handle
Ok(mmap)
}