Skip to content

Commit 735ed1f

Browse files
committed
Improved performance and reduced memory usage (2x faster, 3.75x less memory usage).
1 parent 70db970 commit 735ed1f

File tree

2 files changed

+5
-13
lines changed

2 files changed

+5
-13
lines changed

errors.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ func pad(buf *bytes.Buffer, pad string) {
214214
func New(args ...interface{}) *Error {
215215
err := newError(args...)
216216

217-
updateLocation(err)
218217
updateCaller(err)
219218

220219
return err
@@ -269,7 +268,6 @@ func Wrap(cause error, args ...interface{}) *Error {
269268
err := newError(args...)
270269

271270
// We have to set these again, as they'll be at the wrong depth now.
272-
updateLocation(err)
273271
updateCaller(err)
274272

275273
return err
@@ -286,15 +284,10 @@ func updateCaller(err *Error) {
286284

287285
fun := runtime.FuncForPC(fpcs[0] - 1)
288286
if fun != nil {
289-
funcName := strings.Split(fun.Name(), "/")
290-
err.caller = funcName[len(funcName)-1]
291-
}
292-
}
287+
li := strings.LastIndex(fun.Name(), "/") + 1
293288

294-
// updateLocation takes an error and sets file and line information on it. Safe to use in error
295-
// constructors, but no deeper.
296-
func updateLocation(err *Error) {
297-
_, file, line, _ := runtime.Caller(2)
298-
err.file = file
299-
err.line = line
289+
funcName := fun.Name()[li:]
290+
err.caller = funcName
291+
err.file, err.line = fun.FileLine(fpcs[0] - 1)
292+
}
300293
}

utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func Fatal(err error) {
1616

1717
wrapped := newError(err)
1818

19-
updateLocation(wrapped)
2019
updateCaller(wrapped)
2120

2221
if v, ok := err.(*Error); ok {

0 commit comments

Comments
 (0)