Skip to content

Make the Bytes struct the size of 3 pointers rather than 4? #799

@jbms

Description

@jbms

Currently the Bytes struct is the size of 4 pointers:

pub struct Bytes {

However, we could reduce it to 3 pointers by replacing the vtable pointer with an index into a fixed list of vtables, and packing that index into unused bits of the data (owner) pointer. That would actually simplify the code in some ways, because the KIND_ARC vs KIND_VEC tagging would not be necessary since the pointer and vtable could be atomically replaced together.

As there are fewer than 8 distinct vtables, only 3 bits are needed.

It would be easiest and most portable to use the low bits, which would mean requiring 8-byte alignment. That is easy to require for everything except the Vec backing buffer allocation. However, I believe in practice the default allocator on all or most systems will always return 8-byte aligned buffers, and therefore it may be fine to require that. In the (assumed to be rare) case that a non-8-byte-aligned Vec buffer is received, it can just be promoted to the Shared representation.

Alternatively, on 64-bit systems there are plenty of high bits to use, and the size optimization could be disabled for 32-bit systems (where already it is half the size). This would require keeping the KIND_ARC and KIND_VEC tagging for 32-bit systems, though.

If the maintainers are open to this, I can create a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions