From ddd6e61f530eebf5f1d3ceec2c4cbdb9ea0708d2 Mon Sep 17 00:00:00 2001 From: Christoph Jabs Date: Tue, 2 Apr 2024 14:07:44 +0300 Subject: [PATCH] perf: add `BufWriter` when writing to file resolves #76 --- rustsat/src/bench.rs | 19 +++++++++++++++++++ rustsat/src/instances/fio.rs | 20 +++++++++++--------- rustsat/src/instances/multiopt.rs | 8 ++++++++ rustsat/src/instances/opt.rs | 8 ++++++++ rustsat/src/instances/sat.rs | 8 ++++++++ 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/rustsat/src/bench.rs b/rustsat/src/bench.rs index 478d5297..7bb79c5a 100644 --- a/rustsat/src/bench.rs +++ b/rustsat/src/bench.rs @@ -154,3 +154,22 @@ mod pb_enc { b.iter(|| build_full_ub::(&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()); + } +} diff --git a/rustsat/src/instances/fio.rs b/rustsat/src/instances/fio.rs index 1fa11360..3ee2cc9f 100644 --- a/rustsat/src/instances/fio.rs +++ b/rustsat/src/instances/fio.rs @@ -43,24 +43,26 @@ pub(crate) fn open_compressed_uncompressed_write>( path: P, ) -> Result, 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))) } diff --git a/rustsat/src/instances/multiopt.rs b/rustsat/src/instances/multiopt.rs index 41ca4a00..53ed0aa3 100644 --- a/rustsat/src/instances/multiopt.rs +++ b/rustsat/src/instances/multiopt.rs @@ -178,6 +178,10 @@ impl MultiOptInstance { } /// 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>(self, path: P) -> Result<(), io::Error> { let mut writer = fio::open_compressed_uncompressed_write(path)?; self.to_dimacs(&mut writer) @@ -211,6 +215,10 @@ impl MultiOptInstance { } /// 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>( self, path: P, diff --git a/rustsat/src/instances/opt.rs b/rustsat/src/instances/opt.rs index 6bfb2608..84bbd64c 100644 --- a/rustsat/src/instances/opt.rs +++ b/rustsat/src/instances/opt.rs @@ -963,6 +963,10 @@ impl OptInstance { } /// 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>(self, path: P) -> Result<(), io::Error> { let mut writer = fio::open_compressed_uncompressed_write(path)?; self.to_dimacs(&mut writer) @@ -996,6 +1000,10 @@ impl OptInstance { } /// 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>( self, path: P, diff --git a/rustsat/src/instances/sat.rs b/rustsat/src/instances/sat.rs index 244930aa..3a475205 100644 --- a/rustsat/src/instances/sat.rs +++ b/rustsat/src/instances/sat.rs @@ -710,6 +710,10 @@ impl SatInstance { } /// 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>(self, path: P) -> Result<(), io::Error> { let mut writer = fio::open_compressed_uncompressed_write(path)?; self.to_dimacs(&mut writer) @@ -742,6 +746,10 @@ impl SatInstance { } /// 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>( self, path: P,