frostfs-api-go/tombstone/marshal.go
Evgenii Stratonikov 84daaf59ef [#376] refs: Replace []*ObjectID with []ObjectID
```
ObjectIDSlice/0_elements/to_grpc_message-8       5.64ns ± 2%    5.89ns ± 4%   +4.56%  (p=0.000 n=10+10)
ObjectIDSlice/0_elements/from_grpc_message-8     6.68ns ± 3%    6.81ns ± 8%     ~     (p=0.143 n=10+10)
ObjectIDSlice/0_elements/marshal-8               7.41ns ± 3%    7.91ns ± 4%   +6.63%  (p=0.000 n=10+10)
ObjectIDSlice/1_elements/to_grpc_message-8       69.8ns ± 3%    80.5ns ± 7%  +15.39%  (p=0.000 n=10+10)
ObjectIDSlice/1_elements/from_grpc_message-8     56.4ns ± 6%    34.7ns ± 5%  -38.55%  (p=0.000 n=10+9)
ObjectIDSlice/1_elements/marshal-8               68.4ns ± 4%    67.6ns ± 4%     ~     (p=0.404 n=10+10)
ObjectIDSlice/50_elements/to_grpc_message-8      2.52µs ± 7%    2.56µs ± 4%     ~     (p=0.315 n=10+9)
ObjectIDSlice/50_elements/from_grpc_message-8    1.83µs ± 8%    0.44µs ± 1%  -75.73%  (p=0.000 n=10+8)
ObjectIDSlice/50_elements/marshal-8              2.32µs ±17%    2.22µs ± 3%     ~     (p=0.247 n=10+10)

name                                           old alloc/op   new alloc/op   delta
ObjectIDSlice/0_elements/to_grpc_message-8        0.00B          0.00B          ~     (all equal)
ObjectIDSlice/0_elements/from_grpc_message-8      0.00B          0.00B          ~     (all equal)
ObjectIDSlice/0_elements/marshal-8                0.00B          0.00B          ~     (all equal)
ObjectIDSlice/1_elements/to_grpc_message-8        72.0B ± 0%     72.0B ± 0%     ~     (all equal)
ObjectIDSlice/1_elements/from_grpc_message-8      32.0B ± 0%     24.0B ± 0%  -25.00%  (p=0.000 n=10+10)
ObjectIDSlice/1_elements/marshal-8                48.0B ± 0%     48.0B ± 0%     ~     (all equal)
ObjectIDSlice/50_elements/to_grpc_message-8      3.62kB ± 0%    3.62kB ± 0%     ~     (all equal)
ObjectIDSlice/50_elements/from_grpc_message-8    1.62kB ± 0%    1.28kB ± 0%  -20.79%  (p=0.000 n=10+10)
ObjectIDSlice/50_elements/marshal-8              2.05kB ± 0%    2.05kB ± 0%     ~     (all equal)

name                                           old allocs/op  new allocs/op  delta
ObjectIDSlice/0_elements/to_grpc_message-8         0.00           0.00          ~     (all equal)
ObjectIDSlice/0_elements/from_grpc_message-8       0.00           0.00          ~     (all equal)
ObjectIDSlice/0_elements/marshal-8                 0.00           0.00          ~     (all equal)
ObjectIDSlice/1_elements/to_grpc_message-8         2.00 ± 0%      2.00 ± 0%     ~     (all equal)
ObjectIDSlice/1_elements/from_grpc_message-8       2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
ObjectIDSlice/1_elements/marshal-8                 1.00 ± 0%      1.00 ± 0%     ~     (all equal)
ObjectIDSlice/50_elements/to_grpc_message-8        51.0 ± 0%      51.0 ± 0%     ~     (all equal)
ObjectIDSlice/50_elements/from_grpc_message-8      51.0 ± 0%       1.0 ± 0%  -98.04%  (p=0.000 n=10+10)
ObjectIDSlice/50_elements/marshal-8                1.00 ± 0%      1.00 ± 0%     ~     (all equal)
```

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-03-15 12:19:49 +03:00

75 lines
1.5 KiB
Go

package tombstone
import (
"github.com/nspcc-dev/neofs-api-go/v2/rpc/message"
tombstone "github.com/nspcc-dev/neofs-api-go/v2/tombstone/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/util/proto"
)
const (
expFNum = 1
splitIDFNum = 2
membersFNum = 3
)
// StableMarshal marshals unified tombstone message in a protobuf
// compatible way without field order shuffle.
func (s *Tombstone) StableMarshal(buf []byte) ([]byte, error) {
if s == nil {
return []byte{}, nil
}
if buf == nil {
buf = make([]byte, s.StableSize())
}
var (
offset, n int
err error
)
n, err = proto.UInt64Marshal(expFNum, buf[offset:], s.exp)
if err != nil {
return nil, err
}
offset += n
n, err = proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID)
if err != nil {
return nil, err
}
offset += n
for i := range s.members {
n, err = proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i])
if err != nil {
return nil, err
}
offset += n
}
return buf, nil
}
// StableSize returns size of tombstone message marshalled by StableMarshal function.
func (s *Tombstone) StableSize() (size int) {
if s == nil {
return 0
}
size += proto.UInt64Size(expFNum, s.exp)
size += proto.BytesSize(splitIDFNum, s.splitID)
for i := range s.members {
size += proto.NestedStructureSize(membersFNum, &s.members[i])
}
return size
}
// Unmarshal unmarshal tombstone message from its binary representation.
func (s *Tombstone) Unmarshal(data []byte) error {
return message.Unmarshal(s, data, new(tombstone.Tombstone))
}