-
Notifications
You must be signed in to change notification settings - Fork 316
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
Initial working example of lending iterator for combinations. #682
Open
Easyoakland
wants to merge
5
commits into
rust-itertools:master
Choose a base branch
from
Easyoakland:initial_lending_iterator_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0eefe16
Initial working example of lending iterator for combinations.
Easyoakland 94857f8
Fixed minor whitespace and comment issues.
Easyoakland 7839cd3
Moved common logic for out.
Easyoakland ce1e22a
Changed parity test to quickcheck.
Easyoakland cdf8507
Comment fix.
Easyoakland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
#![cfg(feature = "lending_iters")] | ||
|
||
use std::collections::{HashSet, VecDeque}; | ||
|
||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use itertools::Itertools; | ||
use itertools::LendingIterator; | ||
|
||
// approximate 100_000 iterations for each combination | ||
const N1: usize = 100_000; | ||
const N2: usize = 448; | ||
const N3: usize = 86; | ||
const N4: usize = 41; | ||
const N14: usize = 21; | ||
|
||
fn comb_lending_c1(c: &mut Criterion) { | ||
c.bench_function("comb lending c1", move |b| { | ||
b.iter(|| { | ||
(0..N1).combinations_lending(1).for_each(|combo| { | ||
black_box({ | ||
let mut out = Vec::with_capacity(1); | ||
out.extend(combo); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_c2(c: &mut Criterion) { | ||
c.bench_function("comb lending c2", move |b| { | ||
b.iter(|| { | ||
(0..N2).combinations_lending(2).for_each(|combo| { | ||
black_box({ | ||
let mut out = Vec::with_capacity(2); | ||
out.extend(combo); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_c3(c: &mut Criterion) { | ||
c.bench_function("comb lending c3", move |b| { | ||
b.iter(|| { | ||
(0..N3).combinations_lending(3).for_each(|combo| { | ||
black_box({ | ||
let mut out = Vec::with_capacity(3); | ||
out.extend(combo); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_c4(c: &mut Criterion) { | ||
c.bench_function("comb lending c4", move |b| { | ||
b.iter(|| { | ||
(0..N4).combinations_lending(4).for_each(|combo| { | ||
black_box({ | ||
let mut out = Vec::with_capacity(4); | ||
out.extend(combo); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_c14(c: &mut Criterion) { | ||
c.bench_function("comb lending c14", move |b| { | ||
b.iter(|| { | ||
(0..N14).combinations_lending(14).for_each(|combo| { | ||
black_box({ | ||
let mut out = Vec::with_capacity(14); | ||
out.extend(combo); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_single_use(c: &mut Criterion) { | ||
c.bench_function("comb lending single use", move |b| { | ||
b.iter(|| { | ||
let mut combination_bitmask = 0usize; | ||
(0..N14).combinations_lending(14).for_each(|combo| { | ||
let compared_bitmask = 0b101010101010101011110000usize; | ||
combo.for_each(|bit_pos| { | ||
combination_bitmask |= 1 << bit_pos; | ||
}); | ||
black_box((combination_bitmask & compared_bitmask).count_ones()); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_into_hash_set_from_collect(c: &mut Criterion) { | ||
c.bench_function("comb lending into hash set from collect", move |b| { | ||
b.iter(|| { | ||
(0..N14).combinations_lending(14).for_each(|combo| { | ||
black_box(combo.collect::<HashSet<_>>()); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_into_hash_set_from_extend(c: &mut Criterion) { | ||
c.bench_function("comb lending into hash set from extend", move |b| { | ||
b.iter(|| { | ||
(0..N14).combinations_lending(14).for_each(|combo| { | ||
black_box({ | ||
let mut out = HashSet::with_capacity(14); | ||
out.extend(combo); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_into_vec_deque_from_collect(c: &mut Criterion) { | ||
c.bench_function("comb lending into vec deque from collect", move |b| { | ||
b.iter(|| { | ||
(0..N14).combinations_lending(14).for_each(|combo| { | ||
black_box(combo.collect::<VecDeque<_>>()); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_into_vec_deque_from_extend(c: &mut Criterion) { | ||
c.bench_function("comb lending into vec deque from extend", move |b| { | ||
b.iter(|| { | ||
(0..N14).combinations_lending(14).for_each(|combo| { | ||
black_box({ | ||
let mut out = VecDeque::with_capacity(14); | ||
out.extend(combo); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_into_slice(c: &mut Criterion) { | ||
c.bench_function("comb lending into slice", move |b| { | ||
b.iter(|| { | ||
(0..N14).combinations_lending(14).for_each(|mut combo| { | ||
black_box({ | ||
let mut out = [0; 14]; | ||
out.fill_with(|| combo.next().unwrap_or_default()); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
fn comb_lending_into_slice_unchecked(c: &mut Criterion) { | ||
c.bench_function("comb lending into slice unchecked", move |b| { | ||
b.iter(|| { | ||
(0..N14).combinations_lending(14).for_each(|mut combo| { | ||
black_box({ | ||
let mut out = [0; 14]; | ||
out.fill_with(|| combo.next().unwrap()); | ||
out | ||
}); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!( | ||
benches, | ||
comb_lending_c1, | ||
comb_lending_c2, | ||
comb_lending_c3, | ||
comb_lending_c4, | ||
comb_lending_c14, | ||
comb_lending_single_use, | ||
comb_lending_into_hash_set_from_collect, | ||
comb_lending_into_hash_set_from_extend, | ||
comb_lending_into_vec_deque_from_collect, | ||
comb_lending_into_vec_deque_from_extend, | ||
comb_lending_into_slice, | ||
comb_lending_into_slice_unchecked, | ||
); | ||
|
||
criterion_main!(benches); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danielhenrymantilla This feature relies heavily on your
lending-iterator
crate. Do you think this would be appropriate functionality tolending-iterator
? I think the performance story presented by the PR is compelling enough that this functionality should be somewhere, but I'm not yet sure where.I share @phimuemue's hesitation for adding a new public dependency to itertools. The bar for adding dependencies, especially public ones, is very high. Itertools only has one dependency (
either
), and it's maintained by a maintainer of itertoolsFirst, we need to be sure that new dependencies will not pose a risk of breakage for our users. At minimum, new dependencies must adhere to the itertools MSRV policy. As a foundational crate in the Rust ecosystem, we treat MSRV increases as breaking changes; our dependencies should do so too. We also maintain a very conservative MSRV of
1.36
; addinglending-iterator
as a dependency would require increasing that to1.57
.Second, we need to be confident that new dependencies (and features, really) will not increase our maintenance burden too much. If we accept this PR, I think it would be reasonable to expect that we will eventually provide lending alternatives of many of our other iterator adapters. I don't know about @phimuemue, but I don't have any experience with
lending-iterator
. We'll need to consider whether this is a road we want itertools to go down.