Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Any stack usage with map + list of lookups #1307

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 90 additions & 127 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package zap
import (
"fmt"
"math"
"reflect"
"time"

"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -410,6 +411,80 @@ func Inline(val zapcore.ObjectMarshaler) Field {
}
}

type anyFuncType func(string, any) Field

func typedToAnyFunc[T any](f func(string, T) Field) anyFuncType {
return func(k string, v any) Field {
return f(k, v.(T))
}
}

// anyTypeLookup is a map from concrete types to the field constructors.
var anyTypeLookup = buildAnyTypeLookup()
var anyInterfaceLookup = buildAnyInterfaceLookup()

type interfaceMatcher struct {
match func(any) bool
field func(string, any) Field
}

func addTypeLookup[T any](
m map[reflect.Type]anyFuncType,
directTypeFn func(string, T) Field,
ptrTypeFn func(string, *T) Field,
arrayTypeFn func(string, []T) Field,
) {
var v T
m[reflect.TypeOf(v)] = typedToAnyFunc(directTypeFn)
m[reflect.TypeOf(&v)] = typedToAnyFunc(ptrTypeFn)
m[reflect.TypeOf([]T(nil))] = typedToAnyFunc(arrayTypeFn)
}

func buildAnyTypeLookup() map[reflect.Type]anyFuncType {
m := make(map[reflect.Type]anyFuncType)
addTypeLookup[bool](m, Bool, Boolp, Bools)
addTypeLookup[complex128](m, Complex128, Complex128p, Complex128s)
addTypeLookup[complex64](m, Complex64, Complex64p, Complex64s)
addTypeLookup[float64](m, Float64, Float64p, Float64s)
addTypeLookup[float32](m, Float32, Float32p, Float32s)
addTypeLookup[int](m, Int, Intp, Ints)
addTypeLookup[int64](m, Int64, Int64p, Int64s)
addTypeLookup[int32](m, Int32, Int32p, Int32s)
addTypeLookup[int16](m, Int16, Int16p, Int16s)
addTypeLookup[int8](m, Int8, Int8p, Int8s)
addTypeLookup[string](m, String, Stringp, Strings)
addTypeLookup[uint](m, Uint, Uintp, Uints)
addTypeLookup[uint64](m, Uint64, Uint64p, Uint64s)
addTypeLookup[uint32](m, Uint32, Uint32p, Uint32s)
addTypeLookup[uint16](m, Uint16, Uint16p, Uint16s)
addTypeLookup[uint8](m, Uint8, Uint8p, Uint8s)
addTypeLookup[uintptr](m, Uintptr, Uintptrp, Uintptrs)
addTypeLookup[time.Time](m, Time, Timep, Times)
addTypeLookup[time.Duration](m, Duration, Durationp, Durations)
m[reflect.TypeOf([]byte(nil))] = typedToAnyFunc(Binary)
m[reflect.TypeOf([]error(nil))] = typedToAnyFunc(Errors)
return m
}

func buildInterfaceMatcher[T any](typedFn func(string, T) Field) interfaceMatcher {
return interfaceMatcher{
match: func(v any) bool {
_, ok := v.(T)
return ok
},
field: typedToAnyFunc(typedFn),
}
}

func buildAnyInterfaceLookup() []interfaceMatcher {
return []interfaceMatcher{
buildInterfaceMatcher[zapcore.ObjectMarshaler](Object),
buildInterfaceMatcher[zapcore.ArrayMarshaler](Array),
buildInterfaceMatcher[error](NamedError),
buildInterfaceMatcher[fmt.Stringer](Stringer),
}
}

