From 4cf19fb5a73a33f838efe21b3baa14167469ac83 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Mon, 22 May 2017 21:39:38 +0200 Subject: [PATCH] log: Prevent getting time if it's unnecessary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Small performance gain: name old time/op new time/op delta Itoa-4 95.4ns ± 4% 95.6ns ± 3% ~ (p=0.256 n=45+46) Println-4 480ns ± 4% 476ns ± 5% -0.87% (p=0.003 n=45+45) PrintlnNoFlags-4 316ns ± 3% 299ns ± 4% -5.38% (p=0.000 n=42+44) name old alloc/op new alloc/op delta Itoa-4 0.00B 0.00B ~ (all equal) Println-4 21.0B ± 0% 21.0B ± 0% ~ (all equal) PrintlnNoFlags-4 21.0B ± 0% 21.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta Itoa-4 0.00 0.00 ~ (all equal) Println-4 2.00 ± 0% 2.00 ± 0% ~ (all equal) PrintlnNoFlags-4 2.00 ± 0% 2.00 ± 0% ~ (all equal) Change-Id: Idcd03609a5a437a69ffa7004a673bf0b8d22e7ad Reviewed-on: https://go-review.googlesource.com/38056 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick --- src/log/log.go | 18 +++++++++++++----- src/log/log_test.go | 10 ++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/log/log.go b/src/log/log.go index 58b8788be4a..0ea4b896589 100644 --- a/src/log/log.go +++ b/src/log/log.go @@ -72,7 +72,7 @@ func (l *Logger) SetOutput(w io.Writer) { var std = New(os.Stderr, "", LstdFlags) -// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. +// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. func itoa(buf *[]byte, i int, wid int) { // Assemble decimal in reverse order. var b [20]byte @@ -89,12 +89,16 @@ func itoa(buf *[]byte, i int, wid int) { *buf = append(*buf, b[bp:]...) } +// formatHeader writes log header to buf in following order: +// * l.prefix (if it's not blank), +// * date and/or time (if corresponding flags are provided), +// * file and line number (if corresponding flags are provided). func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { *buf = append(*buf, l.prefix...) - if l.flag&LUTC != 0 { - t = t.UTC() - } if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { + if l.flag&LUTC != 0 { + t = t.UTC() + } if l.flag&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) @@ -143,7 +147,11 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { // provided for generality, although at the moment on all pre-defined // paths it will be 2. func (l *Logger) Output(calldepth int, s string) error { - now := time.Now() // get this early. + // Get time early if we need it. + var now time.Time + if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { + now = time.Now() + } var file string var line int l.mu.Lock() diff --git a/src/log/log_test.go b/src/log/log_test.go index dd16c9d3e18..966fdf306bc 100644 --- a/src/log/log_test.go +++ b/src/log/log_test.go @@ -182,3 +182,13 @@ func BenchmarkPrintln(b *testing.B) { l.Println(testString) } } + +func BenchmarkPrintlnNoFlags(b *testing.B) { + const testString = "test" + var buf bytes.Buffer + l := New(&buf, "", 0) + for i := 0; i < b.N; i++ { + buf.Reset() + l.Println(testString) + } +}