Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
merged 4 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions 32 vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,37 @@ impl PyByteArray {
}

#[pymethod]
fn lstrip(&self, chars: OptionalOption<PyBytesInner>) -> Self {
self.inner().lstrip(chars).into()
fn lstrip(
zelf: PyRef<Self>,
chars: OptionalOption<PyBytesInner>,
vm: &VirtualMachine,
) -> PyRef<Self> {
let inner = zelf.inner();
let stripped = inner.lstrip(chars);
let elements = &inner.elements;
if stripped == elements {
drop(inner);
zelf
} else {
vm.new_pyref(PyByteArray::from(stripped.to_vec()))
}
}

#[pymethod]
fn rstrip(&self, chars: OptionalOption<PyBytesInner>) -> Self {
self.inner().rstrip(chars).into()
fn rstrip(
zelf: PyRef<Self>,
chars: OptionalOption<PyBytesInner>,
vm: &VirtualMachine,
) -> PyRef<Self> {
let inner = zelf.inner();
let stripped = inner.rstrip(chars);
let elements = &inner.elements;
if stripped == elements {
drop(inner);
zelf
} else {
vm.new_pyref(PyByteArray::from(stripped.to_vec()))
}
}

/// removeprefix($self, prefix, /)
Expand Down
26 changes: 22 additions & 4 deletions 26 vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,31 @@ impl PyBytes {
}

#[pymethod]
fn lstrip(&self, chars: OptionalOption<PyBytesInner>) -> Self {
self.inner.lstrip(chars).into()
fn lstrip(
zelf: PyRef<Self>,
chars: OptionalOption<PyBytesInner>,
vm: &VirtualMachine,
) -> PyRef<Self> {
Comment on lines +369 to +373
Copy link
Member

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 of Vec<u8>, but it may not be easy due to lifetime.

Copy link
Contributor Author

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

Copy link
Contributor Author

@dannasman dannasman Feb 15, 2023

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

Choose a reason for hiding this comment

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

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

let stripped = zelf.inner.lstrip(chars);
if stripped == zelf.as_bytes() {
zelf
} else {
vm.ctx.new_bytes(stripped.to_vec())
}
}

#[pymethod]
fn rstrip(&self, chars: OptionalOption<PyBytesInner>) -> Self {
self.inner.rstrip(chars).into()
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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

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

zelf
} else {
vm.ctx.new_bytes(stripped.to_vec())
}
}

/// removeprefix($self, prefix, /)
Expand Down
28 changes: 12 additions & 16 deletions 28 vm/src/bytesinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,24 +578,20 @@ impl PyBytesInner {
.to_vec()
}

pub fn lstrip(&self, chars: OptionalOption<PyBytesInner>) -> Vec<u8> {
self.elements
.py_strip(
chars,
|s, chars| s.trim_start_with(|c| chars.contains(&(c as u8))),
|s| s.trim_start(),
)
.to_vec()
pub fn lstrip(&self, chars: OptionalOption<PyBytesInner>) -> &[u8] {
self.elements.py_strip(
chars,
|s, chars| s.trim_start_with(|c| chars.contains(&(c as u8))),
|s| s.trim_start(),
)
}

pub fn rstrip(&self, chars: OptionalOption<PyBytesInner>) -> Vec<u8> {
self.elements
.py_strip(
chars,
|s, chars| s.trim_end_with(|c| chars.contains(&(c as u8))),
|s| s.trim_end(),
)
.to_vec()
pub fn rstrip(&self, chars: OptionalOption<PyBytesInner>) -> &[u8] {
self.elements.py_strip(
chars,
|s, chars| s.trim_end_with(|c| chars.contains(&(c as u8))),
|s| s.trim_end(),
)
}

// new in Python 3.9
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.