From 3ca01f4bc352e36e9d80f87d80b7de57d967fb7a Mon Sep 17 00:00:00 2001 From: Mike Auclair Date: Mon, 24 Jun 2024 16:39:18 +0000 Subject: [PATCH] Stop querying for stack frames multiple times on CallerInfo() --- assert/assertions.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 104a0c9..cf4d80f 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -212,19 +212,23 @@ 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 := []uintptr{} + n := runtime.Callers(0, pcs) + if n == 0 { + return []string{} + } + 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 == "" { @@ -263,6 +267,10 @@ func CallerInfo() []string { isTest(name, "Example") { break } + + if !more { + break + } } return callers