Skip to content

Commit

Permalink
dumpfile: use buffered writing
Browse files Browse the repository at this point in the history
We were previously doing one write() syscall per dumpfile line, which
was pretty wasteful.  This collects 32KiB before doing a single syscall,
which shaves ~20% the time it takes the write the dumpfile to a pipe or
/dev/null, and is ~3.5x faster when redirecting to a file (although this
is not really our main use case).
  • Loading branch information
allisonkarlitskaya committed Oct 15, 2024
1 parent 2074f3a commit f76506e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/dumpfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::{
OsString,
},
fmt,
io::Write,
io::{
BufWriter,
Write,
},
os::unix::ffi::OsStrExt,
path::{
PathBuf,
Expand Down Expand Up @@ -229,5 +232,14 @@ impl<'a, W: Write> DumpfileWriter<'a, W> {
}

pub fn write_dumpfile<W: Write>(writer: &mut W, fs: &FileSystem) -> Result<()> {
DumpfileWriter::new(writer).write_dir(&mut PathBuf::from("/"), &fs.root)
// default pipe capacity on Linux is 16 pages (65536 bytes), but
// sometimes the BufWriter will write more than its capacity...
let mut buffer = BufWriter::with_capacity(32768, writer);
let mut dfw = DumpfileWriter::new(&mut buffer);
let mut path = PathBuf::from("/");

dfw.write_dir(&mut path, &fs.root)?;
buffer.flush()?;

Ok(())
}

0 comments on commit f76506e

Please sign in to comment.