diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 8d24825..d4cb6f9 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -24,6 +24,6 @@ jobs: go-version: oldstable - uses: actions/checkout@v4 - name: golangci-lint - uses: golangci/golangci-lint-action@v9.2.0 + uses: golangci/golangci-lint-action@v9.3.0 with: - version: v2.11.3 + version: v2.15.0 diff --git a/.golangci.yml b/.golangci.yml index 200dd7b..9ae8fdd 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,7 @@ run: linters: default: all disable: + - exhaustruct_v5 - cyclop - err113 - errcheck diff --git a/Makefile b/Makefile index c366297..08a65bd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -#GOLANGCI_LINT_VERSION := "v2.11.3" # Optional configuration to pinpoint golangci-lint version. +#GOLANGCI_LINT_VERSION := "v2.15.0" # Optional configuration to pinpoint golangci-lint version. # The head of Makefile determines location of dev-go to include standard targets. GO ?= go diff --git a/cmd/catp/catp/app.go b/cmd/catp/catp/app.go index dcc4f09..784acc5 100644 --- a/cmd/catp/catp/app.go +++ b/cmd/catp/catp/app.go @@ -84,7 +84,7 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g flag.IntVar(&r.parallel, "parallel", 1, "number of parallel readers if multiple files are provided\n"+ "lines from different files will go to output simultaneously (out of order of files, but in order of lines in each file)\n"+ - "use 0 for multi-threaded zst decoder (slightly faster at cost of more CPU)") + "use 0 for a 2-block pipelined zst decoder (slightly faster at cost of more CPU)") flag.BoolVar(&r.splitChunks, "split-chunks", false, "experimental: with -parallel N (N >= 2), split each uncompressed file (>= 1 MB per chunk)\n"+ "into up to N byte-range chunks processed in parallel as virtual files named file.chunkNN,\n"+ diff --git a/cmd/catp/catp/catp.go b/cmd/catp/catp/catp.go index e2686a0..6fad7b4 100644 --- a/cmd/catp/catp/catp.go +++ b/cmd/catp/catp/catp.go @@ -23,7 +23,6 @@ import ( "time" "github.com/bool64/progress" - "github.com/klauspost/compress/zstd" gzip "github.com/klauspost/pgzip" "github.com/vearutop/plot-go" "golang.org/x/time/rate" @@ -475,7 +474,7 @@ func (r *runner) addSample(s speedSample) { // speedJSON returns samples newer than ?since= as [ts, lines/s, MB/s] rows. func (r *runner) speedJSON(w http.ResponseWriter, req *http.Request) { - since, _ := strconv.ParseInt(req.FormValue("since"), 10, 64) //nolint:gosec + since, _ := strconv.ParseInt(req.FormValue("since"), 10, 64) rows := [][]any{} r.statusMu.Lock() @@ -519,7 +518,7 @@ func (r *runner) startControlServer() error { mux := http.NewServeMux() mux.HandleFunc("/rate-limit", func(w http.ResponseWriter, req *http.Request) { if req.Method == http.MethodPost { - v, err := strconv.ParseFloat(req.FormValue("value"), 64) //nolint:gosec + v, err := strconv.ParseFloat(req.FormValue("value"), 64) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -741,7 +740,14 @@ func (r *runner) catChunk(filename string, c *chunk) (err error) { out = w } - if r.parallel <= 1 && !r.noProgress && !r.hasCompression { + if r.startLine != 0 || r.endLine != 0 { + r.countLines = true + } + + useScan := r.filters.isSet() || r.parallel > 1 || r.hasOptions || r.countLines || r.limiter != nil || sampleBytes != nil + + // consumed is only maintained by scanFile; readFile relies on the counting reader. + if r.parallel <= 1 && !r.noProgress && !r.hasCompression && useScan { r.consumed = new(int64) } @@ -766,11 +772,7 @@ func (r *runner) catChunk(filename string, c *chunk) (err error) { }) } - if r.startLine != 0 || r.endLine != 0 { - r.countLines = true - } - - if r.filters.isSet() || r.parallel > 1 || r.hasOptions || r.countLines || r.limiter != nil || sampleBytes != nil { + if useScan { r.scanFile(filename, rd, out, sampleBytes) } else { r.readFile(rd, out) @@ -792,14 +794,8 @@ func (r *runner) openReader(rd io.Reader, filename string) (io.Reader, error) { return nil, fmt.Errorf("failed to init gzip reader: %w", err) } case strings.HasSuffix(filename, ".zst"): - if r.parallel >= 1 { - if rd, err = zstdReader(rd); err != nil { - return nil, fmt.Errorf("failed to init zst reader: %w", err) - } - } else { - if rd, err = zstd.NewReader(rd); err != nil { - return nil, fmt.Errorf("failed to init zst reader: %w", err) - } + if rd, err = zstdReader(rd, r.parallel); err != nil { + return nil, fmt.Errorf("failed to init zst reader: %w", err) } } @@ -817,7 +813,7 @@ func startProfiling(cpuProfile string, memProfile string) { } go func() { - time.Sleep(10 * time.Second) + time.Sleep(30 * time.Second) pprof.StopCPUProfile() println("CPU profile written to", cpuProfile) diff --git a/cmd/catp/catp/cgo_zstd.go b/cmd/catp/catp/cgo_zstd.go index 2f3b02e..25d5e6d 100644 --- a/cmd/catp/catp/cgo_zstd.go +++ b/cmd/catp/catp/cgo_zstd.go @@ -5,17 +5,20 @@ package catp import ( "io" - "github.com/DataDog/zstd" + zstd "github.com/vearutop/zstd-go/libzstd" ) func init() { versionExtra = append(versionExtra, "CGO_ZSTD") } -func zstdReader(rd io.Reader) (io.Reader, error) { +func zstdReader(rd io.Reader, _ int) (io.Reader, error) { return zstd.NewReader(rd), nil } func zstdWriter(w io.Writer) (io.WriteCloser, error) { - return zstd.NewWriterLevel(w, zstd.BestSpeed), nil + return zstd.NewWriter(w, zstd.ProfileFastest(), func(w *zstd.Writer) { + w.FrameSize = 16 << 20 + w.Seekable = true + }), nil } diff --git a/cmd/catp/catp/zstd.go b/cmd/catp/catp/zstd.go index de0c48d..e3949c6 100644 --- a/cmd/catp/catp/zstd.go +++ b/cmd/catp/catp/zstd.go @@ -3,20 +3,24 @@ package catp import ( - "fmt" "io" - "github.com/klauspost/compress/zstd" + zstdgo "github.com/vearutop/zstd-go" ) -func zstdReader(rd io.Reader) (io.Reader, error) { - if rd, err := zstd.NewReader(rd, zstd.WithDecoderConcurrency(1)); err == nil { - return rd, nil - } else { //nolint:revive - return nil, fmt.Errorf("failed to init zst reader: %w", err) +// zstdReader decodes zstd, -parallel 0 enables a 2-block decoding pipeline (faster, more CPU). +func zstdReader(rd io.Reader, parallel int) (io.Reader, error) { + zr := zstdgo.NewReader(rd) + if parallel == 0 { + zr.Concurrency = 2 } + + return zr, nil } func zstdWriter(w io.Writer) (io.WriteCloser, error) { - return zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedFastest), zstd.WithLowerEncoderMem(true)) + return zstdgo.NewWriter(w, zstdgo.ProfileFastest(), func(w *zstdgo.Writer) { + w.FrameSize = 16 << 20 + w.Seekable = true + }), nil } diff --git a/cmd/catp/default.pgo b/cmd/catp/default.pgo index 1da740f..95ea4f7 100644 Binary files a/cmd/catp/default.pgo and b/cmd/catp/default.pgo differ diff --git a/go.mod b/go.mod index 15d2653..c04674c 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,16 @@ module github.com/bool64/progress go 1.26.0 require ( - github.com/DataDog/zstd v1.5.7 - github.com/bool64/dev v0.2.45 + github.com/bool64/dev v0.2.46 github.com/cloudflare/ahocorasick v0.0.0-20240916140611-054963ec9396 - github.com/klauspost/compress v1.20.3 github.com/klauspost/pgzip v1.3.0 github.com/vearutop/plot-go v0.1.1 + github.com/vearutop/zstd-go v0.0.4 golang.org/x/time v0.16.0 ) -require github.com/vearutop/statigz v1.5.0 // indirect +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/klauspost/compress v1.20.4 // indirect + github.com/vearutop/statigz v1.5.0 // indirect +) diff --git a/go.sum b/go.sum index 07b8d66..39ffca9 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,13 @@ -github.com/DataDog/zstd v1.5.7 h1:ybO8RBeh29qrxIhCA9E8gKY6xfONU9T6G6aP9DTKfLE= -github.com/DataDog/zstd v1.5.7/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= -github.com/bool64/dev v0.2.45 h1:3nLKhAS/6Oklk3Mt2lHYSN/Cb4tdAD77KLwzeP+6eYE= -github.com/bool64/dev v0.2.45/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= +github.com/bool64/dev v0.2.46 h1:oOM68buCVIZBJscLMEHfalYW/SOPfABr5grztbwBa3w= +github.com/bool64/dev v0.2.46/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cloudflare/ahocorasick v0.0.0-20240916140611-054963ec9396 h1:W2HK1IdCnCGuLUeyizSCkwvBjdj0ZL7mxnJYQ3poyzI= github.com/cloudflare/ahocorasick v0.0.0-20240916140611-054963ec9396/go.mod h1:tGWUZLZp9ajsxUOnHmFFLnqnlKXsCn6GReG4jAD59H0= -github.com/klauspost/compress v1.20.3 h1:zWNoFqq/lSuYQRim3dUXlXeA0IqwuZGicdG5ZTfY5j4= -github.com/klauspost/compress v1.20.3/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= +github.com/klauspost/compress v1.20.4 h1:dD0v/bvxtjCBqQu6sx1TNWkalEY5lNTIe+HY9w8TeNI= +github.com/klauspost/compress v1.20.4/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= github.com/klauspost/pgzip v1.3.0 h1:8t+KjjzD/0n/8vNlJGGZmTeK4njuwffOQvJm8tOSc8o= github.com/klauspost/pgzip v1.3.0/go.mod h1:Xx7tAv5DH8ObLHUDQKXoE/nd8i/FYdNDTVMvi3SLTKI= github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= @@ -16,6 +16,8 @@ github.com/vearutop/plot-go v0.1.1 h1:TU9XyjsA6zbanlpjNS/Hq80HZNao4kJAfr1k+z9k3f github.com/vearutop/plot-go v0.1.1/go.mod h1:u/PnA1M0q8hVeruxfyF9rgqlOdU+BT7z1L20OARhgTY= github.com/vearutop/statigz v1.5.0 h1:FuWwZiT82yBw4xbWdWIawiP2XFTyEPhIo8upRxiKLqk= github.com/vearutop/statigz v1.5.0/go.mod h1:oHmjFf3izfCO804Di1ZjB666P3fAlVzJEx2k6jNt/Gk= +github.com/vearutop/zstd-go v0.0.4 h1:zrOEyqX4EcpivgpdaR69SSPGXcmUHWKvD2V/zEuGZ24= +github.com/vearutop/zstd-go v0.0.4/go.mod h1:1Gji1pmd5yWRZ+pSBC6E0/RJtJMh/afR4f8S4zFA/2E= go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= golang.org/x/time v0.16.0 h1:vMb6ptszcQMkcwiRTAuNNU50gom6++Q/6gY2hDM6VDE=