New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add WithLazy Logger method #1319
Merged
Merged
Commits on Sep 8, 2023
-
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.
Configuration menu - View commit details
-
Copy full SHA for f600804 - Browse repository at this point
Copy the full SHA f600804View commit details
Commits on Sep 10, 2023
-
Explicitly place table tests in named variable
This makes for slightly more readable code.
Configuration menu - View commit details
-
Copy full SHA for edc75e6 - Browse repository at this point
Copy the full SHA edc75e6View commit details -
Configuration menu - View commit details
-
Copy full SHA for c3f187d - Browse repository at this point
Copy the full SHA c3f187dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 7389c46 - Browse repository at this point
Copy the full SHA 7389c46View commit details -
Configuration menu - View commit details
-
Copy full SHA for c3eec45 - Browse repository at this point
Copy the full SHA c3eec45View commit details -
Add coverage of chained With/WithLazy combinations
Test and assert behaviour of .With.WithLazy, .WithLazy.WithLazy, .WithLazy.With combinations.
Configuration menu - View commit details
-
Copy full SHA for cfeb077 - Browse repository at this point
Copy the full SHA cfeb077View commit details -
doc(WithLazy): Split into multiple paragraphs
Split the doc strings into multiple paragraphs, adjust the wording.
Configuration menu - View commit details
-
Copy full SHA for 1d9848c - Browse repository at this point
Copy the full SHA 1d9848cView commit details -
Configuration menu - View commit details
-
Copy full SHA for f5b6618 - Browse repository at this point
Copy the full SHA f5b6618View commit details
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.