Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reset frame when read to EOF to release buffer in Block #220

Open
wants to merge 1 commit into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}
8 changes: 5 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down