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

Conversation

mikeauclair
Copy link

@mikeauclair mikeauclair commented Jun 24, 2024

Summary

  • Switches CallerInfo to use runtime.Callers() to pull the stack instead of repeatedly calling runtime.Caller()

Changes

  • Buffer 10 stack frames at a time by switching to a series of calls to Callers() instead of repeated Caller invocations, because there is a cost to querying the stack
  • Iterate over the frames with the frames.Next() iterator

Motivation

  • We heavily use mocks at DevotedHealth and the repeated calls to pull the stack were flagging as a contributor when I profiled some of our slow tests - see example of Caller contributing a CPU cost of 7660ms out of a total cost of 13740ms
Screenshot 2024-06-24 at 1 10 26 PM

Related issues

assert/assertions.go Outdated Show resolved Hide resolved
assert/assertions.go Outdated Show resolved Hide resolved
@mikeauclair mikeauclair requested a review from ccoVeille June 25, 2024 13:44
Comment on lines 277 to 290
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.

@brackendawson
Copy link
Collaborator

The performance enhancement in this code is a change I'd be happy to include, with some slight alteration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants