Skip to content

Commit 2bb851f

Browse files
authored
Merge pull request #220 from aiquestion/v4_restframe
fix: reset frame when read to EOF to release buffer in Block
2 parents fdaa7e2 + 72f5eb6 commit 2bb851f

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

bench_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ var (
8484
digitsLZ4 = mustLoadFile("testdata/e.txt.lz4")
8585
twainLZ4 = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.lz4")
8686
randomLZ4 = mustLoadFile("testdata/random.data.lz4")
87+
repeatLz4 = mustLoadFile("testdata/repeat.txt.lz4")
8788
)
8889

8990
func benchmarkUncompress(b *testing.B, compressed []byte) {
@@ -160,3 +161,19 @@ func BenchmarkWriterReset(b *testing.B) {
160161
_ = zw.Close()
161162
}
162163
}
164+
165+
func BenchmarkReaderNoReset(b *testing.B) {
166+
compressed := repeatLz4
167+
b.ReportAllocs()
168+
b.ResetTimer()
169+
170+
for i := 0; i < b.N; i++ {
171+
r := bytes.NewReader(compressed)
172+
zr := lz4.NewReader(r)
173+
buf := bytes.NewBuffer(nil)
174+
_, err := buf.ReadFrom(zr)
175+
if err != nil {
176+
b.Fatal(err)
177+
}
178+
}
179+
}

reader.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ func (r *Reader) Read(buf []byte) (n int, err error) {
135135
}
136136
lz4block.Put(r.data)
137137
r.data = nil
138+
// reset frame to release the buffer held by Block
139+
r.frame.Reset(r.num)
138140
return
139141
default:
140142
return
@@ -157,9 +159,9 @@ func (r *Reader) Read(buf []byte) (n int, err error) {
157159
}
158160

159161
// read uncompresses the next block as follow:
160-
// - if buf has enough room, the block is uncompressed into it directly
161-
// and the lenght of used space is returned
162-
// - else, the uncompress data is stored in r.data and 0 is returned
162+
// - if buf has enough room, the block is uncompressed into it directly
163+
// and the lenght of used space is returned
164+
// - else, the uncompress data is stored in r.data and 0 is returned
163165
func (r *Reader) read(buf []byte) (int, error) {
164166
block := r.frame.Blocks.Block
165167
_, err := block.Read(r.frame, r.src, r.cum)

0 commit comments

Comments
 (0)