Skip to content

Commit

Permalink
refactor: Remove clone in ExtensionSet::union (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
croyzor authored Mar 5, 2024
1 parent 6be3ca2 commit c02ab92
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> DFGBuilder<T> {
parent,
NodeType::new(
output,
input_extensions.map(|inp| inp.union(&signature.extension_reqs)),
input_extensions.map(|inp| inp.union(signature.extension_reqs)),
),
)?;

Expand Down Expand Up @@ -430,7 +430,7 @@ pub(crate) mod test {
let xb: ExtensionId = "B".try_into().unwrap();
let xc: ExtensionId = "C".try_into().unwrap();
let ab_extensions = ExtensionSet::from_iter([xa.clone(), xb.clone()]);
let abc_extensions = ab_extensions.clone().union(&xc.clone().into());
let abc_extensions = ab_extensions.clone().union(xc.clone().into());

let parent_sig =
FunctionType::new(type_row![BIT], type_row![BIT]).with_extension_delta(abc_extensions);
Expand Down
4 changes: 2 additions & 2 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ impl ExtensionSet {
}

/// Returns the union of two extension sets.
pub fn union(mut self, other: &Self) -> Self {
self.0.extend(other.0.iter().cloned());
pub fn union(mut self, other: Self) -> Self {
self.0.extend(other.0);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/extension/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ impl UnificationContext {
// to a set which already contained it.
Constraint::Plus(r, other_meta) => {
if let Some(rs) = self.get_solution(other_meta) {
let rrs = rs.clone().union(r);
let rrs = rs.clone().union(r.clone());
match self.get_solution(&meta) {
// Let's check that this is right?
Some(rs) => {
Expand Down Expand Up @@ -725,7 +725,7 @@ impl UnificationContext {
.iter()
.flat_map(|m| self.get_constraints(m).unwrap())
.filter_map(|c| match c {
Constraint::Plus(_, other_m) => solutions.get(&self.resolve(*other_m)),
Constraint::Plus(_, other_m) => solutions.get(&self.resolve(*other_m)).cloned(),
Constraint::Equal(_) => None,
})
.fold(ExtensionSet::new(), ExtensionSet::union);
Expand Down
11 changes: 7 additions & 4 deletions src/extension/infer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn dangling_src() -> Result<(), Box<dyn Error>> {
assert_eq!(hugr.get_nodetype(src.node()).io_extensions().unwrap().1, rs);
assert_eq!(
hugr.get_nodetype(mult.node()).io_extensions().unwrap(),
(&rs.clone(), rs)
(rs.clone(), rs)
);
Ok(())
}
Expand Down Expand Up @@ -366,11 +366,11 @@ fn test_conditional_inference() -> Result<(), Box<dyn Error>> {
for node in [case0_node, case1_node, conditional_node] {
assert_eq!(
hugr.get_nodetype(node).io_extensions().unwrap().0,
&ExtensionSet::new()
ExtensionSet::new()
);
assert_eq!(
hugr.get_nodetype(node).io_extensions().unwrap().0,
&ExtensionSet::new()
ExtensionSet::new()
);
}
Ok(())
Expand Down Expand Up @@ -724,7 +724,10 @@ fn make_looping_cfg(
bb1_ext: ExtensionSet,
bb2_ext: ExtensionSet,
) -> Result<Hugr, Box<dyn Error>> {
let hugr_delta = entry_ext.clone().union(&bb1_ext).union(&bb2_ext);
let hugr_delta = entry_ext
.clone()
.union(bb1_ext.clone())
.union(bb2_ext.clone());

let mut hugr = Hugr::new(NodeType::new_open(ops::CFG {
signature: FunctionType::new(type_row![NAT], type_row![NAT])
Expand Down
2 changes: 1 addition & 1 deletion src/extension/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ExtensionValidator {
.map(|s| s.extension_reqs)
.unwrap_or_default();

let outgoing_sol = extension_reqs.union(&incoming_sol);
let outgoing_sol = extension_reqs.union(incoming_sol.clone());

extensions.insert((node, Direction::Incoming), incoming_sol);
extensions.insert((node, Direction::Outgoing), outgoing_sol);
Expand Down
6 changes: 3 additions & 3 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ impl NodeType {
///
/// `None`` if the [Self::input_extensions] is `None`.
/// Otherwise, will return Some, with the output extensions computed from the node's delta
pub fn io_extensions(&self) -> Option<(&ExtensionSet, ExtensionSet)> {
pub fn io_extensions(&self) -> Option<(ExtensionSet, ExtensionSet)> {
self.input_extensions
.as_ref()
.map(|e| (e, self.op.extension_delta().union(e)))
.clone()
.map(|e| (e.clone(), self.op.extension_delta().union(e)))
}

/// Gets the underlying [OpType] i.e. without any [input_extensions]
Expand Down
2 changes: 1 addition & 1 deletion src/hugr/rewrite/outline_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl OutlineCfg {
}
}
}
extension_delta = extension_delta.union(&o.signature().extension_reqs);
extension_delta = extension_delta.union(o.signature().extension_reqs);
let external_succs = h.output_neighbours(n).filter(|s| !self.blocks.contains(s));
match external_succs.at_most_one() {
Ok(None) => (), // No external successors
Expand Down
2 changes: 1 addition & 1 deletion src/std_extensions/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl CustomConst for ListValue {

fn extension_reqs(&self) -> ExtensionSet {
ExtensionSet::union_over(self.0.iter().map(Value::extension_reqs))
.union(&(EXTENSION_NAME.into()))
.union(EXTENSION_NAME.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct FunctionType {
impl FunctionType {
/// Builder method, add extension_reqs to an FunctionType
pub fn with_extension_delta(mut self, rs: impl Into<ExtensionSet>) -> Self {
self.extension_reqs = self.extension_reqs.union(&rs.into());
self.extension_reqs = self.extension_reqs.union(rs.into());
self
}

Expand Down

0 comments on commit c02ab92

Please sign in to comment.