scylla-cdc-rust icon indicating copy to clipboard operation
scylla-cdc-rust copied to clipboard

StreamID unnecessarily allocates

Open piodul opened this issue 2 months ago • 0 comments

The StreamID struct is defined like this:

/// A struct representing a stream ID.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct StreamID {
    pub(crate) id: Vec<u8>,
}

The vector is unnecessary - in the current format, CDC stream IDs are always constant size and have 128 bits. Keeping it as a vector is wasteful, especially that the StreamID struct is embedded within the CDCRow objects which can be created by the library in large quantities.

Proposal: change it to:

pub struct StreamID {
	pub(crate) repr: StreamIDRepr,
}

enum StreamIDRepr {
	StandardSize([u8; 16]),
	NonStandardSize(Vec<u8>),
}

Thanks to the null pointer optimization, the new struct should have the same size as the old one. We are not reducing generality while optimizing the case that will practically happen 100% of the time.

piodul avatar Sep 12 '25 14:09 piodul