Skip to content

Commit

Permalink
Merge pull request #6 from scunningham/progress
Browse files Browse the repository at this point in the history
Work around handler behavior in lz4.
  • Loading branch information
scunningham authored Jan 27, 2025
2 parents 889f059 + a717e64 commit 9b7913e
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions cmd/plz4/internal/ops/bakeoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,6 @@ func _prepLz4(rd io.ReadSeeker, srcSz int64, pw progress.Writer) (bakeFuncT, err
i = 0
)

bsz, err := parseBlockSize(CLI.Bakeoff.BS)
if err != nil {
return nil, err
}
bss := int64(bsz.Size())

tr := &progress.Tracker{
Message: "Processing lz4",
Total: srcSz * 10,
Expand All @@ -193,10 +187,21 @@ func _prepLz4(rd io.ReadSeeker, srcSz int64, pw progress.Writer) (bakeFuncT, err

pw.AppendTracker(tr)

// lz4 callback on write is odd, returns compressed block size
// Work around by assuming incremental block size and fix up for overflow
cbHandler := func(_ int) {
tr.Increment(bss)
// lz4 callback on write is buggy. If you call through the Write interface,
// the handler gets called back once with the size of the compressed buffer.
// If you call through the ReadFrom interface, the handler gets called twice,
// once with the size of the compressed buffer and once with the size of the src.
// Work around by ignoring the first callback and accumulating the second.

cnt := 0
cbHandler := func(sz int) {
cnt += 1
if cnt%2 == 0 {
// Ignore every other callback; we only track the src size, effectively
// taking advantage of buggy implementation. This is likely fragile assuming the
// bug will be fixed at some point.
tr.Increment(int64(sz))
}
}

opts = append(opts, lz4.OnBlockDoneOption(cbHandler))
Expand Down Expand Up @@ -537,7 +542,6 @@ func _parseBakeLz4Opts(srcSz int64) ([]lz4.Option, error) {

func lz4Level(l int) (lz4.CompressionLevel, error) {

// Last one wins; so append is ok.
var lz4Level lz4.CompressionLevel
switch l {
case 0:
Expand Down

0 comments on commit 9b7913e

Please sign in to comment.