Skip to content

Commit

Permalink
*: Use bytes.Clone to copy byte slices
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cthulhu-rider committed Feb 29, 2024
1 parent 8eb706c commit 66f6ab4
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions cmd/blobovnicza-to-peapod/blobovnicza/get.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (b *Blobovnicza) Get(prm GetPrm) (GetRes, error) {
return nil
}

data = slice.Copy(data)
data = bytes.Clone(data)

return errInterruptForEach
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/blobovnicza-to-peapod/blobovnicza/iterate_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/blobstor/peapod/peapod.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package peapod

import (
"bytes"
"context"
"crypto/sha256"
"errors"
Expand All @@ -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"
Expand Down Expand Up @@ -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
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/local_object_storage/metabase/list.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/local_object_storage/metabase/shard_id.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package meta

import (
"github.com/nspcc-dev/neo-go/pkg/util/slice"
"bytes"

"go.etcd.io/bbolt"
)

Expand All @@ -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
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/metabase/storage_id.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/shard/range_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions pkg/local_object_storage/writecache/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pkg/local_object_storage/writecache/get.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/morph/event/notary_preparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/util/salt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"bytes"
"io"
)

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/state/storage.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package state

import (
"bytes"
"encoding/binary"
"fmt"

"github.com/nspcc-dev/neo-go/pkg/util/slice"
"go.etcd.io/bbolt"
)

Expand Down Expand Up @@ -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
})
Expand Down

0 comments on commit 66f6ab4

Please sign in to comment.