TranscodingStreams.jl icon indicating copy to clipboard operation
TranscodingStreams.jl copied to clipboard

Enhancement: Request at least N bytes of space in buffers

Open jakobnissen opened this issue 5 years ago • 2 comments

A new codec is implemented by extending the process function, which gives you a number of input bytes to transcode, and a number of bytes to write the result to. Right now, there is, as far as I can see, no way of requesting at least N bytes of input/output space. This makes it a little annoying to implement blocked codexes.

E.g. in the following example:

julia> open("/tmp/file", "w") do file
           write(file, rand(UInt8, Int(1.5 * 1 << 17)))
       end
196608

julia> stream = NoopStream(open("/tmp/file"); bufsize=1<<18)
TranscodingStream{Noop,IOStream}(<mode=idle>)

julia> read(stream, UInt8); bytesavailable(stream)
131071

Here, even though the stream has 2^18 bytes of buffer, and the file is 1.5*2^17, only 2^17 bytes is read into the buffer at a time.

What assumptions can we make about the number of available bytes? Could we, somehow, force the codec to make at least N bytes available, unless we read the EOF?

jakobnissen avatar Aug 17 '20 07:08 jakobnissen

For future reference, the internal function makemargin!(::Buffer, N) will resize the buffer so that at least N bytes from the margin (that is, the end of the usable data) to the end of the buffer. It will not resize if there is already that much space, and will move away unmarked and used up data to make room before resizing.

This is purely internal, so not really safe for use. Still, useful to know.

jakobnissen avatar Aug 13 '22 10:08 jakobnissen

Was this fixed? I'm not so sure it's a problem.

If I set eager=true in fillbuffer, bytesavailable says I've processed the full 1.5*2^17 bytes. Without eager set, eof(stream.stream); bytesavailable(stream.stream) gave me 32768, which matched read(stream, UInt8); bytesavailable(stream). So I think this is limited by the input stream.

warrenalphonso avatar Oct 13 '22 21:10 warrenalphonso