Skip to content

Commit

Permalink
Merge pull request #263 from hatoo/print-size-all
Browse files Browse the repository at this point in the history
Support `(print-size)` to print all size of functions
  • Loading branch information
oflatt authored Oct 20, 2023
2 parents 959afb9 + b42eab1 commit c7405d8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub enum NCommand {
Check(Vec<NormFact>),
CheckProof,
PrintTable(Symbol, usize),
PrintSize(Symbol),
PrintSize(Option<Symbol>),
Output {
file: String,
exprs: Vec<Expr>,
Expand Down Expand Up @@ -642,8 +642,8 @@ pub enum Command {
/// prints the first 20 rows of the `Add` function.
///
PrintFunction(Symbol, usize),
/// Print out the number of rows in a function.
PrintSize(Symbol),
/// Print out the number of rows in a function or all functions.
PrintSize(Option<Symbol>),
/// Input a CSV file directly into a function.
Input {
name: Symbol,
Expand Down Expand Up @@ -699,7 +699,7 @@ impl ToSexp for Command {
Command::Push(n) => list!("push", n),
Command::Pop(n) => list!("pop", n),
Command::PrintFunction(name, n) => list!("print-function", name, n),
Command::PrintSize(name) => list!("print-size", name),
Command::PrintSize(name) => list!("print-size", ++ name),
Command::Input { name, file } => list!("input", name, format!("\"{}\"", file)),
Command::Output { file, exprs } => list!("output", format!("\"{}\"", file), ++ exprs),
Command::Fail(cmd) => list!("fail", cmd),
Expand Down
2 changes: 1 addition & 1 deletion src/ast/parse.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Command: Command = {
LParen "push" <UNum?> RParen => Command::Push(<>.unwrap_or(1)),
LParen "pop" <UNum?> RParen => Command::Pop(<>.unwrap_or(1)),
LParen "print-function" <sym:Ident> <n:UNum> RParen => Command::PrintFunction(sym, n),
LParen "print-size" <sym:Ident> RParen => Command::PrintSize(sym),
LParen "print-size" <sym:Ident?> RParen => Command::PrintSize(sym),
LParen "input" <name:Ident> <file:String> RParen => Command::Input { name, file },
LParen "output" <file:String> <exprs:Expr+> RParen => Command::Output { file, exprs },
LParen "fail" <Command> RParen => Command::Fail(Box::new(<>)),
Expand Down
35 changes: 30 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,36 @@ impl EGraph {
Ok(())
}

pub fn print_size(&mut self, sym: Symbol) -> Result<(), Error> {
let f = self.functions.get(&sym).ok_or(TypeError::Unbound(sym))?;
log::info!("Function {} has size {}", sym, f.nodes.len());
self.print_msg(f.nodes.len().to_string());
Ok(())
pub fn print_size(&mut self, sym: Option<Symbol>) -> Result<(), Error> {
if let Some(sym) = sym {
let f = self.functions.get(&sym).ok_or(TypeError::Unbound(sym))?;
log::info!("Function {} has size {}", sym, f.nodes.len());
self.print_msg(f.nodes.len().to_string());
Ok(())
} else {
// Print size of all functions
let mut lens = self
.functions
.iter()
.map(|(sym, f)| (*sym, f.nodes.len()))
.collect::<Vec<_>>();

// Function name's alphabetical order
lens.sort_by_key(|(name, _)| name.as_str());

for (sym, len) in &lens {
log::info!("Function {} has size {}", sym, len);
}

self.print_msg(
lens.into_iter()
.map(|(name, len)| format!("{}: {}", name, len))
.collect::<Vec<_>>()
.join("\n"),
);

Ok(())
}
}

// returns whether the egraph was updated
Expand Down
2 changes: 2 additions & 0 deletions tests/math-microbenchmark.egg
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@
(print-size Add)
(print-size Mul)

(print-size)

(print-stats)

0 comments on commit c7405d8

Please sign in to comment.