Skip to content

Commit

Permalink
Avoid allocating tintError twice (#82)
Browse files Browse the repository at this point in the history
* Avoid allocating tintError twice

Passing the type-asserted tintError value as an error causes a second allocation.

Since the helper method is only for tintError values, change the signature to specify the concrete type to elide the second allocation.

* added `tint.Err` benchmark

---------

Co-authored-by: lmittmann <lmittmann@users.noreply.github.com>
  • Loading branch information
database64128 and lmittmann authored Dec 16, 2024
1 parent de437d0 commit bd5634c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (h *handler) appendValue(buf *buffer, v slog.Value, quote bool) {
}
}

func (h *handler) appendTintError(buf *buffer, err error, attrKey, groupsPrefix string) {
func (h *handler) appendTintError(buf *buffer, err tintError, attrKey, groupsPrefix string) {
buf.WriteStringIf(!h.noColor, ansiBrightRedFaint)
appendString(buf, groupsPrefix+attrKey, true)
buf.WriteByte('=')
Expand Down
8 changes: 8 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,14 @@ func BenchmarkLogAttrs(b *testing.B) {
)
},
},
{
"error",
func(logger *slog.Logger) {
logger.LogAttrs(context.TODO(), slog.LevelError, testMessage,
tint.Err(errTest),
)
},
},
}

for _, h := range handler {
Expand Down

0 comments on commit bd5634c

Please sign in to comment.