From deaded4ca8d0bdaeaf12a2ba9c6de15b34546b86 Mon Sep 17 00:00:00 2001 From: unclejack Date: Thu, 18 Sep 2014 18:04:18 +0300 Subject: [PATCH] pkg/jsonlog: avoid JSONLog allocation in loop Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) --- jsonlog/jsonlog.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/jsonlog/jsonlog.go b/jsonlog/jsonlog.go index 16d015f..b0c61a8 100644 --- a/jsonlog/jsonlog.go +++ b/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() } }