From 7c1ff638d23033ec82fb595a0a1ea6490e7354bb Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Thu, 24 Jun 2021 13:44:30 -0700 Subject: [PATCH] Fast path for empty buffer If we don't have a leftover partial write in the buffer, we can skip the buffer completely and write directly to the logger. --- zapio/writer.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/zapio/writer.go b/zapio/writer.go index 8c367d7f2..29699c86d 100644 --- a/zapio/writer.go +++ b/zapio/writer.go @@ -72,6 +72,14 @@ func (w *Writer) writeLine(line []byte) (remaining []byte) { // Split on the newline, buffer and flush the left. line, remaining = line[:idx], line[idx+1:] + + // Fast path: if we don't have a partial message from a previous write + // in the buffer, skip the buffer and log directly. + if w.buff.Len() == 0 { + w.log(line) + return + } + w.buff.Write(line) // Log empty messages in the middle of the stream so that we don't lose @@ -100,9 +108,13 @@ func (w *Writer) Sync() error { // if the bool is set. func (w *Writer) flush(allowEmpty bool) { if allowEmpty || w.buff.Len() > 0 { - if ce := w.Log.Check(w.Level, w.buff.String()); ce != nil { - ce.Write() - } + w.log(w.buff.Bytes()) } w.buff.Reset() } + +func (w *Writer) log(b []byte) { + if ce := w.Log.Check(w.Level, string(b)); ce != nil { + ce.Write() + } +}