types: add decode_bytes_to_bytes for zero-copy bytes_fields decode#84
Open
types: add decode_bytes_to_bytes for zero-copy bytes_fields decode#84
Conversation
Generated merge_field arms for bytes_fields-tagged fields now call Buf::copy_to_bytes via the new helper instead of allocating a Vec and wrapping it; decoding from a Bytes-backed buffer becomes a refcount bump. Also hardens bytes_from_source's pointer-range containment check with checked_add (closes #76 item b).
|
All contributors have signed the CLA ✍️ ✅ |
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #53. Partially addresses #76 (item b only — items a and d are deferred to 0.5.0, item c landed in #77).
What
buffa::types::decode_bytes_to_bytes(buf: &mut impl Buf) -> Result<Bytes, DecodeError>reads a length-delimitedbytesvalue viaBuf::copy_to_bytes. WhenbufisBytes-backed this is a zero-copysplit_to; for&[u8]it falls back to alloc+copy (same cost as before).bytes_fieldscodegen sites inimpl_message.rs(singular implicit, singular optional, repeated, oneof) now emitdecode_bytes_to_bytes(buf)?instead ofBytes::from(decode_bytes(buf)?).view::bytes_from_sourcenow useschecked_addinstead ofwrapping_addfor the pointer-range containment check, so an overflowing end-pointer falls back to copy rather than reachingslice_ref's internal panic. Defensive only — unreachable on real allocators.buffa-types/src/generated/google.protobuf.any.rs(only WKT with abytes_fieldsfield).Memory retention note
In the zero-copy case the resulting field aliases the source allocation, so the source buffer is freed only once every aliased field is dropped. Documented on the new helper and in the CHANGELOG.
Semver
Additive only (new public function; generated-code call sites switch to a function that ships in the same crate version). Suitable for 0.4.1.
Testing
decode_bytes_to_bytes: zero-copy aliasing fromBytes,&[u8]fallback, empty, truncated.buffa-testdecodesBytesContextsfrom aBytesbuffer and asserts pointer aliasing for all four field shapes.cargo +1.95 clippy --workspace --all-targets -- -D warnings,cargo +1.95 test --workspace,task check-nostd,task lint-mdall pass.