From 855279ebb009b0202533bd0f3149112d87033045 Mon Sep 17 00:00:00 2001 From: rs Date: Fri, 13 Oct 2023 19:17:58 +0500 Subject: [PATCH] refactor: reduce logger calls in hot path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` │ 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 ``` --- tail.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tail.go b/tail.go index 3984e65..c614819 100644 --- a/tail.go +++ b/tail.go @@ -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() @@ -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. @@ -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