Skip to content

Commit

Permalink
Fast path for empty buffer
Browse files Browse the repository at this point in the history
If we don't have a leftover partial write in the buffer, we can skip the
buffer completely and write directly to the logger.
  • Loading branch information
abhinav committed Jun 24, 2021
1 parent 48f2f4d commit 7c1ff63
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions zapio/writer.go
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

0 comments on commit 7c1ff63

Please sign in to comment.