Skip to content

Commit

Permalink
feat: optimize mem allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Dec 18, 2024
1 parent e6fb89c commit 67b1ddd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
7 changes: 5 additions & 2 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func AnyValueToString(v slog.Value) string {
}

func AttrsToString(attrs ...slog.Attr) map[string]string {
output := map[string]string{}
output := make(map[string]string, len(attrs))

for i := range attrs {
attr := attrs[i]
Expand Down Expand Up @@ -198,7 +198,10 @@ func ExtractError(attrs []slog.Attr, errorKeys ...string) ([]slog.Attr, error) {
}

if err, ok := attr.Value.Resolve().Any().(error); ok {
return append(attrs[:i], attrs[i+1:]...), err
output := make([]slog.Attr, 0, len(attrs)-1)
output = append(output, attrs[:i]...)
output = append(output, attrs[i+1:]...)
return output, err
}
}

Expand Down
27 changes: 27 additions & 0 deletions attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ func TestSource(t *testing.T) {
}
}

func TestAppendRecordAttrsToAttrs(t *testing.T) {
t.Parallel()
is := assert.New(t)

a := slog.String("a", "1")
b := slog.Int("b", 2)
c := slog.Bool("c", true)
d := slog.Float64("d", 3.14)
e := slog.Duration("e", time.Second)
r := slog.NewRecord(time.Now(), slog.LevelError, assert.AnError.Error(), 0)
r.AddAttrs(d, e)

output := AppendRecordAttrsToAttrs([]slog.Attr{a, b, c}, []string{}, &r)
is.Len(output, 5)
is.Equal([]slog.Attr{a, b, c, d, e}, output)

output = AppendRecordAttrsToAttrs([]slog.Attr{a, b, c}, []string{"foo", "bar"}, &r)
is.Len(output, 5)
is.Equal([]slog.Attr{a, b, c, slog.Group("foo", slog.Group("bar", d)), slog.Group("foo", slog.Group("bar", e))}, output)

r = slog.NewRecord(time.Now(), slog.LevelError, assert.AnError.Error(), 0)

output = AppendRecordAttrsToAttrs([]slog.Attr{a, b, c}, []string{}, &r)
is.Len(output, 3)
is.Equal([]slog.Attr{a, b, c}, output)
}

type testLogValuer struct {
name string
pass string
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func ContextExtractor(ctx context.Context, fns []func(ctx context.Context) []slo

func ExtractFromContext(keys ...any) func(ctx context.Context) []slog.Attr {
return func(ctx context.Context) []slog.Attr {
attrs := []slog.Attr{}
attrs := make([]slog.Attr, 0, len(keys))
for _, key := range keys {
attrs = append(attrs, slog.Any(key.(string), ctx.Value(key)))
}
Expand Down
9 changes: 6 additions & 3 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
)

func AppendAttrsToGroup(groups []string, actualAttrs []slog.Attr, newAttrs ...slog.Attr) []slog.Attr {
actualAttrs = slices.Clone(actualAttrs)

if len(groups) == 0 {
return UniqAttrs(append(actualAttrs, newAttrs...))
actualAttrsCopy := make([]slog.Attr, 0, len(actualAttrs)+len(newAttrs))
actualAttrsCopy = append(actualAttrsCopy, actualAttrs...)
actualAttrsCopy = append(actualAttrsCopy, newAttrs...)
return UniqAttrs(actualAttrsCopy)
}

actualAttrs = slices.Clone(actualAttrs)

for i := range actualAttrs {
attr := actualAttrs[i]
if attr.Key == groups[0] && attr.Value.Kind() == slog.KindGroup {
Expand Down

0 comments on commit 67b1ddd

Please sign in to comment.