-
Notifications
You must be signed in to change notification settings - Fork 309
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
Implement FlattenOk::{fold, rfold}
#927
Implement FlattenOk::{fold, rfold}
#927
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #927 +/- ##
==========================================
+ Coverage 94.38% 94.55% +0.16%
==========================================
Files 48 48
Lines 6665 6926 +261
==========================================
+ Hits 6291 6549 +258
- Misses 374 377 +3 ☔ View full report in Codecov by Sentry. |
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.
How can it pass tests without using the fields inner_front
and inner_back
?! That's troubling!
EDIT: Note that you can use an unique baseline for running benchmarks.
I agree we should make the tests fail before fixing [r]fold
as this indicates the tests are a bit buggy.
@Philippe-Cholet I was just looking back over the code wondering that myself! Investigating now... Edit: Have to step out for dinner. Will look into this some more later on. I feel as though it shouldn't work. The necessary fix seems pretty simple, but I want to make the tests fail with the current code before I change anything |
FlattenOk::fold
FlattenOk::{fold, rfold}
Ah so the tests fail to capture what's wrong with this code because they use There is a comment there about it being too slow to use itertools/tests/specializations.rs Lines 455 to 456 in dd6a569
I've tested changing over to What's the best approach here do you think? |
e9707bc
to
fc5aab4
Compare
Ah I think I wrote this comment. Use |
So we basically need a light small vector, here is one with max 2 elements. The use quickcheck::Arbitrary;
use rand::Rng;
...
// remove comment
fn flatten_ok(v: Vec<Result<SmallIter2<u8>, char>>) -> () {
...
/// Like `VecIntoIter<T>` with maximum 2 elements.
#[derive(Debug, Clone, Default)]
enum SmallIter2<T> {
#[default]
Zero,
One(T),
Two(T, T),
}
impl<T: Arbitrary> Arbitrary for SmallIter2<T> {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
match g.gen_range(0u8, 3) {
0 => Self::Zero,
1 => Self::One(T::arbitrary(g)),
2 => Self::Two(T::arbitrary(g), T::arbitrary(g)),
_ => unreachable!(),
}
}
// maybe implement shrink too, maybe not
}
impl<T> Iterator for SmallIter2<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match std::mem::take(self) {
Self::Zero => None,
Self::One(val) => Some(val),
Self::Two(val, second) => {
*self = Self::One(second);
Some(val)
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = match self {
Self::Zero => 0,
Self::One(_) => 1,
Self::Two(_, _) => 2,
};
(len, Some(len))
}
}
impl<T> DoubleEndedIterator for SmallIter2<T> {
fn next_back(&mut self) -> Option<Self::Item> {
match std::mem::take(self) {
Self::Zero => None,
Self::One(val) => Some(val),
Self::Two(first, val) => {
*self = Self::One(first);
Some(val)
}
}
}
} |
( |
I thought of |
@Philippe-Cholet Ah neat, thanks for that! Shall I pop that into its own module within |
@kinto-b Just add this in "tests/specializations.rs" where it's needed. We can move it later if we need to. Sidenote: For CI to pass again (because of recent Clippy 1.78), you will need to rebase your branch on master. |
Previously the test used Option<u8> but the coverage was bad. We cannot use Vec<u8> because it is too slow.
fc5aab4
to
568021d
Compare
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.
Looks good to me.
The specialization test did slow us down. An Option
seemed good enough when I wrote it.
I guess the benchmark is up-to-date. -40% is nice enough.
Thanks again!
Thanks! |
Relates to #755
Edit:
.rfold
is an almost identical implementation, so I'll put it through in a moment. Just running benchmarks...Edit: