From dde8e9e29636aff6a1041cf426f4e825dd11015f Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Fri, 27 May 2022 10:55:51 +0200 Subject: [PATCH] internal/restic: Custom ID.MarshalJSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This skips an allocation. internal/archiver benchmarks, Linux/amd64: name old time/op new time/op delta ArchiverSaveFileSmall-8 3.94ms ± 6% 3.91ms ± 6% ~ (p=0.947 n=20+20) ArchiverSaveFileLarge-8 304ms ± 3% 301ms ± 4% ~ (p=0.265 n=18+18) name old speed new speed delta ArchiverSaveFileSmall-8 1.04MB/s ± 6% 1.05MB/s ± 6% ~ (p=0.803 n=20+20) ArchiverSaveFileLarge-8 142MB/s ± 3% 143MB/s ± 4% ~ (p=0.421 n=18+19) name old alloc/op new alloc/op delta ArchiverSaveFileSmall-8 17.9MB ± 0% 17.9MB ± 0% -0.01% (p=0.000 n=19+19) ArchiverSaveFileLarge-8 382MB ± 2% 382MB ± 1% ~ (p=0.687 n=20+19) name old allocs/op new allocs/op delta ArchiverSaveFileSmall-8 540 ± 1% 528 ± 0% -2.19% (p=0.000 n=19+19) ArchiverSaveFileLarge-8 1.93k ± 3% 1.79k ± 4% -7.06% (p=0.000 n=20+20) --- internal/restic/id.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/restic/id.go b/internal/restic/id.go index c098dbfb3..6d85ed68a 100644 --- a/internal/restic/id.go +++ b/internal/restic/id.go @@ -3,7 +3,6 @@ package restic import ( "crypto/rand" "encoding/hex" - "encoding/json" "fmt" "io" @@ -98,7 +97,13 @@ func (id ID) EqualString(other string) (bool, error) { // MarshalJSON returns the JSON encoding of id. func (id ID) MarshalJSON() ([]byte, error) { - return json.Marshal(id.String()) + buf := make([]byte, 2+hex.EncodedLen(len(id))) + + buf[0] = '"' + hex.Encode(buf[1:], id[:]) + buf[len(buf)-1] = '"' + + return buf, nil } // UnmarshalJSON parses the JSON-encoded data and stores the result in id.