Skip to content

Commit ae2cee4

Browse files
committed
builder by value
pass builder by value, therefore it is not allocated benchmark old ns/op new ns/op delta BenchmarkAllocProperty/props0 100 83.3 -16.70% BenchmarkAllocProperty/props1 319 296 -7.21% BenchmarkAllocProperty/props2 628 606 -3.50% BenchmarkAllocProperty/props3 1033 1009 -2.32% BenchmarkAllocProperty/props4 1485 1439 -3.10% BenchmarkAllocProperty/props5 2012 1952 -2.98% BenchmarkAllocProperty/props6 2573 2486 -3.38% BenchmarkAllocProperty/props7 3201 3115 -2.69% BenchmarkAllocProperty/props8 3829 3751 -2.04% benchmark old allocs new allocs delta BenchmarkAllocProperty/props0 2 1 -50.00% BenchmarkAllocProperty/props1 5 4 -20.00% BenchmarkAllocProperty/props2 8 7 -12.50% BenchmarkAllocProperty/props3 11 10 -9.09% BenchmarkAllocProperty/props4 14 13 -7.14% BenchmarkAllocProperty/props5 17 16 -5.88% BenchmarkAllocProperty/props6 20 19 -5.00% BenchmarkAllocProperty/props7 23 22 -4.35% BenchmarkAllocProperty/props8 26 25 -3.85% benchmark old bytes new bytes delta BenchmarkAllocProperty/props0 176 96 -45.45% BenchmarkAllocProperty/props1 672 592 -11.90% BenchmarkAllocProperty/props2 1168 1088 -6.85% BenchmarkAllocProperty/props3 1664 1584 -4.81% BenchmarkAllocProperty/props4 2160 2080 -3.70% BenchmarkAllocProperty/props5 2656 2576 -3.01% BenchmarkAllocProperty/props6 3152 3072 -2.54% BenchmarkAllocProperty/props7 3648 3568 -2.19% BenchmarkAllocProperty/props8 4144 4064 -1.93%
1 parent 8fb51e4 commit ae2cee4

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

builder.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ type ErrorBuilder struct {
1818
}
1919

