-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Optimize bytes-like (l|r)strip #4500
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -366,7 +366,11 @@ impl PyBytes { | |||||
| } | ||||||
|
|
||||||
| #[pymethod] | ||||||
| fn lstrip(zelf: PyRef<Self>, chars: OptionalOption<PyBytesInner>, vm: &VirtualMachine) -> PyRef<Self> { | ||||||
| fn lstrip( | ||||||
| zelf: PyRef<Self>, | ||||||
| chars: OptionalOption<PyBytesInner>, | ||||||
| vm: &VirtualMachine, | ||||||
| ) -> PyRef<Self> { | ||||||
| let stripped = zelf.inner.lstrip(chars); | ||||||
| if stripped == zelf.as_bytes().to_vec() { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work?
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||||||
| zelf | ||||||
|
|
@@ -376,7 +380,11 @@ impl PyBytes { | |||||
| } | ||||||
|
|
||||||
| #[pymethod] | ||||||
| fn rstrip(zelf: PyRef<Self>, chars: OptionalOption<PyBytesInner>, vm: &VirtualMachine) -> PyRef<Self> { | ||||||
| fn rstrip( | ||||||
| zelf: PyRef<Self>, | ||||||
| chars: OptionalOption<PyBytesInner>, | ||||||
| vm: &VirtualMachine, | ||||||
| ) -> PyRef<Self> { | ||||||
| let stripped = zelf.inner.rstrip(chars); | ||||||
| if stripped == zelf.as_bytes().to_vec() { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| zelf | ||||||
|
|
||||||
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.
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 ofVec<u8>, but it may not be easy due to lifetime.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.
I see. If the
zelf.inner.lstripreturns&[u8]is it possible to turn&[u8]toVec<u8>without copying the values or is there a implementation for turning&[u8]directly toPyBytesInner? Because otherwisezelf.inner.elementsis unnecessarily copied in the case of duplication.Uh oh!
There was an error while loading. Please reload this page.
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.
Could wrapping the return value of
zelf.inner.lstripinOptionbe an option? In that case it would returnNoneon duplication andSome(stripped)would be returned in other cases.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.
PyBytesInneris a wrapper aroundVec<u8>, so allocation is inevitable. Fortunately, converting aVec<u8>into aPyBytesInneronly takes ownership of thatVec<u8>.