[performance] minimise log field allocations (#3529)

* when appending log field only do so by minimal amount

* move slice utils to separate package to fix import cycle, add GrowJust() and AppendJust() functions

* fix GrowJust() not returning slice of same length

* improved xslices tests

* make AppendJust() test check for slice contents, fix AppendJust() final copying behaviour

* add a +1 with field growth to try minimise allocation for log 'msg' field
This commit is contained in:
kim 2024-11-11 15:45:19 +00:00 committed by GitHub
commit e3c2b790fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 189 additions and 76 deletions

View file

@ -22,11 +22,11 @@ import (
"fmt"
"log/syslog"
"os"
"slices"
"strings"
"time"
"codeberg.org/gruf/go-kv"
"github.com/superseriousbusiness/gotosocial/internal/util/xslices"
)
var (
@ -412,7 +412,10 @@ func logf(ctx context.Context, depth int, lvl LEVEL, fields []kv.Field, s string
buf.B = append(buf.B, lvlstrs[lvl]...)
buf.B = append(buf.B, ' ')
if ctx != nil {
if ctx != nil && len(ctxhooks) > 0 {
// Ensure fields have space for hooks (+1 for below).
fields = xslices.GrowJust(fields, len(ctxhooks)+1)
// Pass context through hooks.
for _, hook := range ctxhooks {
fields = hook(ctx, fields)
@ -420,9 +423,8 @@ func logf(ctx context.Context, depth int, lvl LEVEL, fields []kv.Field, s string
}
if s != "" {
// Append message to log fields.
fields = slices.Grow(fields, 1)
fields = append(fields, kv.Field{
// Append message (if given) as final log field.
fields = xslices.AppendJust(fields, kv.Field{
K: "msg", V: fmt.Sprintf(s, a...),
})
}