]> Cypherpunks repositories - gostls13.git/commitdiff
log: Prevent getting time if it's unnecessary
authorAlbert Nigmatzianov <albertnigma@gmail.com>
Mon, 22 May 2017 19:39:38 +0000 (21:39 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 22 May 2017 21:59:34 +0000 (21:59 +0000)
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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>

src/log/log.go
src/log/log_test.go

index 58b8788be4a5c4ce865ba12a50d7a4b24433de02..0ea4b896589091c7d5437bca6d5e553fb0133e74 100644 (file)
@@ -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()
index dd16c9d3e18eb0b5509f07a36b9a6de29eac668a..966fdf306bc1c9a2ca368ffc8de0c697e691044d 100644 (file)
@@ -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)
+       }
+}