-
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.
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 method on Logger, WithLazy, which defers all of this expense until the time of first use. This creates considerable performance improvement for the cases where a logger is not regularly used. This does require an explicit opt in, because the author must be aware there is the possibility of objects living longer than the scope of the method wherein WithLazy is called. Additionally, cases such as logging objects where the object is modified between the WithLazy and logging that will reflect the state of the field at log time, rather than With time, which is something authors need to be aware of. ``` % go test -bench=Benchmark5With goos: darwin goarch: arm64 pkg: go.uber.org/zap/exp/lazylogger Benchmark5WithsUsed-10 527385 2390 ns/op Benchmark5WithsNotUsed-10 563468 2246 ns/op Benchmark5WithLazysUsed-10 475064 2642 ns/op Benchmark5WithLazysNotUsed-10 2255786 527.4 ns/op PASS ``` Furthermore, one core microservice in Uber has seen a reduction of 10% heap allocation by simply using this feature.
- Loading branch information
Showing
4 changed files
with
181 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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 lazyWithCore struct { | ||
Core | ||
sync.Once | ||
fields []Field | ||
} | ||
|
||
var _ Core = (*lazyWithCore)(nil) | ||
|
||
// NewLazyWith wraps a Core with a "lazy" ore that will only encode fields if | ||
// the logger is written to (or is further chained in a lon-lazy manner). | ||
func NewLazyWith(core Core, fields []Field) Core { | ||
return &lazyWithCore{ | ||
Core: core, | ||
fields: fields, | ||
} | ||
} | ||
|
||
func (d *lazyWithCore) initOnce() { | ||
d.Once.Do(func() { | ||
d.Core = d.Core.With(d.fields) | ||
}) | ||
} | ||
|
||
func (d *lazyWithCore) With(fields []Field) Core { | ||
d.initOnce() | ||
return d.Core.With(fields) | ||
} | ||
|
||
func (d *lazyWithCore) Check(e Entry, ce *CheckedEntry) *CheckedEntry { | ||
d.initOnce() | ||
return d.Core.Check(e, ce) | ||
} |