diff --git a/CHANGELOG.md b/CHANGELOG.md index fb101c321..bb2d871b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Changelog for NeoFS Node ### Removed ### Updated - `neo-go` to `v0.100.1` +- `github.com/klauspost/compress` to `v1.15.13` ### Updating from v0.35.0 diff --git a/go.mod b/go.mod index 41bc5548d..9ad0843db 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/google/go-github/v39 v39.2.0 github.com/google/uuid v1.3.0 github.com/hashicorp/golang-lru v0.5.4 - github.com/klauspost/compress v1.15.9 + github.com/klauspost/compress v1.15.13 github.com/mitchellh/go-homedir v1.1.0 github.com/mr-tron/base58 v1.2.0 github.com/multiformats/go-multiaddr v0.4.0 diff --git a/go.sum b/go.sum index 2837e39d3..5487b0e87 100644 Binary files a/go.sum and b/go.sum differ diff --git a/pkg/local_object_storage/blobstor/compression/bench_test.go b/pkg/local_object_storage/blobstor/compression/bench_test.go new file mode 100644 index 000000000..6e05366cf --- /dev/null +++ b/pkg/local_object_storage/blobstor/compression/bench_test.go @@ -0,0 +1,49 @@ +package compression + +import ( + "crypto/rand" + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +func BenchmarkCompression(b *testing.B) { + c := Config{Enabled: true} + require.NoError(b, c.Init()) + + for _, size := range []int{128, 1024, 32 * 1024, 32 * 1024 * 1024} { + b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) { + b.Run("zeroed slice", func(b *testing.B) { + data := make([]byte, size) + benchWith(b, c, data) + }) + b.Run("not so random slice (block = 123)", func(b *testing.B) { + data := notSoRandomSlice(size, 123) + benchWith(b, c, data) + }) + b.Run("random slice", func(b *testing.B) { + data := make([]byte, size) + rand.Read(data) + benchWith(b, c, data) + }) + }) + } +} + +func benchWith(b *testing.B, c Config, data []byte) { + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _ = c.Compress(data) + } +} + +func notSoRandomSlice(size, blockSize int) []byte { + data := make([]byte, size) + rand.Read(data[:blockSize]) + for i := blockSize; i < size; i += blockSize { + copy(data[i:], data[:blockSize]) + } + return data +} diff --git a/pkg/local_object_storage/blobstor/compression/compress.go b/pkg/local_object_storage/blobstor/compression/compress.go index 44b83d416..6cab3c29c 100644 --- a/pkg/local_object_storage/blobstor/compression/compress.go +++ b/pkg/local_object_storage/blobstor/compression/compress.go @@ -86,7 +86,8 @@ func (c *Config) Compress(data []byte) []byte { if c == nil || !c.Enabled { return data } - return c.encoder.EncodeAll(data, make([]byte, 0, len(data))) + maxSize := c.encoder.MaxEncodedSize(len(data)) + return c.encoder.EncodeAll(data, make([]byte, 0, maxSize)) } // Close closes encoder and decoder, returns any error occurred.