Skip to content

Commit 998beaf

Browse files
committed
Merge branch 'cstockton-master'
2 parents 584cbac + ee4766c commit 998beaf

File tree

2 files changed

+96
-13
lines changed

2 files changed

+96
-13
lines changed

bench_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func yesErrors(at, depth int) error {
2525

2626
// GlobalE is an exported global to store the result of benchmark results,
2727
// preventing the compiler from optimising the benchmark functions away.
28-
var GlobalE error
28+
var GlobalE interface{}
2929

3030
func BenchmarkErrors(b *testing.B) {
3131
type run struct {
@@ -61,3 +61,50 @@ func BenchmarkErrors(b *testing.B) {
6161
})
6262
}
6363
}
64+
65+
func BenchmarkStackFormatting(b *testing.B) {
66+
type run struct {
67+
stack int
68+
format string
69+
}
70+
runs := []run{
71+
{10, "%s"},
72+
{10, "%v"},
73+
{10, "%+v"},
74+
{30, "%s"},
75+
{30, "%v"},
76+
{30, "%+v"},
77+
{60, "%s"},
78+
{60, "%v"},
79+
{60, "%+v"},
80+
}
81+
82+
var stackStr string
83+
for _, r := range runs {
84+
name := fmt.Sprintf("%s-stack-%d", r.format, r.stack)
85+
b.Run(name, func(b *testing.B) {
86+
err := yesErrors(0, r.stack)
87+
b.ReportAllocs()
88+
b.ResetTimer()
89+
for i := 0; i < b.N; i++ {
90+
stackStr = fmt.Sprintf(r.format, err)
91+
}
92+
b.StopTimer()
93+
})
94+
}
95+
96+
for _, r := range runs {
97+
name := fmt.Sprintf("%s-stacktrace-%d", r.format, r.stack)
98+
b.Run(name, func(b *testing.B) {
99+
err := yesErrors(0, r.stack)
100+
st := err.(*fundamental).stack.StackTrace()
101+
b.ReportAllocs()
102+
b.ResetTimer()
103+
for i := 0; i < b.N; i++ {
104+
stackStr = fmt.Sprintf(r.format, st)
105+
}
106+
b.StopTimer()
107+
})
108+
}
109+
GlobalE = stackStr
110+
}

stack.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package errors
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"path"
78
"runtime"
9+
"strconv"
810
"strings"
911
)
1012

@@ -24,33 +26,40 @@ type Frame runtime.Frame
2426
// GOPATH separated by \n\t (<funcname>\n\t<path>)
2527
// %+v equivalent to %+s:%d
2628
func (f Frame) Format(s fmt.State, verb rune) {
29+
f.format(s, s, verb)
30+
}
31+
32+
// format allows stack trace printing calls to be made with a bytes.Buffer.
33+
func (f Frame) format(w io.Writer, s fmt.State, verb rune) {
2734
switch verb {
2835
case 's':
2936
switch {
3037
case s.Flag('+'):
3138
fn := runtime.Frame(f).Func
3239
if fn == nil {
33-
io.WriteString(s, "unknown")
40+
io.WriteString(w, "unknown")
3441
} else {
3542
file := runtime.Frame(f).File
36-
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
43+
io.WriteString(w, fn.Name())
44+
io.WriteString(w, "\n\t")
45+
io.WriteString(w, file)
3746
}
3847
default:
3948
file := runtime.Frame(f).File
4049
if file == "" {
4150
file = "unknown"
4251
}
43-
io.WriteString(s, path.Base(file))
52+
io.WriteString(w, path.Base(file))
4453
}
4554
case 'd':
46-
fmt.Fprintf(s, "%d", runtime.Frame(f).Line)
55+
io.WriteString(w, strconv.Itoa(runtime.Frame(f).Line))
4756
case 'n':
4857
name := runtime.Frame(f).Function
4958
io.WriteString(s, funcname(name))
5059
case 'v':
51-
f.Format(s, 's')
52-
io.WriteString(s, ":")
53-
f.Format(s, 'd')
60+
f.format(w, s, 's')
61+
io.WriteString(w, ":")
62+
f.format(w, s, 'd')
5463
}
5564
}
5665

@@ -66,23 +75,50 @@ type StackTrace []Frame
6675
//
6776
// %+v Prints filename, function, and line number for each Frame in the stack.
6877
func (st StackTrace) Format(s fmt.State, verb rune) {
78+
var b bytes.Buffer
6979
switch verb {
7080
case 'v':
7181
switch {
7282
case s.Flag('+'):
73-
for _, f := range st {
74-
fmt.Fprintf(s, "\n%+v", f)
83+
b.Grow(len(st) * stackMinLen)
84+
for _, fr := range st {
85+
b.WriteByte('\n')
86+
fr.format(&b, s, verb)
7587
}
7688
case s.Flag('#'):
77-
fmt.Fprintf(s, "%#v", []Frame(st))
89+
fmt.Fprintf(&b, "%#v", []Frame(st))
7890
default:
79-
fmt.Fprintf(s, "%v", []Frame(st))
91+
st.formatSlice(&b, s, verb)
8092
}
8193
case 's':
82-
fmt.Fprintf(s, "%s", []Frame(st))
94+
st.formatSlice(&b, s, verb)
95+
}
96+
io.Copy(s, &b)
97+
}
98+
99+
// formatSlice will format this StackTrace into the given buffer as a slice of
100+
// Frame, only valid when called with '%s' or '%v'.
101+
func (st StackTrace) formatSlice(b *bytes.Buffer, s fmt.State, verb rune) {
102+
b.WriteByte('[')
103+
if len(st) == 0 {
104+
b.WriteByte(']')
105+
return
106+
}
107+
108+
b.Grow(len(st) * (stackMinLen / 4))
109+
st[0].format(b, s, verb)
110+
for _, fr := range st[1:] {
111+
b.WriteByte(' ')
112+
fr.format(b, s, verb)
83113
}
114+
b.WriteByte(']')
84115
}
85116

117+
// stackMinLen is a best-guess at the minimum length of a stack trace. It
118+
// doesn't need to be exact, just give a good enough head start for the buffer
119+
// to avoid the expensive early growth.
120+
const stackMinLen = 96
121+
86122
// stack represents a stack of program counters.
87123
type stack []uintptr
88124

0 commit comments

Comments
 (0)