Skip to content

Commit

Permalink
Optimize zap.Any to allocate less mamory on stack
Browse files Browse the repository at this point in the history
We have identified an issue where zap.Any can cause performance
degradation due to stack size.

This is apparently cased by the compiler assigning 4.8kb (a zap.Field
per arm of the switch statement) for zap.Any on stack. This can result
in an unnecessary runtime.newstack+runtime.copystack.
A github issue against Go language is pending.

This can be particularly bad if `zap.Any` was to be used in a new
goroutine, since the default goroutine sizes can be as low as 2kb (it can
vary depending on average stack size - see golang/go#18138).

This is an alternative to #1301, @cdvr and me were talking about this,
and he inspired this idea with the closure.

By using a function and a closure we're able to reduce the size and
remove the degradation.
At least on my laptop, this change result in a new performance gain,
as all benchmarks show reduced time.

10 runs.
```
❯ benchstat ~/before.txt ~/after-after.txt
goos: darwin
goarch: arm64
pkg: go.uber.org/zap
                                       │ /Users/pawel/before.txt │     /Users/pawel/after-after.txt      │
                                       │         sec/op          │   sec/op     vs base                  │
AnyInGoroutine/any-12                                305.2n ± 3%   297.0n ± 0%        ~ (p=0.085 n=10)
AnyInGoroutine/int-12                                288.0n ± 0%   270.1n ± 1%   -6.25% (p=0.000 n=10)
AnyInGoroutine/goroutine-12                          218.3n ± 6%   209.5n ± 5%   -4.05% (p=0.015 n=10)
AnyInGoroutine/int-in-go-12                          592.7n ± 2%   573.9n ± 2%   -3.17% (p=0.000 n=10)
AnyInGoroutine/any-in-go-12                          666.5n ± 4%   552.3n ± 1%  -17.13% (p=0.000 n=10)
AnyInGoroutine/int-in-go-with-stack-12               474.4n ± 4%   459.4n ± 6%        ~ (p=0.447 n=10+9)
AnyInGoroutine/any-in-go-with-stack-12               617.6n ± 4%   468.8n ± 4%  -24.09% (p=0.000 n=10)
geomean                                              417.8n        380.1n        -9.01%

                                       │ /Users/pawel/before.txt │     /Users/pawel/after-after.txt      │
                                       │          B/op           │    B/op     vs base                   │
AnyInGoroutine/any-12                               64.00 ± 0%     64.00 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/int-12                               64.00 ± 0%     64.00 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/goroutine-12                         0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/int-in-go-12                         88.00 ± 0%     88.00 ± 0%       ~ (p=1.000 n=10)
AnyInGoroutine/any-in-go-12                         88.00 ± 0%     88.00 ± 0%       ~ (p=1.000 n=10)
AnyInGoroutine/int-in-go-with-stack-12              88.00 ± 0%     88.00 ± 0%       ~ (p=1.000 n=10+9) ¹
AnyInGoroutine/any-in-go-with-stack-12              88.00 ± 0%     88.00 ± 0%       ~ (p=1.000 n=10)   ¹
geomean                                                        ²               +0.00%                  ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                       │ /Users/pawel/before.txt │     /Users/pawel/after-after.txt      │
                                       │        allocs/op        │ allocs/op   vs base                   │
AnyInGoroutine/any-12                               1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/int-12                               1.000 ± 0%     1.000 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/goroutine-12                         0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/int-in-go-12                         2.000 ± 0%     2.000 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/any-in-go-12                         2.000 ± 0%     2.000 ± 0%       ~ (p=1.000 n=10)   ¹
AnyInGoroutine/int-in-go-with-stack-12              2.000 ± 0%     2.000 ± 0%       ~ (p=1.000 n=10+9) ¹
AnyInGoroutine/any-in-go-with-stack-12              2.000 ± 0%     2.000 ± 0%       ~ (p=1.000 n=10)   ¹
geomean                                                        ²               +0.00%                  ²
¹ all samples are equal
² summaries must be >0 to compute geomean
```
  • Loading branch information
rabbbit committed Jul 26, 2023
1 parent 382e251 commit bfeed22
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 63 deletions.
257 changes: 194 additions & 63 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,132 +418,263 @@ func Inline(val zapcore.ObjectMarshaler) Field {
// them. To minimize surprises, []byte values are treated as binary blobs, byte
// values are treated as uint8, and runes are always treated as integers.
func Any(key string, value interface{}) Field {
// To work around go compiler assigning unreasonably large space on stack
// (4kb, one `Field` per arm of the switch statement) which can trigger
// performance degradation if `Any` is used in a brand new goroutine.
var f func() Field
switch val := value.(type) {
case zapcore.ObjectMarshaler:
return Object(key, val)
f = func() Field {
return Object(key, val)
}
case zapcore.ArrayMarshaler:
return Array(key, val)
f = func() Field {
return Array(key, val)
}
case bool:
return Bool(key, val)
f = func() Field {
return Bool(key, val)
}
case *bool:
return Boolp(key, val)
f = func() Field {
return Boolp(key, val)
}
case []bool:
return Bools(key, val)
f = func() Field {
return Bools(key, val)
}
case complex128:
return Complex128(key, val)
f = func() Field {
return Complex128(key, val)
}
case *complex128:
return Complex128p(key, val)
f = func() Field {
return Complex128p(key, val)
}
case []complex128:
return Complex128s(key, val)
f = func() Field {
return Complex128s(key, val)
}
case complex64:
return Complex64(key, val)
f = func() Field {
return Complex64(key, val)
}
case *complex64:
return Complex64p(key, val)
f = func() Field {
return Complex64p(key, val)
}
case []complex64:
return Complex64s(key, val)
f = func() Field {
return Complex64s(key, val)
}
case float64:
return Float64(key, val)
f = func() Field {
return Float64(key, val)
}
case *float64:
return Float64p(key, val)
f = func() Field {
return Float64p(key, val)
}
case []float64:
return Float64s(key, val)
f = func() Field {
return Float64s(key, val)
}
case float32:
return Float32(key, val)
f = func() Field {
return Float32(key, val)
}
case *float32:
return Float32p(key, val)
f = func() Field {
return Float32p(key, val)
}
case []float32:
return Float32s(key, val)
f = func() Field {
return Float32s(key, val)
}
case int:
return Int(key, val)
f = func() Field {
return Int(key, val)
}
case *int:
return Intp(key, val)
f = func() Field {
return Intp(key, val)
}
case []int:
return Ints(key, val)
f = func() Field {
return Ints(key, val)
}
case int64:
return Int64(key, val)
f = func() Field {
return Int64(key, val)
}
case *int64:
return Int64p(key, val)
f = func() Field {
return Int64p(key, val)
}
case []int64:
return Int64s(key, val)
f = func() Field {
return Int64s(key, val)
}
case int32:
return Int32(key, val)
f = func() Field {
return Int32(key, val)
}
case *int32:
return Int32p(key, val)
f = func() Field {
return Int32p(key, val)
}
case []int32:
return Int32s(key, val)
f = func() Field {
return Int32s(key, val)
}
case int16:
return Int16(key, val)
f = func() Field {
return Int16(key, val)
}
case *int16:
return Int16p(key, val)
f = func() Field {
return Int16p(key, val)
}
case []int16:
return Int16s(key, val)
f = func() Field {
return Int16s(key, val)
}
case int8:
return Int8(key, val)
f = func() Field {
return Int8(key, val)
}
case *int8:
return Int8p(key, val)
f = func() Field {
return Int8p(key, val)
}
case []int8:
return Int8s(key, val)
f = func() Field {
return Int8s(key, val)
}
case string:
return String(key, val)
f = func() Field {
return String(key, val)
}
case *string:
return Stringp(key, val)
f = func() Field {
return Stringp(key, val)
}
case []string:
return Strings(key, val)
f = func() Field {
return Strings(key, val)
}
case uint:
return Uint(key, val)
f = func() Field {
return Uint(key, val)
}
case *uint:
return Uintp(key, val)
f = func() Field {
return Uintp(key, val)
}
case []uint:
return Uints(key, val)
f = func() Field {
return Uints(key, val)
}
case uint64:
return Uint64(key, val)
f = func() Field {
return Uint64(key, val)
}
case *uint64:
return Uint64p(key, val)
f = func() Field {
return Uint64p(key, val)
}
case []uint64:
return Uint64s(key, val)
f = func() Field {
return Uint64s(key, val)
}
case uint32:
return Uint32(key, val)
f = func() Field {
return Uint32(key, val)
}
case *uint32:
return Uint32p(key, val)
f = func() Field {
return Uint32p(key, val)
}
case []uint32:
return Uint32s(key, val)
f = func() Field {
return Uint32s(key, val)
}
case uint16:
return Uint16(key, val)
f = func() Field {
return Uint16(key, val)
}
case *uint16:
return Uint16p(key, val)
f = func() Field {
return Uint16p(key, val)
}
case []uint16:
return Uint16s(key, val)
f = func() Field {
return Uint16s(key, val)
}
case uint8:
return Uint8(key, val)
f = func() Field {
return Uint8(key, val)
}
case *uint8:
return Uint8p(key, val)
f = func() Field {
return Uint8p(key, val)
}
case []byte:
return Binary(key, val)
f = func() Field {
return Binary(key, val)
}
case uintptr:
return Uintptr(key, val)
f = func() Field {
return Uintptr(key, val)
}
case *uintptr:
return Uintptrp(key, val)
f = func() Field {
return Uintptrp(key, val)
}
case []uintptr:
return Uintptrs(key, val)
f = func() Field {
return Uintptrs(key, val)
}
case time.Time:
return Time(key, val)
f = func() Field {
return Time(key, val)
}
case *time.Time:
return Timep(key, val)
f = func() Field {
return Timep(key, val)
}
case []time.Time:
return Times(key, val)
f = func() Field {
return Times(key, val)
}
case time.Duration:
return Duration(key, val)
f = func() Field {
return Duration(key, val)
}
case *time.Duration:
return Durationp(key, val)
f = func() Field {
return Durationp(key, val)
}
case []time.Duration:
return Durations(key, val)
f = func() Field {
return Durations(key, val)
}
case error:
return NamedError(key, val)
f = func() Field {
return NamedError(key, val)
}
case []error:
return Errors(key, val)
f = func() Field {
return Errors(key, val)
}
case fmt.Stringer:
return Stringer(key, val)
f = func() Field {
return Stringer(key, val)
}
default:
return Reflect(key, val)
f = func() Field {
return Reflect(key, val)
}
}
return f()
}
Loading

0 comments on commit bfeed22

Please sign in to comment.