diff --git a/benches/example_benchmarks.rs b/benches/example_benchmarks.rs index e733986ae..583975dad 100644 --- a/benches/example_benchmarks.rs +++ b/benches/example_benchmarks.rs @@ -1,8 +1,12 @@ use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion}; use egglog::EGraph; -fn run_example(filename: &str, program: &str) { - EGraph::default() +fn run_example(filename: &str, program: &str, no_messages: bool) { + let mut egraph = EGraph::default(); + if no_messages { + egraph.disable_messages(); + } + egraph .parse_and_run_program(Some(filename.to_owned()), program) .unwrap(); } @@ -17,7 +21,10 @@ pub fn criterion_benchmark(c: &mut Criterion) { let name = path.file_stem().unwrap().to_string_lossy().to_string(); let filename = path.to_string_lossy().to_string(); let program = std::fs::read_to_string(&filename).unwrap(); - c.bench_function(&name, |b| b.iter(|| run_example(&filename, &program))); + let no_messages = path_string.contains("no-messages"); + c.bench_function(&name, |b| { + b.iter(|| run_example(&filename, &program, no_messages)) + }); } } diff --git a/src/actions.rs b/src/actions.rs index 491317724..6053884a6 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -339,9 +339,11 @@ impl EGraph { let variants = values[1].bits as i64; if variants == 0 { let (cost, term) = self.extract(values[0], &mut termdag, sort); - let extracted = termdag.to_string(&term); - log::info!("extracted with cost {cost}: {extracted}"); - self.print_msg(extracted); + if self.messages_enabled() { + let extracted = termdag.to_string(&term); + log::info!("extracted with cost {cost}: {extracted}"); + self.print_msg(extracted); + } self.extract_report = Some(ExtractReport::Best { termdag, cost, @@ -353,17 +355,19 @@ impl EGraph { } let terms = self.extract_variants(sort, values[0], variants as usize, &mut termdag); - log::info!("extracted variants:"); - let mut msg = String::default(); - msg += "(\n"; - assert!(!terms.is_empty()); - for expr in &terms { - let str = termdag.to_string(expr); - log::info!(" {str}"); - msg += &format!(" {str}\n"); + if self.messages_enabled() { + log::info!("extracted variants:"); + let mut msg = String::default(); + msg += "(\n"; + assert!(!terms.is_empty()); + for expr in &terms { + let str = termdag.to_string(expr); + log::info!(" {str}"); + msg += &format!(" {str}\n"); + } + msg += ")"; + self.print_msg(msg); } - msg += ")"; - self.print_msg(msg); self.extract_report = Some(ExtractReport::Variants { termdag, terms }); } diff --git a/src/lib.rs b/src/lib.rs index df969de64..9c7c28a7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -443,7 +443,8 @@ pub struct EGraph { recent_run_report: Option, /// The run report unioned over all runs so far. overall_run_report: RunReport, - msgs: Vec, + /// Messages to be printed to the user. If this is `None`, then we are ignoring messages. + msgs: Option>, } impl Default for EGraph { @@ -463,7 +464,7 @@ impl Default for EGraph { extract_report: None, recent_run_report: None, overall_run_report: Default::default(), - msgs: Default::default(), + msgs: Some(vec![]), type_info: Default::default(), }; egraph @@ -495,6 +496,23 @@ impl EGraph { self.egraphs.push(self.clone()); } + /// Disable saving messages to be printed to the user and remove any saved messages. + /// + /// When messages are disabled the vec of messages returned by evaluating commands will always be empty. + pub fn disable_messages(&mut self) { + self.msgs = None; + } + + /// Enable saving messages to be printed to the user. + pub fn enable_messages(&mut self) { + self.msgs = Some(vec![]); + } + + /// Whether messages are enabled. + pub fn messages_enabled(&self) -> bool { + self.msgs.is_some() + } + /// Pop the current egraph off the stack, replacing /// it with the previously pushed egraph. /// It preserves the run report and messages from the popped @@ -1503,12 +1521,17 @@ impl EGraph { } pub(crate) fn print_msg(&mut self, msg: String) { - self.msgs.push(msg); + if let Some(ref mut msgs) = self.msgs { + msgs.push(msg); + } } fn flush_msgs(&mut self) -> Vec { - self.msgs.dedup_by(|a, b| a.is_empty() && b.is_empty()); - std::mem::take(&mut self.msgs) + if let Some(ref mut msgs) = self.msgs { + msgs.dedup_by(|a, b| a.is_empty() && b.is_empty()); + return std::mem::take(msgs); + } + return vec![]; } } diff --git a/src/main.rs b/src/main.rs index c8cb3ab16..dfb294497 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,6 +105,9 @@ fn main() { egraph.fact_directory.clone_from(&args.fact_directory); egraph.seminaive = !args.naive; egraph.run_mode = args.show; + if args.dont_print_messages { + egraph.disable_messages(); + } egraph }; @@ -152,10 +155,8 @@ fn main() { let mut egraph = mk_egraph(); match egraph.parse_and_run_program(Some(input.to_str().unwrap().into()), &program) { Ok(msgs) => { - if !args.dont_print_messages { - for msg in msgs { - println!("{msg}"); - } + for msg in msgs { + println!("{msg}"); } } Err(err) => { diff --git a/tests/extract-vec-bench.egg b/tests/extract-vec-bench-no-messages.egg similarity index 100% rename from tests/extract-vec-bench.egg rename to tests/extract-vec-bench-no-messages.egg diff --git a/tests/files.rs b/tests/files.rs index 3caf88140..0c1f04262 100644 --- a/tests/files.rs +++ b/tests/files.rs @@ -40,6 +40,9 @@ impl Run { fn test_program(&self, filename: Option, program: &str, message: &str) { let mut egraph = EGraph::default(); + if self.no_messages() { + egraph.disable_messages(); + } egraph.set_reserved_symbol("___".into()); match egraph.parse_and_run_program(filename, program) { Ok(msgs) => { @@ -100,6 +103,10 @@ impl Run { fn should_fail(&self) -> bool { self.path.to_string_lossy().contains("fail-typecheck") } + + fn no_messages(&self) -> bool { + self.path.to_string_lossy().contains("no-messages") + } } fn generate_tests(glob: &str) -> Vec {