Skip to content

Feature idea: Lazy instantiation for all decompressors #1046

Closed

Description

@Pr0methean

zip::compression::Decompressor's LZMA and PPMd variants are currently lazily instantiated when the first byte is read, and with #986 the Zstd variant will be as well. Each of these variants has their own enum with instantiated and uninstantiated states.

I'm wondering if we ought to make lazy instantiation possible for all compression methods. Instead of each method having its own lazy-instantiation enum, Decompressor would contain only initialized decoders (except in the Stored case where there'd still be no decoder at all), but would be wrapped in a lazy-instantiation enum that would look something like this:

pub(crate) enum LazyDecompressor<R: BufRead> {
    Uninitialized {
        inner: R,
        instantiator: Box<dyn FnOnce(R) -> ZipResult<Decompressor<R>>>,
    },
    Initialized(Decompressor<R>),
    FailedToInitialize
}

impl <R: BufRead> Read for LazyDecompressor<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let decompressor = match mem::replace(self, LazyDecompressor::FailedToInitialize) {
            LazyDecompressor::Initialized(decompressor) => decompressor,
            LazyDecompressor::Uninitialized { inner, instantiator } => instantiator(inner)?,
            LazyDecompressor::FailedToInitialize => {
                return Err(io::Error::other("Previously failed to initialize decompressor"));
            }
        };
        let result = decompressor.read(buf);
        *self = LazyDecompressor::Initialized(decompressor);
        result
    }
}

(The Box is needed because there'll be a varying need to capture variables: in the case of LZMA we capture the uncompressed size, and in the case of an unsupported method we capture the method ID for the error message. We could use a multi-purpose "payload" field/parameter and a fn field, but it'd be less readable and, because the size would have to be specified upfront, less future-proof.)

And the match statement in Decompressor::new would be replaced with one in a LazyDecompressor::new that would select the instantiator; so (when enough compression-method features were enabled for the match to be megamorphic) we'd have an eager table lookup and a lazy dispatch to an address from that table, rather than an immediate table disapatch.

Besides speeding up operations like raw_copy_file by letting them skip initializers that might be nontrivial, this would also allow them to support compression methods we can't decompress. On the other hand, it would mean that if someone tried to decompress a file with an unsupported compression method, we'd be stringing them along with false hope until the moment they were ready to read the first decompressed byte, losing some fail-fast behavior we have now.

One performance consideration is that every call to Read::read would entail the above mem::replace on the LazyDecompressor; and when it returned a LazyDecompressor::Initialized then that would then almost always be put back, mostly shallowly unchanged. How much difference that would make compared to the cost of decompressing the decompressor's natural chunk size, I'm not sure of; it'd depend on R's shallow size and whether compiler optimizations could help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions