Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 46dfec7

Browse files
Reduce allocations (#1204)
When creating a copy of a slice/map we already know the size/capacity that the target slice/map needs to be. By using make and providing the capacity argument we avoid allocation memory to grow the slice/map.
1 parent d3cf45e commit 46dfec7

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

trace/lrumap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (lm lruMap) len() int {
4444
}
4545

4646
func (lm lruMap) keys() []interface{} {
47-
keys := []interface{}{}
47+
keys := make([]interface{}, len(lm.cacheKeys))
4848
for k := range lm.cacheKeys {
4949
keys = append(keys, k)
5050
}

trace/trace.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,31 +345,31 @@ func (s *Span) SetStatus(status Status) {
345345
}
346346

347347
func (s *Span) interfaceArrayToLinksArray() []Link {
348-
linksArr := make([]Link, 0)
348+
linksArr := make([]Link, 0, len(s.links.queue))
349349
for _, value := range s.links.queue {
350350
linksArr = append(linksArr, value.(Link))
351351
}
352352
return linksArr
353353
}
354354

355355
func (s *Span) interfaceArrayToMessageEventArray() []MessageEvent {
356-
messageEventArr := make([]MessageEvent, 0)
356+
messageEventArr := make([]MessageEvent, 0, len(s.messageEvents.queue))
357357
for _, value := range s.messageEvents.queue {
358358
messageEventArr = append(messageEventArr, value.(MessageEvent))
359359
}
360360
return messageEventArr
361361
}
362362

363363
func (s *Span) interfaceArrayToAnnotationArray() []Annotation {
364-
annotationArr := make([]Annotation, 0)
364+
annotationArr := make([]Annotation, 0, len(s.annotations.queue))
365365
for _, value := range s.annotations.queue {
366366
annotationArr = append(annotationArr, value.(Annotation))
367367
}
368368
return annotationArr
369369
}
370370

371371
func (s *Span) lruAttributesToAttributeMap() map[string]interface{} {
372-
attributes := make(map[string]interface{})
372+
attributes := make(map[string]interface{}, s.lruAttributes.len())
373373
for _, key := range s.lruAttributes.keys() {
374374
value, ok := s.lruAttributes.get(key)
375375
if ok {
@@ -420,7 +420,7 @@ func (s *Span) lazyPrintfInternal(attributes []Attribute, format string, a ...in
420420
var m map[string]interface{}
421421
s.mu.Lock()
422422
if len(attributes) != 0 {
423-
m = make(map[string]interface{})
423+
m = make(map[string]interface{}, len(attributes))
424424
copyAttributes(m, attributes)
425425
}
426426
s.annotations.add(Annotation{
@@ -436,7 +436,7 @@ func (s *Span) printStringInternal(attributes []Attribute, str string) {
436436
var a map[string]interface{}
437437
s.mu.Lock()
438438
if len(attributes) != 0 {
439-
a = make(map[string]interface{})
439+
a = make(map[string]interface{}, len(attributes))
440440
copyAttributes(a, attributes)
441441
}
442442
s.annotations.add(Annotation{

0 commit comments

Comments
 (0)