Skip to content

Commit fde7eea

Browse files
authored
Grow buffers exponentially, not one byte at a time (#121)
* Grow buffers exponentially, not one byte at a time To check if a TranscodingStream is eof, it first checks if it has any unused bytes in its buffer. If not, it will attempt to fill the buffer, and only return true if no bytes were filled. If there is no margin in the buffer to fill, it will call `makemargin(buffer, 1)` to ensure at least 1 byte of space. In the happy case, this will remove used up bytes. Unfortunately, buffers do never move data pointed to by their mark. So, when no space is available due to the entire buffer being filled up with marked data, the buffer will grow one byte at a time in a loop until the data fills. This is unacceptable as megabytes of data can theoretically be marked. This commit changes the growth behaviour to request at least half the buffer's length in the margin. In most cases, this will do nothing. But in the unhappy case mentioned above, it will cause the buffer to grow expoentially, until a proper size is quickly reached. * Address suggestions
1 parent 38b969d commit fde7eea

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/noop.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function fillbuffer(stream::NoopStream; eager::Bool = false)
167167
end
168168
nfilled::Int = 0
169169
while ((!eager && buffersize(buffer) == 0) || (eager && makemargin!(buffer, 0, eager = true) > 0)) && !eof(stream.stream)
170-
makemargin!(buffer, 1)
170+
makemargin!(buffer, max(1, div(length(buffer), 2)))
171171
nfilled += readdata!(stream.stream, buffer)
172172
end
173173
buffer.transcoded += nfilled

src/stream.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ function fillbuffer(stream::TranscodingStream; eager::Bool = false)
575575
end
576576
callstartproc(stream, :read)
577577
end
578-
makemargin!(buffer2, 1)
578+
makemargin!(buffer2, max(1, div(length(buffer2), 2)))
579579
readdata!(stream.stream, buffer2)
580580
_, Δout = callprocess(stream, buffer2, buffer1)
581581
nfilled += Δout

0 commit comments

Comments
 (0)