-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to defer With composition to first log
One common performance problem we see is that Loggers and their children may be composed with one or more With() invocations, but the logger/s is/are not used in most invocations of the function. For example, an author might initialise a logger with some function-scoped contexttual information at the start of a function: ``` l := logger.With(zap.String("tripID", trip.ID)) ``` but never use 'l' except in seldom taken branches. However, in the majority cases the expensive with operation has been performed which can result in the wasted effort (in terms of alloc, CPU) of cloning (e.g of the JSON encoder). This commit adds a new option, DeferWith which defers all of this expense until the time of first use. This creates considerable performance improvement in the non error path at the slight expense of performance in the first-use path over the existing implementation: ``` % go test -bench=Benchmark5Withs goos: darwin goarch: arm64 pkg: go.uber.org/zap Benchmark5WithsUsed-10 492462 2328 ns/op Benchmark5WithsNotUsed-10 548709 2284 ns/op Benchmark5WithsDeferredUsed-10 469576 2565 ns/op Benchmark5WithsDeferredNotUsed-10 2590400 465.7 ns/op ``` Furthermore, one core microservice in Uber has seen a reduction of 10% heap allocation by simply using this option.
- Loading branch information
Showing
6 changed files
with
161 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2023 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zapcore | ||
|
||
import "sync" | ||
|
||
type deferredWithCore struct { | ||
Core | ||
sync.Once | ||
fields []Field | ||
} | ||
|
||
var ( | ||
_ Core = (*deferredWithCore)(nil) | ||
_ leveledEnabler = (*deferredWithCore)(nil) | ||
) | ||
|
||
// NewDeferredWith wraps a Core with a "lazy" core that will. | ||
// only execute With operations if the logger is actually used | ||
func NewDeferredWith(core Core, fields []Field) Core { | ||
return &deferredWithCore{ | ||
Core: core, | ||
fields: fields, | ||
} | ||
} | ||
|
||
func (d *deferredWithCore) initOnce() { | ||
d.Once.Do(func() { | ||
d.Core = d.Core.With(d.fields) | ||
}) | ||
} | ||
|
||
func (d *deferredWithCore) With(fields []Field) Core { | ||
d.initOnce() | ||
return d.Core.With(fields) | ||
} | ||
|
||
func (d *deferredWithCore) Check(e Entry, ce *CheckedEntry) *CheckedEntry { | ||
d.initOnce() | ||
return d.Core.Check(e, ce) | ||
} | ||
|
||
func (d *deferredWithCore) Write(e Entry, fields []Field) error { | ||
d.initOnce() | ||
return d.Core.Write(e, fields) | ||
} | ||
|
||
func (d *deferredWithCore) Sync() error { | ||
d.initOnce() | ||
return d.Core.Sync() | ||
} | ||
|
||
func (d *deferredWithCore) Enabled(l Level) bool { | ||
d.initOnce() | ||
return d.Core.Enabled(l) | ||
} | ||
|
||
func (d *deferredWithCore) Level() Level { | ||
d.initOnce() | ||
return LevelOf(d.Core) | ||
} |