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

feat(frame): add reset API for frame codec #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

PureWhiteWu
Copy link

In our previous usage, we are using it like this way:

pub fn decode(&self, mut data: Bytes) -> Result<Bytes> {
    self.detect(&mut data)?;
    let rd = data.reader();
    let mut rd: Box<dyn Read> = match self {
        CodecType::Gzip => Box::new(GzDecoder::new(rd)),
        CodecType::Snappy => Box::new(FrameDecoder::new(rd)),
        CodecType::Noop => Box::new(rd),
    };

    let mut buf = Vec::new();
    rd.read_to_end(&mut buf)?;
    Ok(buf.into())
}

And we found that there's a lot of memory allocation in the flamegraph which uses about 25% of cpu:

image

After I added the reset API and uses it like this way:

thread_local! {
    static SNAP_DECODER: RefCell<FrameDecoder<Reader<Bytes>>> = RefCell::new(FrameDecoder::new(Bytes::new().reader()));
    static MAX_SIZE: RefCell<usize> = RefCell::new(4096);
}

pub fn decode(&self, mut data: Bytes) -> Result<Vec<u8>> {
    self.detect(&mut data)?;
    let rd = data.reader();
    SNAP_DECODER.with(|s| {
        let mut snap_decoder = s.borrow_mut();
        let mut rd: Box<dyn Read> = match self {
            Self::Gzip => Box::new(GzDecoder::new(rd)),
            Self::Snappy => {
                snap_decoder.reset(rd);
                Box::new(&mut *snap_decoder)
            }
            Self::Noop => Box::new(rd),
        };

        MAX_SIZE.with(|s| {
            let mut size = s.borrow_mut();
            let mut buf = Vec::with_capacity(*size);
            rd.read_to_end(&mut buf)?;
            if buf.len() > *size {
                *size = buf.len();
            }
            Ok(buf)
        })
    })
}

The CPU usage of this part is greatly reduced and can even be ignored:

image

@PureWhiteWu
Copy link
Author

r? @BurntSushi

@PureWhiteWu
Copy link
Author

ping~ @BurntSushi

@PureWhiteWu
Copy link
Author

Kindly remind if you missed this @BurntSushi

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.

1 participant