From 6041f3e22ae2801b728a32657cbeb0fe97c4007f Mon Sep 17 00:00:00 2001 From: Yevhen Sylenko Date: Thu, 2 Apr 2020 23:14:34 +0200 Subject: [PATCH 1/2] Make AppendKey in json inlinable This change increase performance by inlining this frequently called function --- internal/json/base.go | 7 +++---- internal/json/types.go | 2 +- log.go | 6 +++++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/json/base.go b/internal/json/base.go index d6f8839e..62248e71 100644 --- a/internal/json/base.go +++ b/internal/json/base.go @@ -4,9 +4,8 @@ type Encoder struct{} // AppendKey appends a new key to the output JSON. func (e Encoder) AppendKey(dst []byte, key string) []byte { - if len(dst) > 1 && dst[len(dst)-1] != '{' { + if dst[len(dst)-1] != '{' { dst = append(dst, ',') } - dst = e.AppendString(dst, key) - return append(dst, ':') -} \ No newline at end of file + return append(e.AppendString(dst, key), ':') +} diff --git a/internal/json/types.go b/internal/json/types.go index bc8bc095..b2ac564f 100644 --- a/internal/json/types.go +++ b/internal/json/types.go @@ -379,7 +379,7 @@ func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { // to separate with existing content OR // 3. existing content has already other fields if o[0] == '{' { - if len(dst) == 0 { + if len(dst) <= 1 { o = o[1:] } else { o[0] = ',' diff --git a/log.go b/log.go index b1e7ac13..cbf68850 100644 --- a/log.go +++ b/log.go @@ -234,6 +234,10 @@ func (l Logger) With() Context { l.context = make([]byte, 0, 500) if context != nil { l.context = append(l.context, context...) + } else { + // This is needed for AppendKey to not check len of input + // thus making it inlinable + l.context = enc.AppendBeginMarker(l.context) } return Context{l} } @@ -415,7 +419,7 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event { if level != NoLevel { e.Str(LevelFieldName, LevelFieldMarshalFunc(level)) } - if l.context != nil && len(l.context) > 0 { + if l.context != nil && len(l.context) > 1 { e.buf = enc.AppendObjectData(e.buf, l.context) } return e From 4c7399d5ffc9a9917d5c87932b5ac805c597082a Mon Sep 17 00:00:00 2001 From: ffenix113 Date: Tue, 5 May 2020 22:02:30 +0200 Subject: [PATCH 2/2] Fix race condition --- internal/json/types.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/json/types.go b/internal/json/types.go index b2ac564f..d1862426 100644 --- a/internal/json/types.go +++ b/internal/json/types.go @@ -379,11 +379,10 @@ func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { // to separate with existing content OR // 3. existing content has already other fields if o[0] == '{' { - if len(dst) <= 1 { - o = o[1:] - } else { - o[0] = ',' + if len(dst) > 1 { + dst = append(dst, ',') } + o = o[1:] } else if len(dst) > 1 { dst = append(dst, ',') }