2020
// NewErrorBuilder creates error builder from an existing error type.
21-
func NewErrorBuilder(t *Type) *ErrorBuilder {
21+
func NewErrorBuilder(t *Type) ErrorBuilder {
2222
getMode := func() callStackBuildMode {
2323
if !t.modifiers.CollectStackTrace() {
2424
return stackTraceOmit
2525
}
2626
return stackTraceCollect
2727
}
2828

29-
return &ErrorBuilder{
29+
return ErrorBuilder{
3030
errorType: t,
3131
mode: getMode(),
3232
isTransparent: t.modifiers.Transparent(),
@@ -37,7 +37,7 @@ func NewErrorBuilder(t *Type) *ErrorBuilder {
3737
// For non-errorx errors, a stack trace is collected.
3838
// Otherwise, it is inherited by default, as error wrapping is typically performed 'en passe'.
3939
// Note that even if an original error explicitly omitted the stack trace, it could be added on wrap.
40-
func (eb *ErrorBuilder) WithCause(err error) *ErrorBuilder {
40+
func (eb ErrorBuilder) WithCause(err error) ErrorBuilder {
4141
eb.cause = err
4242
if Cast(err) != nil {
4343
eb.mode = stackTraceBorrow
@@ -50,7 +50,7 @@ func (eb *ErrorBuilder) WithCause(err error) *ErrorBuilder {
5050
// Transparent wrap hides the current error type from the type checks and exposes the error type of the cause instead.
5151
// The same holds true for traits, and the dynamic properties are visible from both cause and transparent wrapper.
5252
// Note that if the cause error is non-errorx, transparency will still hold, type check against wrapper will still fail.
53-
func (eb *ErrorBuilder) Transparent() *ErrorBuilder {
53+
func (eb ErrorBuilder) Transparent() ErrorBuilder {
5454
if eb.cause == nil {
5555
panic("wrong builder usage: wrap modifier without non-nil cause")
5656
}
@@ -64,7 +64,7 @@ func (eb *ErrorBuilder) Transparent() *ErrorBuilder {
6464
// This is typically a way to handle an error received from another goroutine - say, a worker pool.
6565
// When stack traces overlap, formatting makes a conservative attempt not to repeat itself,
6666
// preserving the *original* stack trace in its entirety.
67-
func (eb *ErrorBuilder) EnhanceStackTrace() *ErrorBuilder {
67+
func (eb ErrorBuilder) EnhanceStackTrace() ErrorBuilder {
6868
if eb.cause == nil {
6969
panic("wrong builder usage: wrap modifier without non-nil cause")
7070
}
@@ -81,7 +81,7 @@ func (eb *ErrorBuilder) EnhanceStackTrace() *ErrorBuilder {
8181
// WithConditionallyFormattedMessage provides a message for an error in flexible format, to simplify its usages.
8282
// Without args, leaves the original message intact, so a message may be generated or provided externally.
8383
// With args, a formatting is performed, and it is therefore expected a format string to be constant.
84-
func (eb *ErrorBuilder) WithConditionallyFormattedMessage(message string, args ...interface{}) *ErrorBuilder {
84+
func (eb ErrorBuilder) WithConditionallyFormattedMessage(message string, args ...interface{}) ErrorBuilder {
8585
if len(args) == 0 {
8686
eb.message = message
8787
} else {
@@ -92,7 +92,7 @@ func (eb *ErrorBuilder) WithConditionallyFormattedMessage(message string, args .
9292
}
9393

9494
// Create returns an error with specified params.
95-
func (eb *ErrorBuilder) Create() *Error {
95+
func (eb ErrorBuilder) Create() *Error {
9696
return &Error{
9797
errorType: eb.errorType,
9898
message: eb.message,
@@ -112,7 +112,7 @@ const (
112112
stackTraceOmit callStackBuildMode = 4
113113
)
114114

115-
func (eb *ErrorBuilder) assembleStackTrace() *stackTrace {
115+
func (eb ErrorBuilder) assembleStackTrace() *stackTrace {
116116
switch eb.mode {
117117
case stackTraceCollect:
118118
return eb.collectOriginalStackTrace()
@@ -127,19 +127,19 @@ func (eb *ErrorBuilder) assembleStackTrace() *stackTrace {
127127
}
128128
}
129129

130-
func (eb *ErrorBuilder) collectOriginalStackTrace() *stackTrace {
130+
func (eb ErrorBuilder) collectOriginalStackTrace() *stackTrace {
131131
return collectStackTrace()
132132
}
133133

134-
func (eb *ErrorBuilder) borrowStackTraceFromCause() *stackTrace {
134+
func (eb ErrorBuilder) borrowStackTraceFromCause() *stackTrace {
135135
originalStackTrace := eb.extractStackTraceFromCause(eb.cause)
136136
if originalStackTrace != nil {
137137
return originalStackTrace
138138
}
139139
return collectStackTrace()
140140
}
141141

142-
func (eb *ErrorBuilder) combineStackTraceWithCause() *stackTrace {
142+
func (eb ErrorBuilder) combineStackTraceWithCause() *stackTrace {
143143
currentStackTrace := collectStackTrace()
144144

145145
originalStackTrace := eb.extractStackTraceFromCause(eb.cause)
@@ -150,7 +150,7 @@ func (eb *ErrorBuilder) combineStackTraceWithCause() *stackTrace {
150150
return currentStackTrace
151151
}
152152

153-
func (eb *ErrorBuilder) extractStackTraceFromCause(cause error) *stackTrace {
153+
func (eb ErrorBuilder) extractStackTraceFromCause(cause error) *stackTrace {
154154
if typedCause := Cast(cause); typedCause != nil {
155155
return typedCause.stackTrace
156156
}

0 commit comments

Comments
 (0)