Skip to content

Commit 88c1a61

Browse files
RaduBerindeianlancetaylor
authored andcommitted
trace: don't allocate all events upfront
SetMaxEvent allocates storage for all events, which is very expensive if we want to set a high value; even with small values, the unnecessary allocation of the initial slice is measurable. We improve the performance by having a few events preallocated as part of the trace, and appending as necessary. We also co-locate the flags in event which makes it smaller (by a word). We also add a set of benchmarks; before and after amortized cost per-event is shown: name old time/op new time/op delta Trace/0-2-32 1.19µs ± 1% 0.96µs ± 0% -18.94% (p=0.008 n=5+5) Trace/0-10-32 579ns ± 0% 635ns ± 1% +9.82% (p=0.008 n=5+5) Trace/0-100-32 463ns ± 1% 466ns ± 1% ~ (p=0.190 n=5+5) Trace/0-1000-32 451ns ± 1% 451ns ± 0% ~ (p=0.984 n=5+5) Trace/0-10000-32 451ns ± 2% 449ns ± 1% ~ (p=0.492 n=5+5) Trace/10-2-32 1.41µs ± 1% 0.96µs ± 1% -31.74% (p=0.008 n=5+5) Trace/10-10-32 623ns ± 1% 634ns ± 1% +1.73% (p=0.008 n=5+5) Trace/10-100-32 469ns ± 1% 466ns ± 1% ~ (p=0.357 n=5+5) Trace/10-1000-32 452ns ± 1% 453ns ± 2% ~ (p=0.913 n=5+5) Trace/10-10000-32 451ns ± 1% 449ns ± 1% ~ (p=0.175 n=5+5) Trace/100-2-32 2.78µs ± 2% 0.97µs ± 1% -65.28% (p=0.008 n=5+5) Trace/100-10-32 912ns ± 1% 637ns ± 1% -30.23% (p=0.008 n=5+5) Trace/100-100-32 479ns ± 1% 541ns ± 0% +12.77% (p=0.008 n=5+5) Trace/100-1000-32 510ns ± 0% 541ns ± 1% +6.08% (p=0.008 n=5+5) Trace/100-10000-32 514ns ± 1% 540ns ± 0% +5.14% (p=0.008 n=5+5) Trace/1000-2-32 17.2µs ± 1% 1.0µs ± 1% -94.39% (p=0.008 n=5+5) Trace/1000-10-32 3.90µs ± 1% 0.64µs ± 0% -83.68% (p=0.008 n=5+5) Trace/1000-100-32 814ns ± 1% 542ns ± 0% -33.46% (p=0.008 n=5+5) Trace/1000-1000-32 503ns ± 1% 581ns ± 0% +15.56% (p=0.008 n=5+5) Trace/1000-10000-32 1.28µs ± 2% 1.03µs ± 1% -19.68% (p=0.008 n=5+5) Change-Id: If7c68bb1809fb92fa5d06cb6640be5e09e1f131f Reviewed-on: https://go-review.googlesource.com/30374 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 2a824cf commit 88c1a61

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

