Optimize bytes-like (l|r)strip by dannasman · Pull Request #4500 · RustPython/RustPython
Comment on lines +369 to +373
| fn lstrip( | ||
| zelf: PyRef<Self>, | ||
| chars: OptionalOption<PyBytesInner>, | ||
| vm: &VirtualMachine, | ||
| ) -> PyRef<Self> { |
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.
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.
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.
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>.