Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Send messages as Vec<Bytes> #650

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

YHM404
Copy link
Contributor

@YHM404 YHM404 commented Feb 5, 2024

resolve #622

still in draft.

Copy link
Collaborator

@hubcio hubcio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have better idea.

we don't need intermediate vector - and you can use BufWriter which has tons better performance

impl for Message:

 pub async fn write_to<T>(&self, writer: &mut T) -> Result<(), IggyError>
    where
        T: AsyncWriteExt + Unpin,
    {
        writer.write_u64_le(self.offset).await?;
        writer.write_u8(self.state.as_code()).await?;
        writer.write_u64_le(self.timestamp).await?;
        writer.write_u128_le(self.id).await?;
        writer.write_u32_le(self.checksum).await?;
        if let Some(headers) = &self.headers {
            let headers_bytes = headers.as_bytes();
            #[allow(clippy::cast_possible_truncation)]
            writer.write_u32_le(headers_bytes.len() as u32).await?;
            writer.write_all(&headers_bytes).await?;
        } else {
            writer.write_u32_le(0u32).await?;
        }
        writer.write_u32_le(self.length).await?;
        writer.write_all(&self.payload).await?;
        Ok(())
    }

@YHM404
Copy link
Contributor Author

YHM404 commented Feb 5, 2024

I have better idea.

we don't need intermediate vector - and you can use BufWriter which has tons better performance

impl for Message:

 pub async fn write_to<T>(&self, writer: &mut T) -> Result<(), IggyError>
    where
        T: AsyncWriteExt + Unpin,
    {
        writer.write_u64_le(self.offset).await?;
        writer.write_u8(self.state.as_code()).await?;
        writer.write_u64_le(self.timestamp).await?;
        writer.write_u128_le(self.id).await?;
        writer.write_u32_le(self.checksum).await?;
        if let Some(headers) = &self.headers {
            let headers_bytes = headers.as_bytes();
            #[allow(clippy::cast_possible_truncation)]
            writer.write_u32_le(headers_bytes.len() as u32).await?;
            writer.write_all(&headers_bytes).await?;
        } else {
            writer.write_u32_le(0u32).await?;
        }
        writer.write_u32_le(self.length).await?;
        writer.write_all(&self.payload).await?;
        Ok(())
    }

That sound's great. I'll try it. thx

@hubcio
Copy link
Collaborator

hubcio commented Feb 5, 2024

@YHM404 remember to call flush() at the end of each command send

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use write_vectored to replace write_all
2 participants