FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Optimize bytes-like (l|r)strip by dannasman · Pull Request #4500 · RustPython/RustPython · GitHub

Optimize bytes-like (l|r)strip - #4500

Merged
youknowone merged 4 commits into
RustPython:mainfrom
dannasman:bytes_lrstrip
Feb 17, 2023
Merged

Optimize bytes-like (l|r)strip#4500
youknowone merged 4 commits into
RustPython:mainfrom
dannasman:bytes_lrstrip

Conversation

Copy link
Copy Markdown
Contributor

Hi!

Here is a solution proposal to #4497. I was not quite sure what the optimization part of #4496 was (I assumed it was the if-else part).

DimitrisJim linked an issue Feb 14, 2023 that may be closed by this pull request
Comment thread vm/src/builtins/bytes.rs
Comment on lines +369 to +373
fn lstrip(
zelf: PyRef<Self>,
chars: OptionalOption<PyBytesInner>,
vm: &VirtualMachine,
) -> PyRef<Self> {

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I found bytes.lstrip has different issue to str.lstrip. zelf.inner.lstrip already returns Vec<u8>, which means unnecessary copy happened.
I think this duplication check need to be done inside zelf.inner.lstip. otherwise zelf.inner.lstip also could return &[u8] instead of Vec<u8>, but it may not be easy due to lifetime.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I see. If the zelf.inner.lstrip returns &[u8] is it possible to turn &[u8] to Vec<u8> without copying the values or is there a implementation for turning &[u8] directly to PyBytesInner? Because otherwise zelf.inner.elements is unnecessarily copied in the case of duplication.

dannasman Feb 15, 2023
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Could wrapping the return value of zelf.inner.lstrip in Option be an option? In that case it would return None on duplication and Some(stripped) would be returned in other cases.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

PyBytesInner is a wrapper around Vec<u8>, so allocation is inevitable. Fortunately, converting a Vec<u8> into a PyBytesInner only takes ownership of that Vec<u8>.

fanninpm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Does this work? I don't think we need the explicit conversion in these two places, as PartialEq<&[U]> is implemented for Vec<T, A>.

Comment thread vm/src/builtins/bytes.rs Outdated
vm: &VirtualMachine,
) -> PyRef<Self> {
let stripped = zelf.inner.lstrip(chars);
if stripped == zelf.as_bytes().to_vec() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Does this work?

Suggested change
if stripped == zelf.as_bytes().to_vec() {
if stripped == zelf.as_bytes() {

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think &stripped == zelf.as_bytes() works, but still stripped is Vec<u8>.

Comment thread vm/src/builtins/bytes.rs
vm: &VirtualMachine,
) -> PyRef<Self> {
let stripped = zelf.inner.rstrip(chars);
if stripped == zelf.as_bytes().to_vec() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
if stripped == zelf.as_bytes().to_vec() {
if stripped == zelf.as_bytes() {

dannasman requested review from fanninpm and youknowone and removed request for fanninpm and youknowone February 17, 2023 08:22

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Great, Thank you!

youknowone merged commit a0c34da into RustPython:main Feb 17, 2023
dannasman deleted the bytes_lrstrip branch February 17, 2023 20:27
itsankitkp pushed a commit to itsankitkp/RustPython that referenced this pull request Feb 19, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Optimize bytes-like (l|r)strip

3 participants


Back | FazBrowse Home | New Git URL