Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] primitive slice, avoid allocation if enough space #6061

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.