Skip to content

Commit de62b63

Browse files
authored
Do not reserve exponentially growing margin (#130)
In fde7eea, buffers were changed to growing exponentially by calling `makemargin!(buf, X)` with an exponentially growing X. However, this is not the right way of solving the problem. Indeed, only a single byte of margin is necessary when removing already-used data from the buffer. Requiring exponentially increasing margins forces unneeded buffer resizes. Instead, `makemargin!` itself should exponentially increase buffersize when it finds that it can't make enough margin, and the callers of `makemargin!` should request only 1 byte of space, as before.
1 parent 79738b5 commit de62b63

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "TranscodingStreams"
22
uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
33
license = "MIT"
44
authors = ["Kenta Sato <[email protected]>"]
5-
version = "0.9.10"
5+
version = "0.9.11"
66

77
[deps]
88
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

src/buffer.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,19 @@ function makemargin!(buf::Buffer, minsize::Integer; eager::Bool = false)
130130
buf.bufferpos = buf.marginpos = 1
131131
end
132132
if marginsize(buf) < minsize || eager
133-
# shift data to left
133+
# datapos refer to the leftmost position of data that must not be
134+
# discarded. We can left-shift to discard all data before this
134135
if buf.markpos == 0
136+
# If data is not marked we must not discard buffered (nonconsumed) data
135137
datapos = buf.bufferpos
136138
datasize = buffersize(buf)
137139
else
140+
# Else, we must not consume marked data
141+
# (Since markpos ≤ bufferpos, we do not consume buffered data either)
138142
datapos = buf.markpos
139143
datasize = buf.marginpos - buf.markpos
140144
end
145+
# Shift data left in buffer to make space for new data
141146
copyto!(buf.data, 1, buf.data, datapos, datasize)
142147
shift = datapos - 1
143148
if buf.markpos > 0
@@ -146,9 +151,11 @@ function makemargin!(buf::Buffer, minsize::Integer; eager::Bool = false)
146151
buf.bufferpos -= shift
147152
buf.marginpos -= shift
148153
end
154+
# If there is still not enough margin, we expand buffer.
155+
# At least enough for minsize, but otherwise 1.5 times
149156
if marginsize(buf) < minsize
150-
# expand data buffer
151-
resize!(buf.data, buf.marginpos + minsize - 1)
157+
datasize = length(buf.data)
158+
resize!(buf.data, max(buf.marginpos + minsize - 1, datasize + div(datasize, 2)))
152159
end
153160
@assert marginsize(buf) minsize
154161
return marginsize(buf)

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, max(1, div(length(buffer), 2)))
170+
makemargin!(buffer, 1)
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
@@ -591,7 +591,7 @@ function fillbuffer(stream::TranscodingStream; eager::Bool = false)
591591
end
592592
callstartproc(stream, :read)
593593
end
594-
makemargin!(buffer2, max(1, div(length(buffer2), 2)))
594+
makemargin!(buffer2, 1)
595595
readdata!(stream.stream, buffer2)
596596
_, Δout = callprocess(stream, buffer2, buffer1)
597597
nfilled += Δout

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ using TranscodingStreams:
5858
writebyte!(buf, 0x34)
5959
@test makemargin!(buf, 0) === 15
6060
writebyte!(buf, 0x99)
61-
@test makemargin!(buf, 20) === 20
61+
margin_size = makemargin!(buf, 20)
62+
@test margin_size >= 20
6263
emptybuffer!(buf)
63-
@test makemargin!(buf, 0) === 22
64+
@test makemargin!(buf, 0) === margin_size + 2
6465
end
6566

6667
@testset "Memory" begin

0 commit comments

Comments
 (0)