-
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 an option to defer expensive With operations
One common problem we see is that Loggers may be composed with one or more With() statements but never used in a non error path. For example, an author might write at the start of a function: ``` l := logger.With(zap.String("foo", "bar")) ``` but never use 'l' except in error branches. However, in the non error cases, the expensive with operation has been performed which can result in expensive (in terms of heap, CPU) cloning (e.g of the JSON encoder) for no benefit. This commit adds a new option, WithDeferred 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 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 ```
- Loading branch information
Showing
6 changed files
with
167 additions
and
2 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) *deferredWithCore { | ||
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) | ||
} |