Skip to content

Commit af5fc7e

Browse files
@facebook-github-bot
authored andcommitted
Replace std HashMap with im HashMap.
Summary: Replace the std HashMap to im::HashMap for HashSet lattice domain, abstract partition and abstract environment. This should give much better performance as it reduces the deep clone. Reviewed By: yuxuanchen1997 Differential Revision: D40204693 fbshipit-source-id: 3b4aca2b3ead1449b2453236bbcabd5224b055ab
1 parent bfbe243 commit af5fc7e

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ edition = "2021"
1010
[dependencies]
1111
smallvec = { version = "1.9.0", features = ["const_generics"] }
1212
disjoint-sets = "0.4.2"
13+
im = "14.2.0"
1314
petgraph = "0.6.2"
1415
sparta-proc-macros = { path = "../rust-proc-macros", version = "0.1.0" }
1516

rust/src/datatype/abstract_environment.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
*/
77

88
use std::borrow::Cow;
9-
use std::collections::HashMap;
109
use std::hash::Hash;
1110

11+
use im::HashMap;
12+
1213
use crate::datatype::bitvec::BitVec;
1314
use crate::datatype::AbstractDomain;
1415
use crate::datatype::PatriciaTreeMap;
@@ -222,8 +223,11 @@ where
222223
(Value(l_map), Value(ref mut r_map)) => {
223224
l_map.retain(|l_k, _| r_map.contains_key(l_k));
224225

225-
for (l_k, l_v) in l_map.iter_mut() {
226-
let r_v = r_map.remove(l_k).unwrap();
226+
// NOTE: implcit prerequisite: key() and iter_mut() get the same order for the same map.
227+
// When `im` crate upgrades to 15.1, we could simply use iter_mut() as it will get both
228+
// keys and values.
229+
let r_vs: Vec<_> = l_map.keys().map(|l_k| r_map.remove(l_k).unwrap()).collect();
230+
for (l_v, r_v) in l_map.iter_mut().zip(r_vs) {
227231
operation(l_v, r_v);
228232
}
229233
l_map.retain(|_, l_v| !l_v.is_top());

rust/src/datatype/abstract_partition.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
*/
77

88
use std::borrow::Cow;
9-
use std::collections::HashMap;
109
use std::hash::Hash;
1110

11+
use im::HashMap;
12+
1213
use crate::datatype::bitvec::BitVec;
1314
use crate::datatype::AbstractDomain;
1415
use crate::datatype::PatriciaTreeMap;
@@ -238,8 +239,8 @@ where
238239
(Value(l_map), Value(mut r_map)) => {
239240
l_map.retain(|l_k, _| r_map.contains_key(l_k));
240241

241-
for (l_k, l_v) in l_map.iter_mut() {
242-
let r_v = r_map.remove(l_k).unwrap();
242+
let r_vs: Vec<_> = l_map.keys().map(|l_k| r_map.remove(l_k).unwrap()).collect();
243+
for (l_v, r_v) in l_map.iter_mut().zip(r_vs) {
243244
operation(l_v, r_v);
244245
}
245246

rust/src/datatype/hash_set_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
use std::collections::HashSet;
98
use std::hash::Hash;
109

10+
use im::HashSet;
11+
1112
use super::powerset::SetAbstractDomainOps;
1213
use super::powerset::SetElementOps;
1314

rust/src/datatype/powerset.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
use std::collections::HashSet;
98
use std::iter::FromIterator;
109

10+
use im::HashSet;
11+
1112
use super::abstract_domain::AbstractDomain;
1213
use crate::datatype::PatriciaTreeSet;
1314

rust/tests/abstract_partition_test.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
*/
77

88
mod abstract_partition_test {
9-
use std::collections::HashSet;
10-
9+
use im::HashSet;
1110
use sparta::datatype::AbstractDomain;
1211
use sparta::datatype::AbstractPartition;
1312
use sparta::datatype::HashMapAbstractPartition;
@@ -65,11 +64,11 @@ mod abstract_partition_test {
6564

6665
assert!(
6766
join.get(&("v2".to_string())).set()
68-
== &HashSet::from(["c".to_string(), "d".to_string()])
67+
== &HashSet::from(vec!["c".to_string(), "d".to_string()])
6968
);
7069
assert!(
7170
join.get(&("v3".to_string())).set()
72-
== &HashSet::from([
71+
== &HashSet::from(vec![
7372
"d".to_string(),
7473
"e".to_string(),
7574
"f".to_string(),
@@ -88,10 +87,10 @@ mod abstract_partition_test {
8887
assert!(meet.leq(&p2));
8988
assert_eq!(meet.len(), 2);
9089

91-
assert!(meet.get(&("v2".to_string())).set() == &HashSet::from(["c".to_string()]));
90+
assert!(meet.get(&("v2".to_string())).set() == &HashSet::from(vec!["c".to_string()]));
9291
assert!(
9392
meet.get(&("v3".to_string())).set()
94-
== &HashSet::from(["d".to_string(), "e".to_string()])
93+
== &HashSet::from(vec!["d".to_string(), "e".to_string()])
9594
);
9695

9796
assert!(p1.clone().meet(Partition::bottom()).is_bottom());
@@ -141,10 +140,10 @@ mod abstract_partition_test {
141140
assert!(join.get(&0) == p2.get(&0));
142141
assert!(join.get(&1) == p1.get(&1));
143142

144-
assert!(join.get(&2).set() == &HashSet::from(["c".to_string(), "d".to_string()]));
143+
assert!(join.get(&2).set() == &HashSet::from(vec!["c".to_string(), "d".to_string()]));
145144
assert!(
146145
join.get(&3).set()
147-
== &HashSet::from([
146+
== &HashSet::from(vec![
148147
"d".to_string(),
149148
"e".to_string(),
150149
"f".to_string(),
@@ -163,8 +162,8 @@ mod abstract_partition_test {
163162
assert!(meet.leq(&p2));
164163
assert_eq!(meet.len(), 2);
165164

166-
assert!(meet.get(&2).set() == &HashSet::from(["c".to_string()]));
167-
assert!(meet.get(&3).set() == &HashSet::from(["d".to_string(), "e".to_string()]));
165+
assert!(meet.get(&2).set() == &HashSet::from(vec!["c".to_string()]));
166+
assert!(meet.get(&3).set() == &HashSet::from(vec!["d".to_string(), "e".to_string()]));
168167

169168
assert!(p1.clone().meet(Partition::bottom()).is_bottom());
170169
assert!(p1.clone().meet(Partition::top()) == p1);

rust/tests/fixpoint_iter_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
1010
mod liveness {
1111
use std::collections::HashMap;
12-
use std::collections::HashSet;
1312

13+
use im::HashSet;
1414
use smallvec::SmallVec;
1515
use sparta::datatype::HashSetAbstractDomain;
1616
use sparta::fixpoint_iter::FixpointIteratorTransformer;

0 commit comments

Comments
 (0)