Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize BddVariableSet::new, 2x speedup #62

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions src/_impl_bdd_variable_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,39 @@
///
/// *Panics:* `vars` must contain unique names which are allowed as variable names.
pub fn new(vars: &[&str]) -> BddVariableSet {
let mut builder = BddVariableSetBuilder::new();
builder.make_variables(vars);
builder.build()
let num_vars = vars.len();
if num_vars >= ((u16::MAX - 1) as usize) {
panic!(

Check warning on line 31 in src/_impl_bdd_variable_set.rs

View check run for this annotation

Codecov / codecov/patch

src/_impl_bdd_variable_set.rs#L31

Added line #L31 was not covered by tests
"Too many BDD variables. There can be at most {} variables.",
u16::MAX - 1
)
}
let var_names: Vec<String> = vars
.iter()
.map(|name| {
if name.chars().any(|c| NOT_IN_VAR_NAME.contains(&c)) {
panic!(

Check warning on line 40 in src/_impl_bdd_variable_set.rs

View check run for this annotation

Codecov / codecov/patch

src/_impl_bdd_variable_set.rs#L40

Added line #L40 was not covered by tests
"Variable name {} is invalid. Cannot use {:?}",
name, NOT_IN_VAR_NAME
);
}
name.to_string()
})
.collect();
let var_index_mapping: HashMap<String, u16> = vars
.iter()
.enumerate()
.map(|(id, name)| (name.to_string(), id as u16))
.collect();

if var_index_mapping.len() != var_names.len() {
panic!("Existing duplicated BDD variable.");

Check warning on line 55 in src/_impl_bdd_variable_set.rs

View check run for this annotation

Codecov / codecov/patch

src/_impl_bdd_variable_set.rs#L55

Added line #L55 was not covered by tests
}
BddVariableSet {
num_vars: num_vars as u16,
var_names,
var_index_mapping,
}
}

/// Return the number of variables in this set.
Expand Down Expand Up @@ -591,4 +621,35 @@
let ctx = BddVariableSet::new(&[]);
assert_eq!("[]", ctx.to_string());
}

#[test]
fn bdd_new_performance() {
fn old_version(vars: &[&str]) -> BddVariableSet {
let mut builder = BddVariableSetBuilder::new();
builder.make_variables(vars);
builder.build()
}
// validate correctness
let ctx = BddVariableSet::new(&["a", "b", "x", "c", "y"]);
let ctx_old = old_version(&["a", "b", "x", "c", "y"]);
assert_eq!(ctx.num_vars, ctx_old.num_vars);
assert_eq!(ctx.var_names, ctx_old.var_names);
assert_eq!(ctx.var_index_mapping, ctx_old.var_index_mapping);
// validate performance
use std::time::SystemTime;
let n = 1000;
let t_bgn1 = SystemTime::now();
for _ in 0..n {
let _ctx = BddVariableSet::new(&["a", "b", "x", "c", "y"]);
}
let t1 = t_bgn1.elapsed().unwrap();
println!("New BddVariableSet::new runtime: {t1:?}");
let t_bgn2 = SystemTime::now();
for _ in 0..n {
let _ctx = old_version(&["a", "b", "x", "c", "y"]);
}
let t2 = t_bgn2.elapsed().unwrap();
println!("Old BddVariableSet::new runtime: {t2:?}");
assert!(t1 < t2);
}
}
Loading