]> Cypherpunks.ru repositories - gostls13.git/commit
log/slog: fix Record.back slice always too small during Add()
authorChris Duncan <veqryn@hotmail.com>
Wed, 13 Sep 2023 22:26:19 +0000 (22:26 +0000)
committerJonathan Amsterdam <jba@google.com>
Thu, 14 Sep 2023 17:38:25 +0000 (17:38 +0000)
commit65c53a1833a26467357b4aa6223e4dde5d6d7ed0
tree82bb77f722f9999913077a16b0ab1c9660e3ceb8
parented26e2fe3485346d682668191a726f096037b452
log/slog: fix Record.back slice always too small during Add()

When slog.Record.Add(args) is called, with enough args to cause the
Record.back []Attr to be created, it is being created 1 too small, which
results in it immediately being grown again by append before the function
exits (needless allocation and copying).
This is because it is created with a capacity equal to countAttrs(args),
but forgets that there is an additional attribute to be appended: a
(args is just the remaining unconsumed attributes).
This PR fixes that by adding 1 to the capacity to account for the `a` attribute.

Additionally, when Record.back already exists, it will most likely be at
max capacity already. Rather than append to it and risk having it grown
multiple times, or grow too large, this adds a slices.Grow call to set it
to the right capacity, similar to what is already done in the
Record.AddAttrs(attrs) function.

Change-Id: Ic4bcf45909fe4436c586ccd2b8d61f24606b6be8
GitHub-Last-Rev: 4c924b610a7987a940360bb1b4cc7c53981afdc5
GitHub-Pull-Request: golang/go#62388
Reviewed-on: https://go-review.googlesource.com/c/go/+/524618
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
src/log/slog/logger_test.go
src/log/slog/record.go