Skip to content

Commit

Permalink
Try skipping messages for this test and not serializing termdags
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Dec 13, 2024
1 parent 5fb127d commit a075e4b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 25 deletions.
13 changes: 10 additions & 3 deletions benches/example_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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))
});
}
}

Expand Down
30 changes: 17 additions & 13 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 });
}

Expand Down
33 changes: 28 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ pub struct EGraph {
recent_run_report: Option<RunReport>,
/// The run report unioned over all runs so far.
overall_run_report: RunReport,
msgs: Vec<String>,
/// Messages to be printed to the user. If this is `None`, then we are ignoring messages.
msgs: Option<Vec<String>>,
}

impl Default for EGraph {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> {
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![];
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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) => {
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ impl Run {

fn test_program(&self, filename: Option<String>, 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) => {
Expand Down Expand Up @@ -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<Trial> {
Expand Down

0 comments on commit a075e4b

Please sign in to comment.