diff --git a/lib/src/merge.rs b/lib/src/merge.rs index 6cf1bcdfa..d55a12469 100644 --- a/lib/src/merge.rs +++ b/lib/src/merge.rs @@ -154,16 +154,14 @@ impl Merge { removes: impl IntoIterator, adds: impl IntoIterator, ) -> Merge> { - // TODO: no need to build intermediate removes/adds vecs - let mut removes = removes.into_iter().map(Some).collect_vec(); - let mut adds = adds.into_iter().map(Some).collect_vec(); - while removes.len() + 1 < adds.len() { - removes.push(None); + let removes = removes.into_iter(); + let mut adds = adds.into_iter().fuse(); + let mut values = smallvec_inline![adds.next()]; + for diff in removes.zip_longest(adds) { + let (remove, add) = diff.map_any(Some, Some).or_default(); + values.extend([remove, add]); } - while adds.len() < removes.len() + 1 { - adds.push(None); - } - Merge::new(removes, adds) + Merge { values } } /// The removed values, also called negative terms.