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

Commit

Permalink
Reduce allocations (#1204)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
boekkooi-fresh authored Apr 3, 2020
1 parent d3cf45e commit 46dfec7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion trace/lrumap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (lm lruMap) len() int {
}

func (lm lruMap) keys() []interface{} {
keys := []interface{}{}
keys := make([]interface{}, len(lm.cacheKeys))
for k := range lm.cacheKeys {
keys = append(keys, k)
}
Expand Down
12 changes: 6 additions & 6 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,31 +345,31 @@ func (s *Span) SetStatus(status Status) {
}

func (s *Span) interfaceArrayToLinksArray() []Link {
linksArr := make([]Link, 0)
linksArr := make([]Link, 0, len(s.links.queue))
for _, value := range s.links.queue {
linksArr = append(linksArr, value.(Link))
}
return linksArr
}

func (s *Span) interfaceArrayToMessageEventArray() []MessageEvent {
messageEventArr := make([]MessageEvent, 0)
messageEventArr := make([]MessageEvent, 0, len(s.messageEvents.queue))
for _, value := range s.messageEvents.queue {
messageEventArr = append(messageEventArr, value.(MessageEvent))
}
return messageEventArr
}

func (s *Span) interfaceArrayToAnnotationArray() []Annotation {
annotationArr := make([]Annotation, 0)
annotationArr := make([]Annotation, 0, len(s.annotations.queue))
for _, value := range s.annotations.queue {
annotationArr = append(annotationArr, value.(Annotation))
}
return annotationArr
}

func (s *Span) lruAttributesToAttributeMap() map[string]interface{} {
attributes := make(map[string]interface{})
attributes := make(map[string]interface{}, s.lruAttributes.len())
for _, key := range s.lruAttributes.keys() {
value, ok := s.lruAttributes.get(key)
if ok {
Expand Down Expand Up @@ -420,7 +420,7 @@ func (s *Span) lazyPrintfInternal(attributes []Attribute, format string, a ...in
var m map[string]interface{}
s.mu.Lock()
if len(attributes) != 0 {
m = make(map[string]interface{})
m = make(map[string]interface{}, len(attributes))
copyAttributes(m, attributes)
}
s.annotations.add(Annotation{
Expand All @@ -436,7 +436,7 @@ func (s *Span) printStringInternal(attributes []Attribute, str string) {
var a map[string]interface{}
s.mu.Lock()
if len(attributes) != 0 {
a = make(map[string]interface{})
a = make(map[string]interface{}, len(attributes))
copyAttributes(a, attributes)
}
s.annotations.add(Annotation{
Expand Down

0 comments on commit 46dfec7

Please sign in to comment.