From 473bb5274ce7a520bc73ccb3529f99729a799062 Mon Sep 17 00:00:00 2001 From: unclejack Date: Thu, 18 Sep 2014 18:04:18 +0300 Subject: [PATCH 1/2] pkg/jsonlog: avoid JSONLog allocation in loop Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) --- pkg/jsonlog/jsonlog.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/jsonlog/jsonlog.go b/pkg/jsonlog/jsonlog.go index 16d015fcd1..b0c61a803f 100644 --- a/pkg/jsonlog/jsonlog.go +++ b/pkg/jsonlog/jsonlog.go @@ -25,11 +25,16 @@ func (jl *JSONLog) Format(format string) (string, error) { return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil } +func (jl *JSONLog) Reset() { + jl.Log = "" + jl.Stream = "" + jl.Created = time.Time{} +} + func WriteLog(src io.Reader, dst io.Writer, format string) error { dec := json.NewDecoder(src) + l := &JSONLog{} for { - l := &JSONLog{} - if err := dec.Decode(l); err == io.EOF { return nil } else if err != nil { @@ -43,5 +48,6 @@ func WriteLog(src io.Reader, dst io.Writer, format string) error { if _, err := io.WriteString(dst, line); err != nil { return err } + l.Reset() } } From d2c104c3a01217f1a860d23143672af74a32e93a Mon Sep 17 00:00:00 2001 From: unclejack Date: Thu, 18 Sep 2014 16:29:19 +0300 Subject: [PATCH 2/2] daemon/logs: lower allocations in loop Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) --- daemon/logs.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/daemon/logs.go b/daemon/logs.go index 6925cabacb..b4df401efd 100644 --- a/daemon/logs.go +++ b/daemon/logs.go @@ -88,9 +88,8 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status { cLog = tmp } dec := json.NewDecoder(cLog) + l := &jsonlog.JSONLog{} for { - l := &jsonlog.JSONLog{} - if err := dec.Decode(l); err == io.EOF { break } else if err != nil { @@ -102,11 +101,12 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status { logLine = fmt.Sprintf("%s %s", l.Created.Format(format), logLine) } if l.Stream == "stdout" && stdout { - fmt.Fprintf(job.Stdout, "%s", logLine) + io.WriteString(job.Stdout, logLine) } if l.Stream == "stderr" && stderr { - fmt.Fprintf(job.Stderr, "%s", logLine) + io.WriteString(job.Stderr, logLine) } + l.Reset() } } }