Skip to content

AnyView::to_owned_message copies Bytes payload unnecessarily #52

@iainmcgin

Description

@iainmcgin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions