Repository navigation
Conversation
Logger.With, logf.With and NewBag keep the fields and the slice and byte data they point to without copying, and the built-in encoders may cache what they encoded, so data changed after With may or may not reach the output. The docs of With, NewBag, Group and the slice-backed field constructors now say so and ask callers not to modify that data.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Logger.With,logf.WithandNewBagkeep the field slice, and the data behindBytes,ByteString,Strings,Ints,Ints64,Floats64,DurationsandGroup(Intson 64-bit platforms), by reference. The data is read during encoding, and the built-in encoders may cache and reuse the result. So if the caller changes that data afterWith, the output depends on what was encoded and cached before:Nothing in the docs says this. The
Bag.Withdoc says "zero copies", but not what that means for the caller.Change
Documentation only.
NewBagdescribes the contract: the data is kept by reference, read during encoding and possibly cached, so it must not change after it is passed in.Bag.With,Logger.Withandlogf.Withrepeat the rule in one line and point toNewBag.Groupand the slice-backed field constructors say not to modify the data until the log call returns, or at all once the field is passed toWith.zap's
WithLazyalso keeps references and evaluates them later; in logf this is howWithalways works. Copying the data inWithwas tried in #9 and dropped: it adds work to everyWith, and reference semantics with a documented contract were chosen instead.Testing
go build ./...andgolangci-lint runpass. No code changes.