Skip to content

Add deque __imul__ method #3020

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 4 commits into from
Sep 6, 2021

Conversation

ConnorTroy
Copy link
Contributor

Closes #2839

Add deque.__imul__() method

@@ -59,9 +59,9 @@ mod _collections {
}
}

struct SimpleSeqDeque<'a>(PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>);
struct SimpleSeqDeque(VecDeque<PyObjectRef>);
Copy link
Member

Choose a reason for hiding this comment

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

This is designed to borrow VecDeque but after this change, it will be cloned to make this struct.

Copy link
Contributor Author

@ConnorTroy ConnorTroy Sep 6, 2021

Choose a reason for hiding this comment

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

I switched to clone because it seemed like the easiest way to implement for the write guard as well. If you want I can switch to struct SimpleSeqDeque<'a>(&'a VecDeque<PyObjectRef>); -- or do you know if there is a trait that PyRwLockReadGuard and PyRwLockWriteGuard implement which I can use?

@@ -471,6 +471,23 @@ mod _collections {
})
}

#[pymethod(magic)]
fn imul(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let mut new_deque: VecDeque<PyObjectRef> = {
Copy link
Member

@DimitrisJim DimitrisJim Sep 6, 2021

Choose a reason for hiding this comment

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

No reason to repeat ourselves here though, right? Either imul calls mul or mul calls imul, both can work. CPython has mul call into imul after doing a copy. For now, this can just call into mul and then swap the deques. We can time it at some later point to see if the opposite yields any better results (though I'm guessing the difference would be small to negligible).

Copy link
Member

@DimitrisJim DimitrisJim left a comment

Choose a reason for hiding this comment

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

lgtm! thanks!

@DimitrisJim DimitrisJim merged commit e485141 into RustPython:main Sep 6, 2021
@ConnorTroy ConnorTroy deleted the connortroy/add-deque-imul branch September 6, 2021 19:36
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.

collections.deque doesn't have __imul__ method.
3 participants