2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 22:27:35 +00:00

internal/restic: Custom ID.MarshalJSON

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)
This commit is contained in:
greatroar 2022-05-27 10:55:51 +02:00
parent bc27c370e7
commit dde8e9e296

View File

@ -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.