Skip to content

Commit

Permalink
Merge pull request go-kit#28 from ChrisHines/with-fewer-allocs
Browse files Browse the repository at this point in the history
Reduce allocations performed by the Logger returned from log.With.
  • Loading branch information
peterbourgon committed Apr 20, 2015
2 parents 4727efa + eb1fea7 commit 657b8d3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
41 changes: 33 additions & 8 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,32 @@ func With(logger Logger, keyvals ...interface{}) Logger {
if w, ok := logger.(Wither); ok {
return w.With(keyvals...)
}
return LoggerFunc(func(kvs ...interface{}) error {
// Limiting the capacity of the first argument to append ensures that
// a new backing array is created if the slice must grow. Using the
// extra capacity without copying risks a data race that would violate
// the Logger interface contract.
n := len(keyvals)
return logger.Log(append(keyvals[:n:n], kvs...)...)
})
// Limiting the capacity of the stored keyvals ensures that a new
// backing array is created if the slice must grow in Log or With.
// Using the extra capacity without copying risks a data race that
// would violate the Logger interface contract.
n := len(keyvals)
return &withLogger{
logger: logger,
keyvals: keyvals[:n:n],
}
}

type withLogger struct {
logger Logger
keyvals []interface{}
}

func (l *withLogger) Log(keyvals ...interface{}) error {
return l.logger.Log(append(l.keyvals, keyvals...)...)
}

func (l *withLogger) With(keyvals ...interface{}) Logger {
n := len(l.keyvals) + len(keyvals)
return &withLogger{
logger: l.logger,
keyvals: append(l.keyvals, keyvals...)[:n:n],
}
}

// LoggerFunc is an adapter to allow use of ordinary functions as Loggers. If
Expand All @@ -49,3 +67,10 @@ func (f LoggerFunc) Log(keyvals ...interface{}) error {
type Wither interface {
With(keyvals ...interface{}) Logger
}

// NewDiscardLogger returns a logger that does not log anything.
func NewDiscardLogger() Logger {
return LoggerFunc(func(...interface{}) error {
return nil
})
}
43 changes: 43 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,46 @@ func TestWithConcurrent(t *testing.T) {
}
}
}

func BenchmarkDiscard(b *testing.B) {
logger := log.NewDiscardLogger()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Log("k", "v")
}
}

func BenchmarkOneWith(b *testing.B) {
logger := log.NewDiscardLogger()
logger = log.With(logger, "k", "v")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Log("k", "v")
}
}

func BenchmarkTwoWith(b *testing.B) {
logger := log.NewDiscardLogger()
for i := 0; i < 2; i++ {
logger = log.With(logger, "k", "v")
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Log("k", "v")
}
}

func BenchmarkTenWith(b *testing.B) {
logger := log.NewDiscardLogger()
for i := 0; i < 10; i++ {
logger = log.With(logger, "k", "v")
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Log("k", "v")
}
}

0 comments on commit 657b8d3

Please sign in to comment.