bytes.Clone for slice copying #96

Closed
opened 2023-03-08 23:30:50 +00:00 by snegurochka · 2 comments

Original issue: https://github.com/nspcc-dev/neofs-node/issues/2251

Go 1.20 finally brought to us a killer feature --- bytes.Clone library function that can... copy byte slices! We can finally replace slice.Copy (but not a `slice.CopyReverse, ha-ha) with something standard.

Original issue: https://github.com/nspcc-dev/neofs-node/issues/2251 Go 1.20 finally brought to us a killer feature --- bytes.Clone library function that can... copy byte slices! We can finally replace `slice.Copy` (but not a `slice.CopyReverse, ha-ha) with something standard.
fyrchik added the
go
label 2023-05-04 06:46:28 +00:00
fyrchik added this to the vNext milestone 2023-05-18 08:42:33 +00:00
fyrchik modified the milestone from vNext to v0.37.0 2023-08-08 19:11:10 +00:00
aarifullin self-assigned this 2023-08-10 11:46:18 +00:00
Collaborator

slice.Copy from github.com/nspcc-dev/neo-go/pkg/util/slice is defined like that:

// Copy copies the byte slice into new slice (make/copy).
func Copy(b []byte) []byte {
	d := make([]byte, len(b))
	copy(d, b)
	return d
}

while bytes.Clone is

// Clone returns a copy of b[:len(b)].
// The result may have additional unused capacity.
// Clone(nil) returns nil.
func Clone(b []byte) []byte {
	if b == nil {
		return nil
	}
	return append([]byte{}, b...)
}

So, Clone does not use preallocated memory that may lead to extra memory usage:

// The result may have additional unused capacity.

I suppose this may break heuristics for GOMEMLIMIT that's used by GC and so on

WDYT?

`slice.Copy` from `github.com/nspcc-dev/neo-go/pkg/util/slice` is defined like that: ```golang // Copy copies the byte slice into new slice (make/copy). func Copy(b []byte) []byte { d := make([]byte, len(b)) copy(d, b) return d } ``` while `bytes.Clone` is ```golang // Clone returns a copy of b[:len(b)]. // The result may have additional unused capacity. // Clone(nil) returns nil. func Clone(b []byte) []byte { if b == nil { return nil } return append([]byte{}, b...) } ``` So, `Clone` does not use preallocated memory that may lead to extra memory usage: ``` // The result may have additional unused capacity. ``` I suppose this may break heuristics for `GOMEMLIMIT` that's used by GC and so on WDYT?
aarifullin added the
discussion
label 2023-08-10 14:06:16 +00:00
aarifullin removed their assignment 2023-08-10 14:09:16 +00:00
fyrchik modified the milestone from v0.37.0 to v0.38.0 2023-08-29 09:12:49 +00:00

neo-go has plans for dropping util/slice, so I suggest moving to std version. https://github.com/nspcc-dev/neo-go/issues/3089

neo-go has plans for dropping `util/slice`, so I suggest moving to std version. https://github.com/nspcc-dev/neo-go/issues/3089
dstepanov-yadro self-assigned this 2023-11-17 07:58:11 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: TrueCloudLab/frostfs-node#96
There is no content yet.