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.

This improves performance when log statements aren't split across too many
writes.

```
name             old time/op    new time/op    delta
Writer/single-4     422ns ±21%     383ns ± 2%  -9.26%  (p=0.000 n=10+9)
Writer/splits-4     433ns ± 4%     435ns ± 1%    ~     (p=0.236 n=9+8)

name             old alloc/op   new alloc/op   delta
Writer/single-4     16.0B ± 0%     16.0B ± 0%    ~     (all equal)
Writer/splits-4     16.0B ± 0%     16.0B ± 0%    ~     (all equal)

name             old allocs/op  new allocs/op  delta
Writer/single-4      2.00 ± 0%      2.00 ± 0%    ~     (all equal)
Writer/splits-4      2.00 ± 0%      2.00 ± 0%    ~     (all equal)
```
  • Loading branch information
abhinav committed Jun 24, 2021
1 parent a17fef8 commit 0c8d989
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 0c8d989

Please sign in to comment.