Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 6 commits into from
Jun 15, 2022
Merged

Conversation

rng-dynamics
Copy link
Contributor

Fix #3787

@rng-dynamics rng-dynamics marked this pull request as draft June 14, 2022 08:27
Comment on lines 79 to 99
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);
}
},
}
},
}
Copy link
Contributor

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 matching 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))
}

Copy link
Member

@youknowone youknowone left a 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.

Comment on lines 68 to 70
match source {
None => {
return Ok(PyIterReturn::StopIteration(None));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Contributor

@fanninpm fanninpm Jun 14, 2022

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.

@rng-dynamics rng-dynamics marked this pull request as ready for review June 14, 2022 18:57
@rng-dynamics
Copy link
Contributor Author

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.

Thank you! The changes looks good. Do you have more works to do? I wonder what made this marked as draft.

The main reason was the clippy test which failed.

Copy link
Member

@youknowone youknowone left a 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!

@youknowone youknowone merged commit cea5a6e into RustPython:main Jun 15, 2022
@youknowone
Copy link
Member

It fixed the ridiculously slow running of test_itertools.py, nice catch and fix!
Now the only slow running is tee

@rng-dynamics rng-dynamics deleted the fix-itertools-chain branch June 15, 2022 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

itertools.chain should evaluate lazily
3 participants