Repository navigation
de: fix bytebuf - #131
de: fix bytebuf#131Finomnis wants to merge 3 commits into
Conversation
davidhewitt
left a comment
There was a problem hiding this comment.
Thanks - suggested a simplification that will make the extraction order "standard"
| if let Ok(b) = self.input.cast::<PyByteArray>() { | ||
| visitor.visit_byte_buf(b.to_vec()) | ||
| } else { | ||
| let b = self.input.cast::<PyBytes>()?; | ||
| visitor.visit_bytes(b.as_bytes()) | ||
| } |
There was a problem hiding this comment.
I think we should do self.input.extract::<PyBackedBytes>(), using https://docs.rs/pyo3/latest/pyo3/pybacked/struct.PyBackedBytes.html as the intermediate - this'll handle both cases in a "standard" way.
There was a problem hiding this comment.
Sadly won't get to work on this for a couple of days, feel free to commit to this branch if you have improvements
There was a problem hiding this comment.
I'm not entirely sure how that would make the code better or cleaner, would you mind explaining your thoughts? I'm probably not knowledged enough about pyo3.
There was a problem hiding this comment.
I tried to understand by letting claude explain what you might mean and claude says the advantage is less code, but it's still preferred to keep it as is because bytearray to pybackedbytes to visit_bytes would copy the data twice, vs the current implementation which only copies once
Quote:
PyBackedBytes can't hand its buffer over as a Vec, so for bytearray → owned targets like ByteBuf it would copy twice (once into the backed storage, once in the visitor). The explicit branch copies once and stays zero-copy for bytes.
If you are fine with the potential double copy and prefer a unified code path though I'm happy to change it.
There was a problem hiding this comment.
If I understand correctly, there's no way to feed a pybackedbytes into visit_byte_buf without a copy, while the current implementation does allow that, which allows visitor implementations to be more efficient if they need the owned Vec
There was a problem hiding this comment.
Sorry, you're right, I missed that they need the owned vec.
What I was thinking the "standard extract" would achieve is to try bytes first (that's presumably more likely) and then give an error which hints at both types:
if let Ok(bytes) = obj.cast::<PyBytes>() {
// handle byes
} else if let Ok(bytearray) = obj.cast::<PyByteArray>() {
// handle bytearray
} else {
Err(CastIntoError::new(
obj,
PyTuple::new(
obj.py(),
[
PyBytes::type_object(obj.py()),
PyByteArray::type_object(obj.py()),
],
)
.unwrap()
.into_any(),
))
}There was a problem hiding this comment.
Fully agree. Also don't like the error right now. The only reason I tried bytearray first is so the current bytes error does not change. But your version is better.
There was a problem hiding this comment.
Maybe fixed :) (edited from phone, didn't run, hopefully no syntax error)
There was a problem hiding this comment.
I recommend to squash merge, my commit history is a mess :D
There was a problem hiding this comment.
@davidhewitt Fixed and squashed. Sorry for the delay, very busy days.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UiciuVAHzaDSTuXNDJPLX6
Fixes #130