Skip to content

Commit

Permalink
*: Convert slices to arrays instead of copy where possible
Browse files Browse the repository at this point in the history
Became possible with Go 1.20.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Jul 5, 2024
1 parent 0efacfb commit b56a732
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 27 deletions.
4 changes: 2 additions & 2 deletions container/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func (id *ID) Decode(src []byte) error {
return fmt.Errorf("invalid length %d", len(src))
}

copy(id[:], src)
*id = ID(src)

return nil
}

// SetSHA256 sets container identifier value to SHA256 checksum of container structure.
func (id *ID) SetSHA256(v [sha256.Size]byte) {
copy(id[:], v[:])
*id = v

Check warning on line 75 in container/id/id.go

View check run for this annotation

Codecov / codecov/patch

container/id/id.go#L75

Added line #L75 was not covered by tests
}

// Equals defines a comparison relation between two ID instances.
Expand Down
5 changes: 1 addition & 4 deletions eacl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ func NewTableFromV2(table *v2acl.Table) *Table {
t.cid = new(cid.ID)
}

var h [sha256.Size]byte

copy(h[:], id.GetValue())
t.cid.SetSHA256(h)
t.cid.SetSHA256([sha256.Size]byte(id.GetValue()))
}

// set eacl records
Expand Down
8 changes: 4 additions & 4 deletions object/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func (id *ID) Decode(src []byte) error {
return fmt.Errorf("invalid length %d", len(src))
}

copy(id[:], src)
*id = ID(src)

return nil
}

// SetSHA256 sets object identifier value to SHA256 checksum.
func (id *ID) SetSHA256(v [sha256.Size]byte) {
copy(id[:], v[:])
*id = v
}

// Equals defines a comparison relation between two ID instances.
Expand Down Expand Up @@ -141,7 +141,7 @@ func (id *ID) Unmarshal(data []byte) error {
return err
}

copy(id[:], v2.GetValue())
*id = ID(v2.GetValue())

return nil
}
Expand All @@ -161,7 +161,7 @@ func (id *ID) UnmarshalJSON(data []byte) error {
return err
}

copy(id[:], v2.GetValue())
*id = ID(v2.GetValue())

return nil
}
10 changes: 2 additions & 8 deletions object/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,8 @@ func TestSearchFilters_AddPayloadHashFilter(t *testing.T) {
func ExampleSearchFilters_AddPayloadHashFilter() {
hash, _ := hex.DecodeString("66842cfea090b1d906b52400fae49d86df078c0670f2bdd059ba289ebe24a498")

var v [sha256.Size]byte
copy(v[:], hash[:sha256.Size])

var cs checksum.Checksum
cs.SetSHA256(v)
cs.SetSHA256([sha256.Size]byte(hash))

fmt.Println(hex.EncodeToString(cs.Value()))
// Output: 66842cfea090b1d906b52400fae49d86df078c0670f2bdd059ba289ebe24a498
Expand Down Expand Up @@ -377,11 +374,8 @@ func TestSearchFilters_AddHomomorphicHashFilter(t *testing.T) {
func ExampleSearchFilters_AddHomomorphicHashFilter() {
hash, _ := hex.DecodeString("7e302ebb3937e810feb501965580c746048db99cebd095c3ce27022407408bf904dde8d9aa8085d2cf7202345341cc947fa9d722c6b6699760d307f653815d0c")

var v [tz.Size]byte
copy(v[:], hash[:tz.Size])

var cs checksum.Checksum
cs.SetTillichZemor(v)
cs.SetTillichZemor([tz.Size]byte(hash))

fmt.Println(hex.EncodeToString(cs.Value()))
// Output: 7e302ebb3937e810feb501965580c746048db99cebd095c3ce27022407408bf904dde8d9aa8085d2cf7202345341cc947fa9d722c6b6699760d307f653815d0c
Expand Down
10 changes: 2 additions & 8 deletions object/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,17 +600,11 @@ func (x *PayloadWriter) _writeChild(ctx context.Context, meta dynamicObjectMetad
func flushObjectMetadata(signer neofscrypto.Signer, meta dynamicObjectMetadata, header *object.Object) (oid.ID, error) {
var cs checksum.Checksum

var csBytes [sha256.Size]byte
copy(csBytes[:], meta.checksum.Sum(nil))

cs.SetSHA256(csBytes)
cs.SetSHA256([sha256.Size]byte(meta.checksum.Sum(nil)))
header.SetPayloadChecksum(cs)

if meta.homomorphicChecksum != nil {
var csHomoBytes [tz.Size]byte
copy(csHomoBytes[:], meta.homomorphicChecksum.Sum(nil))

cs.SetTillichZemor(csHomoBytes)
cs.SetTillichZemor([tz.Size]byte(meta.homomorphicChecksum.Sum(nil)))
header.SetPayloadHomomorphicHash(cs)
}

Expand Down
2 changes: 1 addition & 1 deletion session/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type contextReader func(session.TokenContext, bool) error

func (x commonData) copyTo(dst *commonData) {
dst.idSet = x.idSet
copy(dst.id[:], x.id[:])
dst.id = x.id

dst.issuerSet = x.issuerSet
iss := x.issuer
Expand Down

0 comments on commit b56a732

Please sign in to comment.