From 74b7522b6ac850a609e3ee9959e0f73ade12fb5d Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Tue, 4 Oct 2022 09:13:25 -0600 Subject: [PATCH] rust/http2: box decompressor variants These variants, in particular the Brotli one can be large at over 2500 bytes which is allocated no matter which decompressor is being used. Gzip comes in at over 500 bytes. Box deflate for consistency. --- rust/src/http2/decompression.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/rust/src/http2/decompression.rs b/rust/src/http2/decompression.rs index caa9b72aaa..ff5c7da848 100644 --- a/rust/src/http2/decompression.rs +++ b/rust/src/http2/decompression.rs @@ -76,9 +76,13 @@ impl Read for HTTP2cursor { pub enum HTTP2Decompresser { UNASSIGNED, - GZIP(GzDecoder), - BROTLI(brotli::Decompressor), - DEFLATE(DeflateDecoder), + // Box because large. + GZIP(Box>), + // Box because large. + BROTLI(Box>), + // This one is not so large, at 88 bytes as of doing this, but box + // for consistency. + DEFLATE(Box>), } impl std::fmt::Debug for HTTP2Decompresser { @@ -170,16 +174,16 @@ impl HTTP2DecoderHalf { if self.encoding == HTTP2ContentEncoding::Unknown { if input == b"gzip" { self.encoding = HTTP2ContentEncoding::Gzip; - self.decoder = HTTP2Decompresser::GZIP(GzDecoder::new(HTTP2cursor::new())); + self.decoder = HTTP2Decompresser::GZIP(Box::new(GzDecoder::new(HTTP2cursor::new()))); } else if input == b"deflate" { self.encoding = HTTP2ContentEncoding::Deflate; - self.decoder = HTTP2Decompresser::DEFLATE(DeflateDecoder::new(HTTP2cursor::new())); + self.decoder = HTTP2Decompresser::DEFLATE(Box::new(DeflateDecoder::new(HTTP2cursor::new()))); } else if input == b"br" { self.encoding = HTTP2ContentEncoding::Br; - self.decoder = HTTP2Decompresser::BROTLI(brotli::Decompressor::new( + self.decoder = HTTP2Decompresser::BROTLI(Box::new(brotli::Decompressor::new( HTTP2cursor::new(), HTTP2_DECOMPRESSION_CHUNK_SIZE, - )); + ))); } else { self.encoding = HTTP2ContentEncoding::Unrecognized; } @@ -191,7 +195,7 @@ impl HTTP2DecoderHalf { ) -> io::Result<&'a [u8]> { match self.decoder { HTTP2Decompresser::GZIP(ref mut gzip_decoder) => { - let r = http2_decompress(gzip_decoder, input, output); + let r = http2_decompress(&mut *gzip_decoder.as_mut(), input, output); match r { Err(_) => { self.decoder = HTTP2Decompresser::UNASSIGNED; @@ -201,7 +205,7 @@ impl HTTP2DecoderHalf { return r; } HTTP2Decompresser::BROTLI(ref mut br_decoder) => { - let r = http2_decompress(br_decoder, input, output); + let r = http2_decompress(&mut *br_decoder.as_mut(), input, output); match r { Err(_) => { self.decoder = HTTP2Decompresser::UNASSIGNED; @@ -211,7 +215,7 @@ impl HTTP2DecoderHalf { return r; } HTTP2Decompresser::DEFLATE(ref mut df_decoder) => { - let r = http2_decompress(df_decoder, input, output); + let r = http2_decompress(&mut *df_decoder.as_mut(), input, output); match r { Err(_) => { self.decoder = HTTP2Decompresser::UNASSIGNED;