From 5550a46946a2e6e1be8844d78a78122b333fae26 Mon Sep 17 00:00:00 2001 From: unclejack Date: Mon, 30 Mar 2015 22:09:56 +0300 Subject: [PATCH] pkg/broadcastwriter: use []byte to lower alloc Signed-off-by: Cristian Staretu --- pkg/broadcastwriter/broadcastwriter.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pkg/broadcastwriter/broadcastwriter.go b/pkg/broadcastwriter/broadcastwriter.go index 2ee68944d1..ce63f76133 100644 --- a/pkg/broadcastwriter/broadcastwriter.go +++ b/pkg/broadcastwriter/broadcastwriter.go @@ -8,6 +8,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/jsonlog" + "github.com/docker/docker/pkg/timeutils" ) // BroadcastWriter accumulate multiple io.WriteCloser by stream. @@ -33,6 +34,7 @@ func (w *BroadcastWriter) AddWriter(writer io.WriteCloser, stream string) { // Write writes bytes to all writers. Failed writers will be evicted during // this call. func (w *BroadcastWriter) Write(p []byte) (n int, err error) { + var timestamp string created := time.Now().UTC() w.Lock() if writers, ok := w.streams[""]; ok { @@ -49,16 +51,26 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) { } w.buf.Write(p) for { - line, err := w.buf.ReadString('\n') - if err != nil { - w.buf.WriteString(line) + if n := w.buf.Len(); n == 0 { break } + i := bytes.IndexByte(w.buf.Bytes(), '\n') + if i < 0 { + break + } + lineBytes := w.buf.Next(i + 1) + if timestamp == "" { + timestamp, err = timeutils.FastMarshalJSON(created) + if err != nil { + continue + } + } + for stream, writers := range w.streams { if stream == "" { continue } - jsonLog := jsonlog.JSONLog{Log: line, Stream: stream, Created: created} + jsonLog := jsonlog.JSONLogBytes{Log: lineBytes, Stream: stream, Created: timestamp} err = jsonLog.MarshalJSONBuf(w.jsLogBuf) if err != nil { logrus.Errorf("Error making JSON log line: %s", err)