diff --git a/bench_test.go b/bench_test.go index 3b32a97..37ff23b 100644 --- a/bench_test.go +++ b/bench_test.go @@ -84,6 +84,7 @@ var ( digitsLZ4 = mustLoadFile("testdata/e.txt.lz4") twainLZ4 = mustLoadFile("testdata/Mark.Twain-Tom.Sawyer.txt.lz4") randomLZ4 = mustLoadFile("testdata/random.data.lz4") + repeatLz4 = mustLoadFile("testdata/repeat.txt.lz4") ) func benchmarkUncompress(b *testing.B, compressed []byte) { @@ -160,3 +161,19 @@ func BenchmarkWriterReset(b *testing.B) { _ = zw.Close() } } + +func BenchmarkReaderNoReset(b *testing.B) { + compressed := repeatLz4 + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + r := bytes.NewReader(compressed) + zr := lz4.NewReader(r) + buf := bytes.NewBuffer(nil) + _, err := buf.ReadFrom(zr) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/reader.go b/reader.go index 275daad..0a67e7a 100644 --- a/reader.go +++ b/reader.go @@ -135,6 +135,8 @@ func (r *Reader) Read(buf []byte) (n int, err error) { } lz4block.Put(r.data) r.data = nil + // reset frame to release the buffer held by Block + r.frame.Reset(r.num) return default: return @@ -157,9 +159,9 @@ func (r *Reader) Read(buf []byte) (n int, err error) { } // read uncompresses the next block as follow: -// - if buf has enough room, the block is uncompressed into it directly -// and the lenght of used space is returned -// - else, the uncompress data is stored in r.data and 0 is returned +// - if buf has enough room, the block is uncompressed into it directly +// and the lenght of used space is returned +// - else, the uncompress data is stored in r.data and 0 is returned func (r *Reader) read(buf []byte) (int, error) { block := r.frame.Blocks.Block _, err := block.Read(r.frame, r.src, r.cum)