From 66f6ab453dbbb0c7f6120dc63d06072e9a2a70c4 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 27 Feb 2024 12:53:50 +0400 Subject: [PATCH] *: Use bytes.Clone to copy byte slices New stdlib function was introduced in Go 1.20. It completely replaces the previously used `slice.Copy` utility. Manual `make`+`copy` no longer needed too. Closes #2251. Signed-off-by: Leonard Lyubich --- cmd/blobovnicza-to-peapod/blobovnicza/get.go | 4 ++-- cmd/blobovnicza-to-peapod/blobovnicza/iterate_test.go | 4 ++-- pkg/local_object_storage/blobstor/peapod/peapod.go | 4 ++-- pkg/local_object_storage/metabase/list.go | 8 ++++---- pkg/local_object_storage/metabase/shard_id.go | 5 +++-- pkg/local_object_storage/metabase/storage_id.go | 4 ++-- pkg/local_object_storage/shard/range_test.go | 4 ++-- pkg/local_object_storage/writecache/flush.go | 5 ++--- pkg/local_object_storage/writecache/get.go | 5 +++-- pkg/morph/event/notary_preparator.go | 5 ++--- pkg/util/salt.go | 10 +++++----- pkg/util/state/storage.go | 4 ++-- 12 files changed, 31 insertions(+), 31 deletions(-) diff --git a/cmd/blobovnicza-to-peapod/blobovnicza/get.go b/cmd/blobovnicza-to-peapod/blobovnicza/get.go index f6dc50fb31..0ec5ba3ec3 100644 --- a/cmd/blobovnicza-to-peapod/blobovnicza/get.go +++ b/cmd/blobovnicza-to-peapod/blobovnicza/get.go @@ -1,9 +1,9 @@ package blobovnicza import ( + "bytes" "errors" - "github.com/nspcc-dev/neo-go/pkg/util/slice" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "go.etcd.io/bbolt" @@ -52,7 +52,7 @@ func (b *Blobovnicza) Get(prm GetPrm) (GetRes, error) { return nil } - data = slice.Copy(data) + data = bytes.Clone(data) return errInterruptForEach }) diff --git a/cmd/blobovnicza-to-peapod/blobovnicza/iterate_test.go b/cmd/blobovnicza-to-peapod/blobovnicza/iterate_test.go index d3dd37c14d..f8b10f3981 100644 --- a/cmd/blobovnicza-to-peapod/blobovnicza/iterate_test.go +++ b/cmd/blobovnicza-to-peapod/blobovnicza/iterate_test.go @@ -1,11 +1,11 @@ package blobovnicza import ( + "bytes" "errors" "path/filepath" "testing" - "github.com/nspcc-dev/neo-go/pkg/util/slice" oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test" "github.com/stretchr/testify/require" "go.etcd.io/bbolt" @@ -29,7 +29,7 @@ func TestBlobovniczaIterate(t *testing.T) { seen := make([][]byte, 0, 2) inc := func(e IterationElement) error { - seen = append(seen, slice.Copy(e.data)) + seen = append(seen, bytes.Clone(e.data)) return nil } diff --git a/pkg/local_object_storage/blobstor/peapod/peapod.go b/pkg/local_object_storage/blobstor/peapod/peapod.go index b579d205cd..d27ab62aac 100644 --- a/pkg/local_object_storage/blobstor/peapod/peapod.go +++ b/pkg/local_object_storage/blobstor/peapod/peapod.go @@ -1,6 +1,7 @@ package peapod import ( + "bytes" "context" "crypto/sha256" "errors" @@ -10,7 +11,6 @@ import ( "sync" "time" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/compression" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" @@ -290,7 +290,7 @@ func (x *Peapod) Get(prm common.GetPrm) (common.GetRes, error) { return apistatus.ErrObjectNotFound } - data = slice.Copy(val) + data = bytes.Clone(val) return nil }) diff --git a/pkg/local_object_storage/metabase/list.go b/pkg/local_object_storage/metabase/list.go index 006054673b..dbe74b135e 100644 --- a/pkg/local_object_storage/metabase/list.go +++ b/pkg/local_object_storage/metabase/list.go @@ -1,6 +1,8 @@ package meta import ( + "bytes" + objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" @@ -138,8 +140,7 @@ loop: if offset != nil { // new slice is much faster but less memory efficient // we need to copy, because offset exists during bbolt tx - cursor.inBucketOffset = make([]byte, len(offset)) - copy(cursor.inBucketOffset, offset) + cursor.inBucketOffset = bytes.Clone(offset) } if len(result) == 0 { @@ -148,8 +149,7 @@ loop: // new slice is much faster but less memory efficient // we need to copy, because bucketName exists during bbolt tx - cursor.bucketName = make([]byte, len(bucketName)) - copy(cursor.bucketName, bucketName) + cursor.bucketName = bytes.Clone(bucketName) return result, cursor, nil } diff --git a/pkg/local_object_storage/metabase/shard_id.go b/pkg/local_object_storage/metabase/shard_id.go index fac8a079f3..e2368a026f 100644 --- a/pkg/local_object_storage/metabase/shard_id.go +++ b/pkg/local_object_storage/metabase/shard_id.go @@ -1,7 +1,8 @@ package meta import ( - "github.com/nspcc-dev/neo-go/pkg/util/slice" + "bytes" + "go.etcd.io/bbolt" ) @@ -24,7 +25,7 @@ func (db *DB) ReadShardID() ([]byte, error) { err := db.boltDB.View(func(tx *bbolt.Tx) error { b := tx.Bucket(shardInfoBucket) if b != nil { - id = slice.Copy(b.Get(shardIDKey)) + id = bytes.Clone(b.Get(shardIDKey)) } return nil }) diff --git a/pkg/local_object_storage/metabase/storage_id.go b/pkg/local_object_storage/metabase/storage_id.go index 832a6d7db0..00a524eff9 100644 --- a/pkg/local_object_storage/metabase/storage_id.go +++ b/pkg/local_object_storage/metabase/storage_id.go @@ -1,9 +1,9 @@ package meta import ( + "bytes" "errors" - "github.com/nspcc-dev/neo-go/pkg/util/slice" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "go.etcd.io/bbolt" ) @@ -59,7 +59,7 @@ func (db *DB) storageID(tx *bbolt.Tx, addr oid.Address) ([]byte, error) { return nil, nil } - return slice.Copy(storageID), nil + return bytes.Clone(storageID), nil } // UpdateStorageIDPrm groups the parameters of UpdateStorageID operation. diff --git a/pkg/local_object_storage/shard/range_test.go b/pkg/local_object_storage/shard/range_test.go index c55403cae3..b5273765d2 100644 --- a/pkg/local_object_storage/shard/range_test.go +++ b/pkg/local_object_storage/shard/range_test.go @@ -1,12 +1,12 @@ package shard_test import ( + "bytes" "math" "path/filepath" "testing" "time" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree" @@ -87,7 +87,7 @@ func testShardGetRange(t *testing.T, hasWriteCache bool) { addPayload(obj, tc.payloadSize) addr := object.AddressOf(obj) - payload := slice.Copy(obj.Payload()) + payload := bytes.Clone(obj.Payload()) var putPrm shard.PutPrm putPrm.SetObject(obj) diff --git a/pkg/local_object_storage/writecache/flush.go b/pkg/local_object_storage/writecache/flush.go index d3e5aa6bd8..4b894be957 100644 --- a/pkg/local_object_storage/writecache/flush.go +++ b/pkg/local_object_storage/writecache/flush.go @@ -6,7 +6,6 @@ import ( "time" "github.com/mr-tron/base58" - "github.com/nspcc-dev/neo-go/pkg/util/slice" objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" @@ -97,12 +96,12 @@ func (c *cache) flushDB() { if len(lastKey) == len(k) { copy(lastKey, k) } else { - lastKey = slice.Copy(k) + lastKey = bytes.Clone(k) } m = append(m, objectInfo{ addr: string(k), - data: slice.Copy(v), + data: bytes.Clone(v), }) } return nil diff --git a/pkg/local_object_storage/writecache/get.go b/pkg/local_object_storage/writecache/get.go index 2e591fda40..3376606373 100644 --- a/pkg/local_object_storage/writecache/get.go +++ b/pkg/local_object_storage/writecache/get.go @@ -1,7 +1,8 @@ package writecache import ( - "github.com/nspcc-dev/neo-go/pkg/util/slice" + "bytes" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" @@ -59,7 +60,7 @@ func Get(db *bbolt.DB, key []byte) ([]byte, error) { if value == nil { return logicerr.Wrap(apistatus.ObjectNotFound{}) } - value = slice.Copy(value) + value = bytes.Clone(value) return nil }) return value, err diff --git a/pkg/morph/event/notary_preparator.go b/pkg/morph/event/notary_preparator.go index de960457e7..82deac93a7 100644 --- a/pkg/morph/event/notary_preparator.go +++ b/pkg/morph/event/notary_preparator.go @@ -16,7 +16,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neofs-node/pkg/morph/client" @@ -108,8 +107,8 @@ func txCopy(tx *transaction.Transaction) *transaction.Transaction { cp.Scripts = make([]transaction.Witness, len(tx.Scripts)) for i := range cp.Scripts { cp.Scripts[i] = transaction.Witness{ - InvocationScript: slice.Copy(tx.Scripts[i].InvocationScript), - VerificationScript: slice.Copy(tx.Scripts[i].VerificationScript), + InvocationScript: bytes.Clone(tx.Scripts[i].InvocationScript), + VerificationScript: bytes.Clone(tx.Scripts[i].VerificationScript), } } return &cp diff --git a/pkg/util/salt.go b/pkg/util/salt.go index b97984e70c..eb1e392eb9 100644 --- a/pkg/util/salt.go +++ b/pkg/util/salt.go @@ -1,6 +1,7 @@ package util import ( + "bytes" "io" ) @@ -12,18 +13,17 @@ func SaltXOR(data, salt []byte) []byte { // SaltXOROffset xors bits of data with salt starting from off byte // repeating salt if necessary. -func SaltXOROffset(data, salt []byte, off int) (result []byte) { - result = make([]byte, len(data)) +func SaltXOROffset(data, salt []byte, off int) []byte { ls := len(salt) if ls == 0 { - copy(result, data) - return + return bytes.Clone(data) } + result := make([]byte, len(data)) for i := range result { result[i] = data[i] ^ salt[(i+off)%ls] } - return + return result } type saltWriter struct { diff --git a/pkg/util/state/storage.go b/pkg/util/state/storage.go index e1304bfda3..72e65065a7 100644 --- a/pkg/util/state/storage.go +++ b/pkg/util/state/storage.go @@ -1,10 +1,10 @@ package state import ( + "bytes" "encoding/binary" "fmt" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "go.etcd.io/bbolt" ) @@ -97,7 +97,7 @@ func (p PersistentStorage) SetBytes(key []byte, value []byte) error { func (p PersistentStorage) Bytes(key []byte) (res []byte, err error) { err = p.lookup(key, func(v []byte) error { if v != nil { - res = slice.Copy(v) + res = bytes.Clone(v) } return nil })