Skip to content

Commit

Permalink
perf: add BufWriter when writing to file
Browse files Browse the repository at this point in the history
resolves #76
  • Loading branch information
chrjabs committed Apr 4, 2024
1 parent d1d36fd commit ddd6e61
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
19 changes: 19 additions & 0 deletions rustsat/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,22 @@ mod pb_enc {
b.iter(|| build_full_ub::<DynamicPolyWatchdog>(&lits!()));
}
}

mod fio {
extern crate test;

use test::Bencher;

use crate::instances::SatInstance;

fn read_write_dimacs() {
let inst: SatInstance =
SatInstance::from_dimacs_path("./data/minisat-segfault.cnf").unwrap();
inst.to_dimacs_path("/tmp/rustsat-test.cnf").unwrap();
}

#[bench]
fn read_write(b: &mut Bencher) {
b.iter(|| read_write_dimacs());
}
}
20 changes: 11 additions & 9 deletions rustsat/src/instances/fio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,26 @@ pub(crate) fn open_compressed_uncompressed_write<P: AsRef<Path>>(
path: P,
) -> Result<Box<dyn io::Write>, io::Error> {
let path = path.as_ref();
let raw_reader = File::create(path)?;
let raw_writer = File::create(path)?;
#[cfg(feature = "compression")]
if let Some(ext) = path.extension() {
if ext.eq_ignore_ascii_case(std::ffi::OsStr::new("bz2")) {
return Ok(Box::new(bzip2::write::BzEncoder::new(
raw_reader,
return Ok(Box::new(io::BufWriter::new(bzip2::write::BzEncoder::new(
raw_writer,
bzip2::Compression::fast(),
)));
))));
}
if ext.eq_ignore_ascii_case(std::ffi::OsStr::new("gz")) {
return Ok(Box::new(flate2::write::GzEncoder::new(
raw_reader,
return Ok(Box::new(io::BufWriter::new(flate2::write::GzEncoder::new(
raw_writer,
flate2::Compression::fast(),
)));
))));
}
if ext.eq_ignore_ascii_case(std::ffi::OsStr::new("xz")) {
return Ok(Box::new(xz2::write::XzEncoder::new(raw_reader, 1)));
return Ok(Box::new(io::BufWriter::new(xz2::write::XzEncoder::new(
raw_writer, 1,
))));
}
}
Ok(Box::new(raw_reader))
Ok(Box::new(io::BufWriter::new(raw_writer)))
}
8 changes: 8 additions & 0 deletions rustsat/src/instances/multiopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ impl<VM: ManageVars> MultiOptInstance<VM> {
}

/// Writes the instance to a DIMACS MCNF file at a path
///
/// # Performance
///
/// For performance, consider using a [`std::io::BufWriter`] instance.
pub fn to_dimacs_path<P: AsRef<Path>>(self, path: P) -> Result<(), io::Error> {
let mut writer = fio::open_compressed_uncompressed_write(path)?;
self.to_dimacs(&mut writer)
Expand Down Expand Up @@ -211,6 +215,10 @@ impl<VM: ManageVars> MultiOptInstance<VM> {
}

/// Writes the instance to an OPB file at a path
///
/// # Performance
///
/// For performance, consider using a [`std::io::BufWriter`] instance.
pub fn to_opb_path<P: AsRef<Path>>(
self,
path: P,
Expand Down
8 changes: 8 additions & 0 deletions rustsat/src/instances/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,10 @@ impl<VM: ManageVars> OptInstance<VM> {
}

/// Writes the instance to a DIMACS WCNF file at a path
///
/// # Performance
///
/// For performance, consider using a [`std::io::BufWriter`] instance.
pub fn to_dimacs_path<P: AsRef<Path>>(self, path: P) -> Result<(), io::Error> {
let mut writer = fio::open_compressed_uncompressed_write(path)?;
self.to_dimacs(&mut writer)
Expand Down Expand Up @@ -996,6 +1000,10 @@ impl<VM: ManageVars> OptInstance<VM> {
}

/// Writes the instance to an OPB file at a path
///
/// # Performance
///
/// For performance, consider using a [`std::io::BufWriter`] instance.
pub fn to_opb_path<P: AsRef<Path>>(
self,
path: P,
Expand Down
8 changes: 8 additions & 0 deletions rustsat/src/instances/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@ impl<VM: ManageVars> SatInstance<VM> {
}

/// Writes the instance to a DIMACS CNF file at a path
///
/// # Performance
///
/// For performance, consider using a [`std::io::BufWriter`] instance.
pub fn to_dimacs_path<P: AsRef<Path>>(self, path: P) -> Result<(), io::Error> {
let mut writer = fio::open_compressed_uncompressed_write(path)?;
self.to_dimacs(&mut writer)
Expand Down Expand Up @@ -742,6 +746,10 @@ impl<VM: ManageVars> SatInstance<VM> {
}

/// Writes the instance to an OPB file at a path
///
/// # Performance
///
/// For performance, consider using a [`std::io::BufWriter`] instance.
pub fn to_opb_path<P: AsRef<Path>>(
self,
path: P,
Expand Down

0 comments on commit ddd6e61

Please sign in to comment.