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

Specialize "loops of next" #818

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion benches/bench1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use itertools::free::cloned;
use itertools::iproduct;
use itertools::Itertools;
Expand Down Expand Up @@ -648,6 +648,22 @@ fn step_range_10(c: &mut Criterion) {
});
}

fn vec_iter_mut_partition(c: &mut Criterion) {
let data = std::iter::repeat(-1024i32..1024)
.take(256)
.flatten()
.collect_vec();
c.bench_function("vec iter mut partition", move |b| {
b.iter_batched(
|| data.clone(),
|mut data| {
black_box(itertools::partition(black_box(&mut data), |n| *n >= 0));
},
BatchSize::LargeInput,
)
});
}

fn cartesian_product_iterator(c: &mut Criterion) {
let xs = vec![0; 16];

Expand Down Expand Up @@ -803,6 +819,7 @@ criterion_group!(
step_vec_10,
step_range_2,
step_range_10,
vec_iter_mut_partition,
cartesian_product_iterator,
multi_cartesian_product_iterator,
cartesian_product_nested_for,
Expand Down
32 changes: 10 additions & 22 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,17 +901,11 @@ where
type Item = Result<T, E>;

fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
Some(Ok(v)) => {
if (self.f)(&v) {
return Some(Ok(v));
}
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
let f = &mut self.f;
self.iter.find(|res| match res {
Ok(t) => f(t),
_ => true,
})
}

fn size_hint(&self) -> (usize, Option<usize>) {
Expand Down Expand Up @@ -988,17 +982,11 @@ where
type Item = Result<U, E>;

fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
Some(Ok(v)) => {
if let Some(v) = (self.f)(v) {
return Some(Ok(v));
}
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
let f = &mut self.f;
self.iter.find_map(|res| match res {
Ok(t) => f(t).map(Ok),
Err(e) => Some(Err(e)),
})
}

fn size_hint(&self) -> (usize, Option<usize>) {
Expand Down
15 changes: 4 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4054,18 +4054,11 @@ where
{
let mut split_index = 0;
let mut iter = iter.into_iter();
'main: while let Some(front) = iter.next() {
while let Some(front) = iter.next() {
if !pred(front) {
loop {
match iter.next_back() {
Some(back) => {
if pred(back) {
std::mem::swap(front, back);
break;
}
}
None => break 'main,
}
match iter.rfind(|back| pred(back)) {
Some(back) => std::mem::swap(front, back),
None => break,
}
}
split_index += 1;
Expand Down
36 changes: 14 additions & 22 deletions src/unique_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,8 @@ where
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.next() {
let key = (self.f)(&v);
if self.used.insert(key, ()).is_none() {
return Some(v);
}
}
None
let Self { iter, used, f } = self;
iter.find(|v| used.insert(f(v), ()).is_none())
}

#[inline]
Expand All @@ -89,13 +84,8 @@ where
F: FnMut(&I::Item) -> V,
{
fn next_back(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.next_back() {
let key = (self.f)(&v);
if self.used.insert(key, ()).is_none() {
return Some(v);
}
}
None
let Self { iter, used, f } = self;
iter.rfind(|v| used.insert(f(v), ()).is_none())
}
}

Expand All @@ -115,14 +105,15 @@ where
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.iter.next() {
if let Entry::Vacant(entry) = self.iter.used.entry(v) {
let UniqueBy { iter, used, .. } = &mut self.iter;
iter.find_map(|v| {
if let Entry::Vacant(entry) = used.entry(v) {
let elt = entry.key().clone();
entry.insert(());
return Some(elt);
}
}
None
None
})
}

#[inline]
Expand All @@ -142,14 +133,15 @@ where
I::Item: Eq + Hash + Clone,
{
fn next_back(&mut self) -> Option<Self::Item> {
while let Some(v) = self.iter.iter.next_back() {
if let Entry::Vacant(entry) = self.iter.used.entry(v) {
let UniqueBy { iter, used, .. } = &mut self.iter;
iter.rev().find_map(|v| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-topic: Is it strange that there's rfind, but no rfind_map?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not off-topic to me. I was wondering the same question while writing this.
Maybe there is no large performance gain to expect compare to .rev().method(). I searched a bit but did not find anything.
More generally, DoubleEndedIterator does not have a lot of methods.

if let Entry::Vacant(entry) = used.entry(v) {
let elt = entry.key().clone();
entry.insert(());
return Some(elt);
}
}
None
None
})
}
}

Expand Down