summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWarnar Boekkooi <wbo@hellofresh.com>2020-04-03 02:47:29 +0200
committerGitHub <noreply@github.com>2020-04-02 17:47:29 -0700
commit46dfec7deb6e8c5d4a46f355c0da7c6d6dc59ba4 (patch)
treea293f8178a22b6ea92c51c75b68fcd115ae23d2f
parentd3cf45e7d045b133e91dd98a8a695fd04d6f2d80 (diff)
downloadopencensus-go-46dfec7deb6e8c5d4a46f355c0da7c6d6dc59ba4.tar.gz
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.
-rw-r--r--trace/lrumap.go2
-rw-r--r--trace/trace.go12
2 files changed, 7 insertions, 7 deletions
diff --git a/trace/lrumap.go b/trace/lrumap.go
index dc7a295..908c249 100644
--- a/trace/lrumap.go
+++ b/trace/lrumap.go
@@ -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)
}
diff --git a/trace/trace.go b/trace/trace.go
index 3f8977b..125e2cd 100644
--- a/trace/trace.go
+++ b/trace/trace.go
@@ -345,7 +345,7 @@ 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))
}
@@ -353,7 +353,7 @@ func (s *Span) interfaceArrayToLinksArray() []Link {
}
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))
}
@@ -361,7 +361,7 @@ func (s *Span) interfaceArrayToMessageEventArray() []MessageEvent {
}
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))
}
@@ -369,7 +369,7 @@ func (s *Span) interfaceArrayToAnnotationArray() []Annotation {
}
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 {
@@ -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{
@@ -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{