-
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 495187 2463 ns/op Benchmark5WithsNotUsed-10 530983 2379 ns/op Benchmark5WithsDeferredUsed-10 956476 1161 ns/op Benchmark5WithsDeferredNotUsed-10 2602353 462.9 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
163 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,81 @@ | ||
// 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 { | ||
allFields := make([]Field, 0, len(fields)+len(d.fields)) | ||
allFields = append(allFields, d.fields...) | ||
allFields = append(allFields, fields...) | ||
return d | ||
} | ||
|
||
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) | ||
} |