trace/trace.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ func New(family, title string) Trace {
333333
tr.ref()
334334
tr.Family, tr.Title = family, title
335335
tr.Start = time.Now()
336-
tr.events = make([]event, 0, maxEventsPerTrace)
336+
tr.maxEvents = maxEventsPerTrace
337+
tr.events = tr.eventsBuf[:0]
337338

338339
activeMu.RLock()
339340
s := activeTraces[tr.Family]
@@ -650,8 +651,8 @@ type event struct {
650651
Elapsed time.Duration // since previous event in trace
651652
NewDay bool // whether this event is on a different day to the previous event
652653
Recyclable bool // whether this event was passed via LazyLog
653-
What interface{} // string or fmt.Stringer
654654
Sensitive bool // whether this event contains sensitive information
655+
What interface{} // string or fmt.Stringer
655656
}
656657

657658
// WhenString returns a string representation of the elapsed time of the event.
@@ -692,14 +693,17 @@ type trace struct {
692693
IsError bool
693694

694695
// Append-only sequence of events (modulo discards).
695-
mu sync.RWMutex
696-
events []event
696+
mu sync.RWMutex
697+
events []event
698+
maxEvents int
697699

698700
refs int32 // how many buckets this is in
699701
recycler func(interface{})
700702
disc discarded // scratch space to avoid allocation
701703

702704
finishStack []byte // where finish was called, if DebugUseAfterFinish is set
705+
706+
eventsBuf [4]event // preallocated buffer in case we only log a few events
703707
}
704708

705709
func (tr *trace) reset() {
@@ -711,11 +715,15 @@ func (tr *trace) reset() {
711715
tr.traceID = 0
712716
tr.spanID = 0
713717
tr.IsError = false
718+
tr.maxEvents = 0
714719
tr.events = nil
715720
tr.refs = 0
716721
tr.recycler = nil
717722
tr.disc = 0
718723
tr.finishStack = nil
724+
for i := range tr.eventsBuf {
725+
tr.eventsBuf[i] = event{}
726+
}
719727
}
720728

721729
// delta returns the elapsed time since the last event or the trace start,
@@ -753,11 +761,11 @@ func (tr *trace) addEvent(x interface{}, recyclable, sensitive bool) {
753761
e := event{When: time.Now(), What: x, Recyclable: recyclable, Sensitive: sensitive}
754762
tr.mu.Lock()
755763
e.Elapsed, e.NewDay = tr.delta(e.When)
756-
if len(tr.events) < cap(tr.events) {
764+
if len(tr.events) < tr.maxEvents {
757765
tr.events = append(tr.events, e)
758766
} else {
759767
// Discard the middle events.
760-
di := int((cap(tr.events) - 1) / 2)
768+
di := int((tr.maxEvents - 1) / 2)
761769
if d, ok := tr.events[di].What.(*discarded); ok {
762770
(*d)++
763771
} else {
@@ -777,7 +785,7 @@ func (tr *trace) addEvent(x interface{}, recyclable, sensitive bool) {
777785
go tr.recycler(tr.events[di+1].What)
778786
}
779787
copy(tr.events[di+1:], tr.events[di+2:])
780-
tr.events[cap(tr.events)-1] = e
788+
tr.events[tr.maxEvents-1] = e
781789
}
782790
tr.mu.Unlock()
783791
}
@@ -803,7 +811,7 @@ func (tr *trace) SetTraceInfo(traceID, spanID uint64) {
803811
func (tr *trace) SetMaxEvents(m int) {
804812
// Always keep at least three events: first, discarded count, last.
805813
if len(tr.events) == 0 && m > 3 {
806-
tr.events = make([]event, 0, m)
814+
tr.maxEvents = m
807815
}
808816
}
809817

trace/trace_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package trace
66

77
import (
8+
"fmt"
89
"net/http"
910
"reflect"
1011
"testing"
@@ -69,3 +70,27 @@ func TestAuthRequest(t *testing.T) {
6970
}
7071
}
7172
}
73+
74+
func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
75+
numSpans := (b.N + numEvents + 1) / numEvents
76+
77+
for i := 0; i < numSpans; i++ {
78+
tr := New("test", "test")
79+
tr.SetMaxEvents(maxEvents)
80+
for j := 0; j < numEvents; j++ {
81+
tr.LazyPrintf("%d", j)
82+
}
83+
tr.Finish()
84+
}
85+
}
86+
87+
func BenchmarkTrace(b *testing.B) {
88+
for _, maxEvents := range []int{0, 10, 100, 1000} {
89+
for _, numEvents := range []int{2, 10, 100, 1000, 10000} {
90+
name := fmt.Sprintf("%d-%d", maxEvents, numEvents)
91+
b.Run(name, func(b *testing.B) {
92+
benchmarkTrace(b, maxEvents, numEvents)
93+
})
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)