Skip to content

Commit

Permalink
consoleEncoder: put cloned jsonEncoder back to pool (#852)
Browse files Browse the repository at this point in the history
consoleEncoder clone a jsonEncoder in `writeContext`, but never put back to pool after use.
This make zap do more memory allocations, and may increase gc time.
  • Loading branch information
wyxloading committed Aug 5, 2020
1 parent 53a3870 commit 639461d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion zapcore/console_encoder.go
Expand Up @@ -130,7 +130,12 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,

func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
context := c.jsonEncoder.Clone().(*jsonEncoder)
defer context.buf.Free()
defer func() {
// putJSONEncoder assumes the buffer is still used, but we write out the buffer so
// we can free it.
context.buf.Free()
putJSONEncoder(context)
}()

addFields(context, extra)
context.closeOpenNamespaces()
Expand Down

0 comments on commit 639461d

Please sign in to comment.