rust-lang / flate2-rs

DEFLATE, gzip, and zlib bindings for Rust
https://docs.rs/flate2
Apache License 2.0
891 stars 158 forks source link

using GzDecoder in a loop #287

Closed whyCPPgofast closed 2 years ago

whyCPPgofast commented 2 years ago

Hey I was wondering if my code is inefficient and if there is a way of reusing the GzDecoder instead of calling ::new() every iteration.

    loop {

        let bytes = client.read_message().into_bytes().unwrap();

        let mut msg = String::new();

        GzDecoder::new(bytes).read_to_string(&mut msg).unwrap();

        println("{}", msg);

    }   

I tried instatiating a decoder outside of the loop but ran into multiple errors.

Something like this:

    let decoder = GzDecoder::new();
    loop {

        let bytes = client.read_message().into_bytes().unwrap();

        let mut msg = String::new();

        decoder.read_to_string(&mut msg).unwrap();

        println("{}", msg);

    }
alexcrichton commented 2 years ago

I'd recommend profiling to see if it's sufficient for your use case. If it's not then helper methods can be added to GzDecoder to reuse it.