-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix itertools chain #3788
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
Fix itertools chain #3788
Conversation
vm/src/stdlib/itertools.rs
Outdated
cached_iter | ||
} else { | ||
// Someone changed cached iter to None since we checked. | ||
continue; | ||
}; | ||
|
||
// We need to call "next" outside of the lock. | ||
match cur_iter.next(vm) { | ||
Ok(PyIterReturn::Return(ok)) => return Ok(PyIterReturn::Return(ok)), | ||
Ok(PyIterReturn::StopIteration(_)) => { | ||
zelf.cur_idx.fetch_add(1); | ||
*zelf.cached_iter.write() = None; | ||
} | ||
Err(err) => { | ||
return Err(err); | ||
let next = || { | ||
let source = zelf.source.read().clone(); | ||
match source { | ||
None => { | ||
return Ok(PyIterReturn::StopIteration(None)); | ||
} | ||
Some(source) => loop { | ||
let active = zelf.active.read().clone(); | ||
match active { | ||
None => match source.next(vm) { | ||
Ok(PyIterReturn::Return(ok)) => { | ||
*zelf.active.write() = Some(ok.get_iter(vm)?); | ||
} | ||
Ok(PyIterReturn::StopIteration(_)) => { | ||
return Ok(PyIterReturn::StopIteration(None)); | ||
} | ||
Err(err) => { | ||
return Err(err); | ||
} | ||
}, | ||
Some(active) => match active.next(vm) { | ||
Ok(PyIterReturn::Return(ok)) => { | ||
return Ok(PyIterReturn::Return(ok)); | ||
} | ||
Ok(PyIterReturn::StopIteration(_)) => { | ||
*zelf.active.write() = None; | ||
} | ||
Err(err) => { | ||
return Err(err); | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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.
📎 "It looks like you're match
ing variants of an Option
. Would you like help?" 📎
if let Some(source) = zelf.source.read().clone() {
loop {
if let Some(active) = zelf.active.read().clone() {
match active.next(vm) {
Ok(PyIterReturn::Return(ok)) => {
return Ok(PyIterReturn::Return(ok));
}
Ok(PyIterReturn::StopIteration(_)) => {
*zelf.active.write() = None;
}
Err(err) => {
return Err(err);
}
}
} else {
match source.next(vm) {
Ok(PyIterReturn::Return(ok)) => {
*zelf.active.write() = Some(ok.get_iter(vm)?);
}
Ok(PyIterReturn::StopIteration(_)) => {
return Ok(PyIterReturn::StopIteration(None));
}
Err(err) => {
return Err(err);
}
}
}
}
} else {
Ok(PyIterReturn::StopIteration(None))
}
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.
Thank you! The changes looks good.
Do you have more works to do? I wonder what made this marked as draft.
vm/src/stdlib/itertools.rs
Outdated
match source { | ||
None => { | ||
return Ok(PyIterReturn::StopIteration(None)); |
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.
match source { | |
None => { | |
return Ok(PyIterReturn::StopIteration(None)); | |
let source = if let Some(source) = source { | |
source | |
} else { | |
return Ok(PyIterReturn::StopIteration(None)) | |
}; |
I have different suggestion to @fanninpm's one. Let's keep indent depth shallower
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.
@youknowone that suggestion has a few syntax errors in lines 68 and 71.
EDIT: I stand corrected regarding line 68.
Thanks for the suggestions. I tried to incorporate them and I tried to reduce the nesting. I also removed the closure which I didn't like that much. Let me know what you think. If you have more suggestions, I'd be happy to hear them.
The main reason was the clippy test which failed. |
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, thank you!
It fixed the ridiculously slow running of |
Fix #3787