Skip to content

Commit

Permalink
Merge pull request #220 from aiquestion/v4_restframe
Browse files Browse the repository at this point in the history
fix: reset frame when read to EOF to release buffer in Block
  • Loading branch information
pierrec authored Dec 12, 2024
2 parents fdaa7e2 + 72f5eb6 commit 2bb851f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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

0 comments on commit 2bb851f

Please sign in to comment.