Repository: servo/servo
File: components/script/dom/compressionstream.rs (Rust)

Code before:

    fn try_finish(&mut self) -> io::Result<()> {
        match self {
            Compressor::Deflate(zlib_encoder) => zlib_encoder.try_finish(),
            Compressor::DeflateRaw(deflate_encoder) => deflate_encoder.try_finish(),
            Compressor::Gzip(gz_encoder) => gz_encoder.try_finish(),
            Compressor::Brotli(brotli_encoder) => brotli_encoder.flush(),
        }
    }
}

impl MallocSizeOf for Compressor {
    #[expect(unsafe_code)]
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        match self {
            Compressor::Deflate(zlib_encoder) => zlib_encoder.size_of(ops),
            Compressor::DeflateRaw(deflate_encoder) => deflate_encoder.size_of(ops),
            Compressor::Gzip(gz_encoder) => gz_encoder.size_of(ops),
            Compressor::Brotli(encoder) => {
                // Size of the output Vec buffer
                let output_size = encoder.get_ref().size_of(ops);

                // Size of the boxed BrotliEncoder struct itself
                let encoder_size = unsafe { ops.malloc_size_of(&**encoder) };

                output_size + encoder_size
            },
        }
    }
}

/// <https://compression.spec.whatwg.org/#compressionstream>
#[dom_struct]
pub(crate) struct CompressionStream {
    reflector_: Reflector,

    /// <https://streams.spec.whatwg.org/#generictransformstream>
    transform: Dom<TransformStream>,

    /// <https://compression.spec.whatwg.org/#compressionstream-format>
    format: CompressionFormat,

    // <https://compression.spec.whatwg.org/#compressionstream-context>
    #[no_trace]
    context: RefCell<Compressor>,
}

impl CompressionStream {
    fn new_inherited(transform: &TransformStream, format: CompressionFormat) -> CompressionStream {
        CompressionStream {
            reflector_: Reflector::new(),

Review comment (suggestion):
Measuring only the encoder should be sufficient, since the encoder struct (actually, its inner struct) already includes the output `Vec`.

https://github.com/dropbox/rust-brotli/blob/85dbcc34999e365b965329f5ae66120a04ad87bf/src/enc/writer.rs#L120-L132

Code after:

    fn try_finish(&mut self) -> io::Result<()> {
        match self {
            Compressor::Deflate(zlib_encoder) => zlib_encoder.try_finish(),
            Compressor::DeflateRaw(deflate_encoder) => deflate_encoder.try_finish(),
            Compressor::Gzip(gz_encoder) => gz_encoder.try_finish(),
            Compressor::Brotli(brotli_encoder) => brotli_encoder.flush(),
        }
    }
}

impl MallocSizeOf for Compressor {
    #[expect(unsafe_code)]
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        match self {
            Compressor::Deflate(zlib_encoder) => zlib_encoder.size_of(ops),
            Compressor::DeflateRaw(deflate_encoder) => deflate_encoder.size_of(ops),
            Compressor::Gzip(gz_encoder) => gz_encoder.size_of(ops),
            Compressor::Brotli(brotli_dencoder) => unsafe {
                ops.malloc_size_of(&**brotli_dencoder)
            },
        }
    }
}

/// <https://compression.spec.whatwg.org/#compressionstream>
#[dom_struct]
pub(crate) struct CompressionStream {
    reflector_: Reflector,

    /// <https://streams.spec.whatwg.org/#generictransformstream>
    transform: Dom<TransformStream>,

    /// <https://compression.spec.whatwg.org/#compressionstream-format>
    format: CompressionFormat,

    // <https://compression.spec.whatwg.org/#compressionstream-context>
    #[no_trace]
    context: RefCell<Compressor>,
}

impl CompressionStream {
    fn new_inherited(transform: &TransformStream, format: CompressionFormat) -> CompressionStream {
        CompressionStream {
            reflector_: Reflector::new(),
            transform: Dom::from_ref(transform),
            format,
            context: RefCell::new(Compressor::new(format)),
        }
    }