Skip to content

Commit

Permalink
[chore] primitive slice, avoid allocation if enough space (#6061)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan <[email protected]>

Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu authored Sep 13, 2022
1 parent a95999c commit 98c787a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
17 changes: 6 additions & 11 deletions pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func New${structName}() ${structName} {
// AsRaw returns a copy of the []${itemType} slice.
func (ms ${structName}) AsRaw() []${itemType} {
return copy${structName}(*ms.getOrig())
return copy${structName}(nil, *ms.getOrig())
}
// FromRaw copies raw []${itemType} into the slice ${structName}.
func (ms ${structName}) FromRaw(val []${itemType}) {
*ms.getOrig() = copy${structName}(val)
*ms.getOrig() = copy${structName}(*ms.getOrig(), val)
}
// Len returns length of the []${itemType} slice value.
Expand All @@ -69,17 +69,12 @@ func (ms ${structName}) MoveTo(dest ${structName}) {
// CopyTo copies ${structName} to another instance.
func (ms ${structName}) CopyTo(dest ${structName}) {
*dest.getOrig() = copy${structName}(*ms.getOrig())
*dest.getOrig() = copy${structName}(*dest.getOrig(), *ms.getOrig())
}
func copy${structName}(from []${itemType}) []${itemType} {
if len(from) == 0 {
return nil
}
to := make([]${itemType}, len(from))
copy(to, from)
return to
func copy${structName}(dst, src []${itemType}) []${itemType} {
dst = dst[:0]
return append(dst, src...)
}`

const immutableSliceTestTemplate = `func TestNew${structName}(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pdata/pcommon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ type ImmutableByteSlice = ByteSlice
// NewImmutableByteSlice creates a new ByteSlice by copying the provided []byte slice.
// Deprecated: [0.60.0] Use New{structName}() and {structName}.FromRaw([]byte) instead
func NewImmutableByteSlice(from []byte) ByteSlice {
orig := copyByteSlice(from)
orig := copyByteSlice(nil, from)
return ByteSlice(internal.NewByteSlice(&orig))
}

Expand All @@ -1056,7 +1056,7 @@ type ImmutableFloat64Slice = Float64Slice
// NewImmutableFloat64Slice creates a new Float64Slice by copying the provided []float64 slice.
// Deprecated: [0.60.0] Use New{structName}() and {structName}.FromRaw([]float64) instead
func NewImmutableFloat64Slice(from []float64) Float64Slice {
orig := copyFloat64Slice(from)
orig := copyFloat64Slice(nil, from)
return Float64Slice(internal.NewFloat64Slice(&orig))
}

Expand All @@ -1066,6 +1066,6 @@ type ImmutableUInt64Slice = UInt64Slice
// NewImmutableUInt64Slice creates a new UInt64Slice by copying the provided []uint64 slice.
// Deprecated: [0.60.0] Use New{structName}() and {structName}.FromRaw([]uint64) instead
func NewImmutableUInt64Slice(from []uint64) UInt64Slice {
orig := copyUInt64Slice(from)
orig := copyUInt64Slice(nil, from)
return UInt64Slice(internal.NewUInt64Slice(&orig))
}
51 changes: 18 additions & 33 deletions pdata/pcommon/generated_primitive_slice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98c787a

Please sign in to comment.