Skip to content

Commit 64aa7f5

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 64aa7f5

2 files changed

Lines changed: 32 additions & 36 deletions

File tree

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)