Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
refactor: reduce logger calls in hot path
Browse files Browse the repository at this point in the history
```
                    │   old.txt   │              new.txt               │
                    │   sec/op    │   sec/op     vs base               │
Tailer_Tail/32-12     959.6µ ± 3%   704.0µ ± 2%  -26.64% (p=0.002 n=6)
Tailer_Tail/128-12    1.391m ± 2%   1.111m ± 1%  -20.14% (p=0.002 n=6)
Tailer_Tail/512-12    4.063m ± 1%   3.797m ± 2%   -6.54% (p=0.002 n=6)
Tailer_Tail/1024-12   7.874m ± 1%   7.637m ± 2%   -3.00% (p=0.002 n=6)
Tailer_Tail/4096-12   29.39m ± 1%   29.56m ± 3%        ~ (p=0.310 n=6)
geomean               4.166m        3.675m       -11.78%

                    │   old.txt    │               new.txt               │
                    │     B/s      │     B/s       vs base               │
Tailer_Tail/32-12     1.640Gi ± 3%   2.235Gi ± 2%  +36.30% (p=0.002 n=6)
Tailer_Tail/128-12    4.423Gi ± 2%   5.538Gi ± 1%  +25.21% (p=0.002 n=6)
Tailer_Tail/512-12    6.021Gi ± 1%   6.443Gi ± 2%   +7.00% (p=0.002 n=6)
Tailer_Tail/1024-12   6.208Gi ± 1%   6.400Gi ± 2%   +3.10% (p=0.002 n=6)
Tailer_Tail/4096-12   6.647Gi ± 1%   6.609Gi ± 3%        ~ (p=0.310 n=6)
geomean               4.479Gi        5.077Gi       +13.36%

                    │   old.txt    │               new.txt                │
                    │     B/op     │     B/op      vs base                │
Tailer_Tail/32-12     113.2Ki ± 0%   113.2Ki ± 0%       ~ (p=0.617 n=6)
Tailer_Tail/128-12    113.1Ki ± 0%   113.1Ki ± 0%       ~ (p=0.924 n=6)
Tailer_Tail/512-12    113.1Ki ± 0%   113.1Ki ± 0%       ~ (p=1.000 n=6)
Tailer_Tail/1024-12   113.1Ki ± 0%   113.1Ki ± 1%       ~ (p=1.000 n=6)
Tailer_Tail/4096-12   113.1Ki ± 0%   113.1Ki ± 0%       ~ (p=1.000 n=6) ¹
geomean               113.2Ki        113.2Ki       -0.00%
¹ all samples are equal

                    │  old.txt   │              new.txt               │
                    │ allocs/op  │ allocs/op   vs base                │
Tailer_Tail/32-12     16.00 ± 0%   16.00 ± 0%       ~ (p=1.000 n=6) ¹
Tailer_Tail/128-12    16.00 ± 0%   16.00 ± 0%       ~ (p=1.000 n=6) ¹
Tailer_Tail/512-12    16.00 ± 0%   16.00 ± 0%       ~ (p=1.000 n=6) ¹
Tailer_Tail/1024-12   16.00 ± 0%   16.00 ± 0%       ~ (p=1.000 n=6) ¹
Tailer_Tail/4096-12   16.00 ± 0%   16.00 ± 0%       ~ (p=1.000 n=6) ¹
geomean               16.00        16.00       +0.00%
¹ all samples are equal
```
  • Loading branch information
shadowspore committed Oct 13, 2023
1 parent 9a1d987 commit 855279e
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func (t *Tailer) Tail(ctx context.Context, h Handler) error {
done.Store(true)
}()

debugEnabled := t.lg.Core().Enabled(zapcore.DebugLevel)
for {
if done.Load() {
return ctx.Err()
Expand All @@ -266,17 +267,18 @@ func (t *Tailer) Tail(ctx context.Context, h Handler) error {
// Grab the offset in case we need to back up in the event of a half-line.
offset := t.offset()
line.Offset = offset
if e := t.lg.Check(zapcore.DebugLevel, "Offset"); e != nil {
e.Write(zap.Int64("offset", offset))
var readErr error
if debugEnabled {
t.lg.Debug("Reading line", zap.Int64("offset", offset))
}

var readErr error
t.lg.Debug("Reading line")
line.Data, readErr = t.readLine(line.Data)

switch readErr {
case io.EOF:
t.lg.Debug("Got EOF")
if debugEnabled {
t.lg.Debug("Got EOF")
}
if line.final() {
// Reporting only final lines, i.e. those ending with newline.
// Line can become final later.
Expand All @@ -297,7 +299,9 @@ func (t *Tailer) Tail(ctx context.Context, h Handler) error {
}
return nil
}
t.lg.Debug("Waiting for changes")
if debugEnabled {
t.lg.Debug("Waiting for changes")
}
if err := t.waitForChanges(ctx, offset); err != nil {
if errors.Is(err, errStop) {
return nil
Expand Down

0 comments on commit 855279e

Please sign in to comment.