Skip to content

Add deque __imul__ method - #3020

Merged
DimitrisJim merged 4 commits into
RustPython:mainfrom
ConnorTroy:connortroy/add-deque-imul
Sep 6, 2021
Merged

DimitrisJim merged 4 commits into
RustPython:mainfrom
ConnorTroy:connortroy/add-deque-imul

Conversation

@ConnorTroy

Copy link
Copy Markdown

Closes #2839

Add deque.__imul__() method

Comment thread vm/src/stdlib/collections.rs Outdated
}

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

Copy link
Copy Markdown
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.

@ConnorTroy ConnorTroy Sep 6, 2021 •

Copy link
Copy Markdown
Author

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?

Comment thread vm/src/stdlib/collections.rs Outdated

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

@DimitrisJim DimitrisJim Sep 6, 2021 •

Copy link
Copy Markdown
Member

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).

@DimitrisJim DimitrisJim left a comment

Copy link
Copy Markdown
Member

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