Follow-up from PR #51 (Timothy Yen's change backing Any.value with bytes::Bytes).
Current behavior
In generated AnyView::to_owned_message, the bytes field is converted via:
value: ::bytes::Bytes::copy_from_slice(self.value),
where self.value: &'a [u8]. copy_from_slice heap-allocates and memcpys, which is no better than the previous .to_vec(). The headline win of PR #51 (clone = refcount bump) is lost on the view-to-owned path that production decode pipelines typically hit.
What we'd want
Bytes::slice_ref(&parent, self.value) would produce a zero-copy, refcount-bump slice of the parent buffer. Safety-wise this is fine — the view already has to live as long as the parent buffer, and the resulting Bytes just extends that lifetime via the refcount.
What's blocking it
Views currently decode from &'a [u8], not &'a Bytes. To slice-ref, decode_view (and the whole view-decoding path) would need to accept and thread through a Bytes parent so codegen can emit slice_ref for bytes_fields. That's a codegen-wide change, not a single-site fix.
Scope
- Extend view decode entry points to accept
Bytes (keeping &[u8] as a convenience that wraps into a short-lived Bytes, or a separate entry point).
- Update
view.rs codegen to emit slice_ref for any field tagged in bytes_fields.
- Consider the same for
string_fields if/when we grow that (shared-buffer strings via bytes::Bytes + Str wrapper, or Arc<str>).
Follow-up from PR #51 (Timothy Yen's change backing
Any.valuewithbytes::Bytes).Current behavior
In generated
AnyView::to_owned_message, the bytes field is converted via:where
self.value: &'a [u8].copy_from_sliceheap-allocates and memcpys, which is no better than the previous.to_vec(). The headline win of PR #51 (clone = refcount bump) is lost on the view-to-owned path that production decode pipelines typically hit.What we'd want
Bytes::slice_ref(&parent, self.value)would produce a zero-copy, refcount-bump slice of the parent buffer. Safety-wise this is fine — the view already has to live as long as the parent buffer, and the resultingBytesjust extends that lifetime via the refcount.What's blocking it
Views currently decode from
&'a [u8], not&'a Bytes. To slice-ref,decode_view(and the whole view-decoding path) would need to accept and thread through aBytesparent so codegen can emitslice_refforbytes_fields. That's a codegen-wide change, not a single-site fix.Scope
Bytes(keeping&[u8]as a convenience that wraps into a short-livedBytes, or a separate entry point).view.rscodegen to emitslice_reffor any field tagged inbytes_fields.string_fieldsif/when we grow that (shared-buffer strings viabytes::Bytes+Strwrapper, orArc<str>).