From 72f5eb617db082814b55046b7959073b5eb8b92b Mon Sep 17 00:00:00 2001 From: ByteDance Date: Mon, 19 Aug 2024 19:41:26 +0800 Subject: [PATCH] fix: reset frame when read to EOF to release buffer in Block --- bench_test.go | 17 +++++++++++++++++ reader.go | 8 +++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bench_test.go b/bench_test.go index 3b32a971..37ff23b7 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 275daad7..0a67e7a5 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)