iggy icon indicating copy to clipboard operation
iggy copied to clipboard

Reducer number of allocations via `Vec` in messages handling from SDK/server side

Open hubcio opened this issue 1 year ago • 1 comments

Instead of writing:

iggy/src/messages/send_messages.rs

impl BytesSerializable for Message {
    fn as_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(self.get_size_bytes() as usize);
        bytes.put_u128_le(self.id);
        if let Some(headers) = &self.headers {
            let headers_bytes = headers.as_bytes();
            bytes.put_u32_le(headers_bytes.len() as u32);
            bytes.extend(&headers_bytes);
        } else {
            bytes.put_u32_le(0);
        }
        bytes.put_u32_le(self.length);
        bytes.extend(&self.payload);
        bytes
    }
...
}


OR

```rust
impl BytesSerializable for HashMap<HeaderKey, HeaderValue> {
    fn as_bytes(&self) -> Vec<u8> {
        if self.is_empty() {
            return EMPTY_BYTES;
        }

        let mut bytes = vec![];
        for (key, value) in self {
            #[allow(clippy::cast_possible_truncation)]
            bytes.put_u32_le(key.0.len() as u32);
            bytes.extend(key.0.as_bytes());
            bytes.put_u8(value.kind.as_code());
            #[allow(clippy::cast_possible_truncation)]
            bytes.put_u32_le(value.value.len() as u32);
            bytes.extend(&value.value);
        }

        bytes
    }

it should be possible to return &[u8] constructed from fields of Message / HashMap<HeaderKey, HeaderValue>

Keep in mind that headers can have variable length.

Same procedure should be possible to apply... everywhere. This way it should be possible to dramatically remove number of allocation in message handling.

hubcio avatar Jan 05 '24 18:01 hubcio

@hubcio may, I pick this one?

KushnerykPavel avatar Jan 11 '24 12:01 KushnerykPavel