Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call stack perf change for CallerInfo #1614

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/stretchr/testify/assert/yaml"
)

const stackFrameBufferSize = 10

//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_format.go.tmpl"

// TestingT is an interface wrapper around *testing.T
Expand Down Expand Up @@ -212,19 +214,27 @@ the problem actually occurred in calling code.*/
func CallerInfo() []string {

var pc uintptr
var ok bool
var file string
var line int
var name string

callers := []string{}
for i := 0; ; i++ {
pc, file, line, ok = runtime.Caller(i)
if !ok {
// The breaks below failed to terminate the loop, and we ran off the
// end of the call stack.
break
}
pcs := make([]uintptr, stackFrameBufferSize)
offset := 1
n := runtime.Callers(offset, pcs)

if n == 0 {
return []string{}
}

maybeMore := n == stackFrameBufferSize
frames := runtime.CallersFrames(pcs[:n])

for {
frame, more := frames.Next()
pc = frame.PC
file = frame.File
line = frame.Line

// This is a huge edge case, but it will panic if this is the case, see #180
if file == "<autogenerated>" {
Expand Down Expand Up @@ -263,6 +273,24 @@ func CallerInfo() []string {
isTest(name, "Example") {
break
}

if more {
continue
}
// We know we already have less than a buffer's worth of frames
if !maybeMore {
break
}
offset += stackFrameBufferSize
n = runtime.Callers(offset, pcs)
if n == 0 {
break
}

maybeMore = n == stackFrameBufferSize
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying this comment from #1615 which also contains this change.

I'm not a fan of more and maybeMore, this code is actually two nested loops and I think it would be more readable if you coded it that way. I think that pure behaviour is usually better than data (varibles) used only to alter behaviour.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed a commit that moves this to two loops (though still using more to break out of the inner loop). LMK if that's along the lines of what you were thinking.


frames = runtime.CallersFrames(pcs[:n])

}

return callers
Expand Down