// Any takes a key and an arbitrary value and chooses the best way to represent
// them as a field, falling back to a reflection-based approach only if
// necessary.
Expand All @@ -418,132 +493,20 @@ 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 {
switch val := value.(type) {
case zapcore.ObjectMarshaler:
return Object(key, val)
case zapcore.ArrayMarshaler:
return Array(key, val)
case bool:
return Bool(key, val)
case *bool:
return Boolp(key, val)
case []bool:
return Bools(key, val)
case complex128:
return Complex128(key, val)
case *complex128:
return Complex128p(key, val)
case []complex128:
return Complex128s(key, val)
case complex64:
return Complex64(key, val)
case *complex64:
return Complex64p(key, val)
case []complex64:
return Complex64s(key, val)
case float64:
return Float64(key, val)
case *float64:
return Float64p(key, val)
case []float64:
return Float64s(key, val)
case float32:
return Float32(key, val)
case *float32:
return Float32p(key, val)
case []float32:
return Float32s(key, val)
case int:
return Int(key, val)
case *int:
return Intp(key, val)
case []int:
return Ints(key, val)
case int64:
return Int64(key, val)
case *int64:
return Int64p(key, val)
case []int64:
return Int64s(key, val)
case int32:
return Int32(key, val)
case *int32:
return Int32p(key, val)
case []int32:
return Int32s(key, val)
case int16:
return Int16(key, val)
case *int16:
return Int16p(key, val)
case []int16:
return Int16s(key, val)
case int8:
return Int8(key, val)
case *int8:
return Int8p(key, val)
case []int8:
return Int8s(key, val)
case string:
return String(key, val)
case *string:
return Stringp(key, val)
case []string:
return Strings(key, val)
case uint:
return Uint(key, val)
case *uint:
return Uintp(key, val)
case []uint:
return Uints(key, val)
case uint64:
return Uint64(key, val)
case *uint64:
return Uint64p(key, val)
case []uint64:
return Uint64s(key, val)
case uint32:
return Uint32(key, val)
case *uint32:
return Uint32p(key, val)
case []uint32:
return Uint32s(key, val)
case uint16:
return Uint16(key, val)
case *uint16:
return Uint16p(key, val)
case []uint16:
return Uint16s(key, val)
case uint8:
return Uint8(key, val)
case *uint8:
return Uint8p(key, val)
case []byte:
return Binary(key, val)
case uintptr:
return Uintptr(key, val)
case *uintptr:
return Uintptrp(key, val)
case []uintptr:
return Uintptrs(key, val)
case time.Time:
return Time(key, val)
case *time.Time:
return Timep(key, val)
case []time.Time:
return Times(key, val)
case time.Duration:
return Duration(key, val)
case *time.Duration:
return Durationp(key, val)
case []time.Duration:
return Durations(key, val)
case error:
return NamedError(key, val)
case []error:
return Errors(key, val)
case fmt.Stringer:
return Stringer(key, val)
default:
return Reflect(key, val)
fn, ok := anyTypeLookup[reflect.TypeOf(value)]

if !ok {
for i := range anyInterfaceLookup {
if anyInterfaceLookup[i].match(value) {
fn = anyInterfaceLookup[i].field
break
}
}
}

if fn == nil {
fn = Reflect
}

return fn(key, value)
}
4 changes: 4 additions & 0 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestFieldConstructors(t *testing.T) {
uint16Val = uint16(1)
uint8Val = uint8(1)
uintptrVal = uintptr(1)
nilErr error
)

tests := []struct {
Expand Down Expand Up @@ -166,6 +167,7 @@ func TestFieldConstructors(t *testing.T) {
{"Any:Uintptr", Any("k", uintptr(1)), Uintptr("k", 1)},
{"Any:Uintptrs", Any("k", []uintptr{1}), Uintptrs("k", []uintptr{1})},
{"Any:Time", Any("k", time.Unix(0, 0)), Time("k", time.Unix(0, 0))},
{"Any:Time-FullType", Any("k", time.Time{}), Time("k", time.Time{})},
{"Any:Times", Any("k", []time.Time{time.Unix(0, 0)}), Times("k", []time.Time{time.Unix(0, 0)})},
{"Any:Duration", Any("k", time.Second), Duration("k", time.Second)},
{"Any:Durations", Any("k", []time.Duration{time.Second}), Durations("k", []time.Duration{time.Second})},
Expand Down Expand Up @@ -222,6 +224,7 @@ func TestFieldConstructors(t *testing.T) {
{"Ptr:Time", Timep("k", &timeVal), Time("k", timeVal)},
{"Any:PtrTime", Any("k", (*time.Time)(nil)), nilField("k")},
{"Any:PtrTime", Any("k", &timeVal), Time("k", timeVal)},
{"Any:PtrTime-FullType", Any("k", &time.Time{}), Time("k", time.Time{})},
{"Ptr:Uint", Uintp("k", nil), nilField("k")},
{"Ptr:Uint", Uintp("k", &uintVal), Uint("k", uintVal)},
{"Any:PtrUint", Any("k", (*uint)(nil)), nilField("k")},
Expand All @@ -246,6 +249,7 @@ func TestFieldConstructors(t *testing.T) {
{"Ptr:Uintptr", Uintptrp("k", &uintptrVal), Uintptr("k", uintptrVal)},
{"Any:PtrUintptr", Any("k", (*uintptr)(nil)), nilField("k")},
{"Any:PtrUintptr", Any("k", &uintptrVal), Uintptr("k", uintptrVal)},
{"Any:Error-nil", Any("k", nilErr), nilField("k")},
{"Namespace", Namespace("k"), Field{Key: "k", Type: zapcore.NamespaceType}},
}

Expand Down
107 changes: 107 additions & 0 deletions logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package zap

import (
"errors"
"runtime"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -238,3 +240,108 @@ func Benchmark100Fields(b *testing.B) {
logger.With(first...).Info("Child loggers with lots of context.", second...)
}
}

func dummy(wg *sync.WaitGroup, s string, i int) string {
if i == 0 {
wg.Wait()
return "1" + s
}
return dummy(wg, s, i-1)
}

func stackGrower(n int) *sync.WaitGroup {
wg := sync.WaitGroup{}
wg.Add(1)

go dummy(&wg, "hi", n)
return &wg
}

func BenchmarkAny(b *testing.B) {
logger := New(
zapcore.NewCore(
zapcore.NewJSONEncoder(NewProductionConfig().EncoderConfig),
&ztest.Discarder{},
DebugLevel,
),
)

b.Run("str-no-logger", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
f := String("some-string-longer-than-16", "yet-another-long-string")
runtime.KeepAlive(f)
}
})
b.Run("any-no-logger", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
f := Any("some-string-longer-than-16", "yet-another-long-string")
runtime.KeepAlive(f)
}
})
b.Run("str-with-logger", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("", String("some-string-longer-than-16", "yet-another-long-string"))
}
})
b.Run("any-with-logger", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("", String("some-string-longer-than-16", "yet-another-long-string"))
}
})
b.Run("str-in-go", func(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", String("some-string-longer-than-16", "yet-another-long-string"))
wg.Done()
}()
}
wg.Wait()
})
b.Run("any-in-go", func(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Any("some-string-longer-than-16", "yet-another-long-string"))
wg.Done()
}()
}
wg.Wait()
})
b.Run("str-in-go-with-stack", func(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(b.N)
defer stackGrower(2000).Done()
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", String("some-string-longer-than-16", "yet-another-long-string"))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
b.Run("any-in-go-with-stack", func(b *testing.B) {
wg := sync.WaitGroup{}
wg.Add(b.N)
defer stackGrower(2000).Done()
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
logger.Info("", Any("some-string-longer-than-16", "yet-another-long-string"))
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
})
}