Skip to content

Commit 9b2ccb4

Browse files
committed
Fix unnecessary allocation of a new state when adding new values to pcommon.Map
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
1 parent 10fd06d commit 9b2ccb4

3 files changed

Lines changed: 57 additions & 36 deletions

File tree

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: Fix unnecessary allocation of a new state when adding new values to pcommon.Map
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13634]
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: [api, user]

pdata/pcommon/map.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (m Map) PutStr(k, v string) {
130130
if av, existing := m.Get(k); existing {
131131
av.SetStr(v)
132132
} else {
133-
*m.getOrig() = append(*m.getOrig(), newKeyValueString(k, v))
133+
*m.getOrig() = append(*m.getOrig(), newKeyValueString(k, v, m.getState()))
134134
}
135135
}
136136

@@ -142,7 +142,7 @@ func (m Map) PutInt(k string, v int64) {
142142
if av, existing := m.Get(k); existing {
143143
av.SetInt(v)
144144
} else {
145-
*m.getOrig() = append(*m.getOrig(), newKeyValueInt(k, v))
145+
*m.getOrig() = append(*m.getOrig(), newKeyValueInt(k, v, m.getState()))
146146
}
147147
}
148148

@@ -154,7 +154,7 @@ func (m Map) PutDouble(k string, v float64) {
154154
if av, existing := m.Get(k); existing {
155155
av.SetDouble(v)
156156
} else {
157-
*m.getOrig() = append(*m.getOrig(), newKeyValueDouble(k, v))
157+
*m.getOrig() = append(*m.getOrig(), newKeyValueDouble(k, v, m.getState()))
158158
}
159159
}
160160

@@ -166,7 +166,7 @@ func (m Map) PutBool(k string, v bool) {
166166
if av, existing := m.Get(k); existing {
167167
av.SetBool(v)
168168
} else {
169-
*m.getOrig() = append(*m.getOrig(), newKeyValueBool(k, v))
169+
*m.getOrig() = append(*m.getOrig(), newKeyValueBool(k, v, m.getState()))
170170
}
171171
}
172172

@@ -317,3 +317,31 @@ func (m Map) Equal(val Map) bool {
317317
})
318318
return fullEqual
319319
}
320+
321+
func newKeyValueString(k, v string, state *internal.State) otlpcommon.KeyValue {
322+
orig := otlpcommon.KeyValue{Key: k}
323+
akv := newValue(&orig.Value, state)
324+
akv.SetStr(v)
325+
return orig
326+
}
327+
328+
func newKeyValueInt(k string, v int64, state *internal.State) otlpcommon.KeyValue {
329+
orig := otlpcommon.KeyValue{Key: k}
330+
akv := newValue(&orig.Value, state)
331+
akv.SetInt(v)
332+
return orig
333+
}
334+
335+
func newKeyValueDouble(k string, v float64, state *internal.State) otlpcommon.KeyValue {
336+
orig := otlpcommon.KeyValue{Key: k}
337+
akv := newValue(&orig.Value, state)
338+
akv.SetDouble(v)
339+
return orig
340+
}
341+
342+
func newKeyValueBool(k string, v bool, state *internal.State) otlpcommon.KeyValue {
343+
orig := otlpcommon.KeyValue{Key: k}
344+
akv := newValue(&orig.Value, state)
345+
akv.SetBool(v)
346+
return orig
347+
}

pdata/pcommon/value.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -455,35 +455,3 @@ func (v Value) Equal(c Value) bool {
455455

456456
return false
457457
}
458-
459-
func newKeyValueString(k, v string) otlpcommon.KeyValue {
460-
orig := otlpcommon.KeyValue{Key: k}
461-
state := internal.StateMutable
462-
akv := newValue(&orig.Value, &state)
463-
akv.SetStr(v)
464-
return orig
465-
}
466-
467-
func newKeyValueInt(k string, v int64) otlpcommon.KeyValue {
468-
orig := otlpcommon.KeyValue{Key: k}
469-
state := internal.StateMutable
470-
akv := newValue(&orig.Value, &state)
471-
akv.SetInt(v)
472-
return orig
473-
}
474-
475-
func newKeyValueDouble(k string, v float64) otlpcommon.KeyValue {
476-
orig := otlpcommon.KeyValue{Key: k}
477-
state := internal.StateMutable
478-
akv := newValue(&orig.Value, &state)
479-
akv.SetDouble(v)
480-
return orig
481-
}
482-
483-
func newKeyValueBool(k string, v bool) otlpcommon.KeyValue {
484-
orig := otlpcommon.KeyValue{Key: k}
485-
state := internal.StateMutable
486-
akv := newValue(&orig.Value, &state)
487-
akv.SetBool(v)
488-
return orig
489-
}

0 commit comments

Comments
 (0)