Skip to content

Commit 9b8207f

Browse files
authored
Improve RemoveIf for slices to not reference anymore the removed memory (#13522)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 79e6823 commit 9b8207f

File tree

63 files changed

+343
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+343
-0
lines changed

.chloggen/rm-refs.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pdata
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Improve RemoveIf for slices to not reference anymore the removed memory
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13522]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [user]

internal/cmd/pdatagen/internal/templates/slice.go.tmpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ func (es {{ .structName }}) RemoveIf(f func({{ .elementName }}) bool) {
133133
newLen := 0
134134
for i := 0; i < len(*es.{{ .origAccessor }}); i++ {
135135
if f(es.At(i)) {
136+
{{- if eq .type "sliceOfPtrs" }}
137+
(*es.{{ .origAccessor }})[i] = nil
138+
{{- else }}
139+
(*es.{{ .origAccessor }})[i] = {{ .emptyOriginElement }}
140+
{{- end }}
136141
continue
137142
}
138143
if newLen == i {
@@ -141,6 +146,11 @@ func (es {{ .structName }}) RemoveIf(f func({{ .elementName }}) bool) {
141146
continue
142147
}
143148
(*es.{{ .origAccessor }})[newLen] = (*es.{{ .origAccessor }})[i]
149+
{{- if eq .type "sliceOfPtrs" }}
150+
(*es.{{ .origAccessor }})[i] = nil
151+
{{- else }}
152+
(*es.{{ .origAccessor }})[i] = {{ .emptyOriginElement }}
153+
{{- end }}
144154
newLen++
145155
}
146156
*es.{{ .origAccessor }} = (*es.{{ .origAccessor }})[:newLen]

internal/cmd/pdatagen/internal/templates/slice_test.go.tmpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ func Test{{ .structName }}_RemoveIf(t *testing.T) {
143143
assert.Equal(t, 5, filtered.Len())
144144
}
145145

146+
func Test{{ .structName }}_RemoveIfAll(t *testing.T) {
147+
got := generateTest{{ .structName }}()
148+
got.RemoveIf(func(el {{ .elementName }}) bool {
149+
return true
150+
})
151+
assert.Equal(t, 0, got.Len())
152+
}
153+
146154
func Test{{ .structName }}All(t *testing.T) {
147155
ms := generateTest{{ .structName }}()
148156
assert.NotEmpty(t, ms.Len())

pdata/plog/generated_logrecordslice.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_logrecordslice_test.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_resourcelogsslice.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_resourcelogsslice_test.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_scopelogsslice.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_scopelogsslice_test.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pmetric/generated_exemplarslice.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)