Skip to content

Allow disabling of stack traces #124

Closed

Description

@tebeka

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

Activity

  1. davecheney commented on Jul 5, 2017

    @davecheney
    Member
  2. tebeka commented on Jul 5, 2017

    @tebeka
    Author

    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.

  3. davecheney commented on Jul 5, 2017

    @davecheney
    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.

  4. tebeka commented on Jul 5, 2017

    @tebeka
    Author

    So I guess I'm just missing WithMessagef :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址