Coder Social home page Coder Social logo

Comments (3)

klauspost avatar klauspost commented on June 5, 2024 1

I wonder how does it know to consume that final empty block without peeking ahead (or does it?)

TBH I haven't looked into it. It could be that it is trying to always have data decompressed, so it decodes a block as soon as there is no more data available.

from compress.

klauspost avatar klauspost commented on June 5, 2024

The main issue is that ReadFull doesn't read until EOF if the array can be satisfied.

If you change the last part to be...

	decomp, err := io.ReadAll(zstdReader)
	if err != nil && err != io.ErrUnexpectedEOF {
		panic(err)
	}
	if sampleLen != len(decomp) {
		panic(fmt.Sprintf("Zstd Length mismatch %d != %d", sampleLen, len(decomp)))
	}
	if !bytes.Equal(decompGozstd[:sampleLen], decomp[:sampleLen]) {
		panic("Decoded data doesn't match")
	}

Everything will be read from the input.

My guess is that "valyala" adds an empty block with a Last_Block identifier. This matches with the 3 bytes you see. I try to add this to the actual last block to not waste 3 bytes.

Since there is enough data to satisfy your request without decoding more, it will not decode more blocks until the data is actually needed.

You can "reintroduce" the issue by doing decomp, err := io.ReadAll(&io.LimitedReader{R: zstdReader, N: int64(sampleLen)}) - since the LimitedReader will stop reading upstream once N has been reached.

It is however not my impression the ZipDecompressor forces this and creates the problem by itself.

from compress.

vsivsi avatar vsivsi commented on June 5, 2024

That all makes perfect sense, thanks so much for quickly responding. Since for my application I know the exact upper bound length of the decompressed data, I can easily work around this by just ensuring the read buffer is strictly longer than that upper bound.

Given your explanation, the most surprising thing is that the gozstd generated io.Reader does consistently consume the input stream to EOF when used with io.ReadFull(). I wonder how does it know to consume that final empty block without peeking ahead (or does it?)

from compress.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.