Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Allow disabling of stack traces #124

Closed
tebeka opened this issue Jul 5, 2017 · 4 comments
Closed

Allow disabling of stack traces #124

tebeka opened this issue Jul 5, 2017 · 4 comments

Comments

@tebeka
Copy link

tebeka commented Jul 5, 2017

Adding of stack traces have performance penalty. In #123 I show a little benchmark on performance with or without stack traces. Repeating it here below.

I know that usually errors are not in the critical path of performance, but sometime they do and I'm working on very performance oriented project right now, every ns counts :)

err_test.go

package main

import (
	"fmt"
	"testing"

	"github.com/pkg/errors"
)

var err1 = errors.New("hi")

func pkgError(err error, i int) error {
	return errors.Wrapf(err, "error number %d", i)
}

func fmtError(err error, i int) error {
	return fmt.Errorf("error number %d (%s)", i, err)
}

func BenchmarkPkg(b *testing.B) {
	for i := 0; i < b.N; i++ {
		pkgError(err1, i)
	}
}

func BenchmarkFmt(b *testing.B) {
	for i := 0; i < b.N; i++ {
		fmtError(err1, i)
	}
}

func BenchmarkPkgNoStack(b *testing.B) {
	errors.AddStack(false)
	defer errors.AddStack(true)
	for i := 0; i < b.N; i++ {
		pkgError(err1, i)
	}
}

Results

$ go test -bench . /tmp/err_test.go 
BenchmarkPkg-4          	 1000000	      1090 ns/op
BenchmarkFmt-4          	 5000000	       323 ns/op
BenchmarkPkgNoStack-4   	10000000	       212 ns/op
PASS
ok  	command-line-arguments	5.386s
@davecheney
Copy link
Member

davecheney commented Jul 5, 2017 via email

@tebeka
Copy link
Author

tebeka commented Jul 5, 2017

No worries, just an idea. Thanks for considering this.

BTW: I don't think stack traces are the only reason for this. The chaining of errors is highly value by itself.

@davecheney
Copy link
Member

If you don't want an error with a stack trace, use the stdlib fmt or errors package. If you want to wrap an error without collecting a stack, use WithMessage.

@tebeka
Copy link
Author

tebeka commented Jul 5, 2017

So I guess I'm just missing WithMessagef :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants