[#376] acl: Replace slices of pointers with raw structs

```
name                                     old time/op    new time/op    delta
Table_ToGRPCMessage/to_grpc_message-8      2.82µs ± 5%    2.86µs ± 5%     ~     (p=0.395 n=10+8)
Table_ToGRPCMessage/from_grpc_message-8    2.47µs ± 5%    1.08µs ± 6%  -56.16%  (p=0.000 n=9+9)

name                                     old alloc/op   new alloc/op   delta
Table_ToGRPCMessage/to_grpc_message-8      3.31kB ± 0%    3.31kB ± 0%     ~     (all equal)
Table_ToGRPCMessage/from_grpc_message-8    1.82kB ± 0%    1.38kB ± 0%  -24.56%  (p=0.000 n=10+10)

name                                     old allocs/op  new allocs/op  delta
Table_ToGRPCMessage/to_grpc_message-8        46.0 ± 0%      46.0 ± 0%     ~     (all equal)
Table_ToGRPCMessage/from_grpc_message-8      45.0 ± 0%       9.0 ± 0%  -80.00%  (p=0.000 n=10+10)
```

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-01 16:32:54 +03:00 committed by Alex Vanin
parent 4a70c42104
commit ec484f2fd2
5 changed files with 44 additions and 63 deletions

View file

@ -12,18 +12,17 @@ func BenchmarkTable_ToGRPCMessage(b *testing.B) {
const size = 4 const size = 4
tb := new(acl.Table) tb := new(acl.Table)
rs := make([]*acl.Record, size) rs := make([]acl.Record, size)
for i := range rs { for i := range rs {
fs := make([]*acl.HeaderFilter, size) fs := make([]acl.HeaderFilter, size)
for j := range fs { for j := range fs {
fs[j] = acltest.GenerateFilter(false) fs[j] = *acltest.GenerateFilter(false)
} }
ts := make([]*acl.Target, size) ts := make([]acl.Target, size)
for j := range ts { for j := range ts {
ts[j] = acltest.GenerateTarget(false) ts[j] = *acltest.GenerateTarget(false)
} }
rs[i] = new(acl.Record)
rs[i].SetFilters(fs) rs[i].SetFilters(fs)
rs[i].SetTargets(ts) rs[i].SetTargets(ts)
} }

View file

@ -185,7 +185,7 @@ func (f *HeaderFilter) FromGRPCMessage(m grpc.Message) error {
return nil return nil
} }
func HeaderFiltersToGRPC(fs []*HeaderFilter) (res []*acl.EACLRecord_Filter) { func HeaderFiltersToGRPC(fs []HeaderFilter) (res []*acl.EACLRecord_Filter) {
if fs != nil { if fs != nil {
res = make([]*acl.EACLRecord_Filter, 0, len(fs)) res = make([]*acl.EACLRecord_Filter, 0, len(fs))
@ -197,23 +197,17 @@ func HeaderFiltersToGRPC(fs []*HeaderFilter) (res []*acl.EACLRecord_Filter) {
return return
} }
func HeaderFiltersFromGRPC(fs []*acl.EACLRecord_Filter) (res []*HeaderFilter, err error) { func HeaderFiltersFromGRPC(fs []*acl.EACLRecord_Filter) (res []HeaderFilter, err error) {
if fs != nil { if fs != nil {
res = make([]*HeaderFilter, 0, len(fs)) res = make([]HeaderFilter, len(fs))
for i := range fs { for i := range fs {
var x *HeaderFilter
if fs[i] != nil { if fs[i] != nil {
x = new(HeaderFilter) err = res[i].FromGRPCMessage(fs[i])
err = x.FromGRPCMessage(fs[i])
if err != nil { if err != nil {
return return
} }
} }
res = append(res, x)
} }
} }
@ -245,7 +239,7 @@ func (t *Target) FromGRPCMessage(m grpc.Message) error {
return nil return nil
} }
func TargetsToGRPC(ts []*Target) (res []*acl.EACLRecord_Target) { func TargetsToGRPC(ts []Target) (res []*acl.EACLRecord_Target) {
if ts != nil { if ts != nil {
res = make([]*acl.EACLRecord_Target, 0, len(ts)) res = make([]*acl.EACLRecord_Target, 0, len(ts))
@ -257,23 +251,17 @@ func TargetsToGRPC(ts []*Target) (res []*acl.EACLRecord_Target) {
return return
} }
func TargetsFromGRPC(fs []*acl.EACLRecord_Target) (res []*Target, err error) { func TargetsFromGRPC(fs []*acl.EACLRecord_Target) (res []Target, err error) {
if fs != nil { if fs != nil {
res = make([]*Target, 0, len(fs)) res = make([]Target, len(fs))
for i := range fs { for i := range fs {
var x *Target
if fs[i] != nil { if fs[i] != nil {
x = new(Target) err = res[i].FromGRPCMessage(fs[i])
err = x.FromGRPCMessage(fs[i])
if err != nil { if err != nil {
return return
} }
} }
res = append(res, x)
} }
} }
@ -319,7 +307,7 @@ func (r *Record) FromGRPCMessage(m grpc.Message) error {
return nil return nil
} }
func RecordsToGRPC(ts []*Record) (res []*acl.EACLRecord) { func RecordsToGRPC(ts []Record) (res []*acl.EACLRecord) {
if ts != nil { if ts != nil {
res = make([]*acl.EACLRecord, 0, len(ts)) res = make([]*acl.EACLRecord, 0, len(ts))
@ -331,23 +319,17 @@ func RecordsToGRPC(ts []*Record) (res []*acl.EACLRecord) {
return return
} }
func RecordsFromGRPC(fs []*acl.EACLRecord) (res []*Record, err error) { func RecordsFromGRPC(fs []*acl.EACLRecord) (res []Record, err error) {
if fs != nil { if fs != nil {
res = make([]*Record, 0, len(fs)) res = make([]Record, len(fs))
for i := range fs { for i := range fs {
var x *Record
if fs[i] != nil { if fs[i] != nil {
x = new(Record) err = res[i].FromGRPCMessage(fs[i])
err = x.FromGRPCMessage(fs[i])
if err != nil { if err != nil {
return return
} }
} }
res = append(res, x)
} }
} }

View file

@ -67,7 +67,7 @@ func (t *Table) StableMarshal(buf []byte) ([]byte, error) {
offset += n offset += n
for i := range t.records { for i := range t.records {
n, err = protoutil.NestedStructureMarshal(tableRecordsField, buf[offset:], t.records[i]) n, err = protoutil.NestedStructureMarshal(tableRecordsField, buf[offset:], &t.records[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -88,7 +88,7 @@ func (t *Table) StableSize() (size int) {
size += protoutil.NestedStructureSize(tableContainerIDField, t.cid) size += protoutil.NestedStructureSize(tableContainerIDField, t.cid)
for i := range t.records { for i := range t.records {
size += protoutil.NestedStructureSize(tableRecordsField, t.records[i]) size += protoutil.NestedStructureSize(tableRecordsField, &t.records[i])
} }
return size return size
@ -129,7 +129,7 @@ func (r *Record) StableMarshal(buf []byte) ([]byte, error) {
offset += n offset += n
for i := range r.filters { for i := range r.filters {
n, err = protoutil.NestedStructureMarshal(recordFiltersField, buf[offset:], r.filters[i]) n, err = protoutil.NestedStructureMarshal(recordFiltersField, buf[offset:], &r.filters[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -138,7 +138,7 @@ func (r *Record) StableMarshal(buf []byte) ([]byte, error) {
} }
for i := range r.targets { for i := range r.targets {
n, err = protoutil.NestedStructureMarshal(recordTargetsField, buf[offset:], r.targets[i]) n, err = protoutil.NestedStructureMarshal(recordTargetsField, buf[offset:], &r.targets[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -159,11 +159,11 @@ func (r *Record) StableSize() (size int) {
size += protoutil.EnumSize(recordActionField, int32(r.action)) size += protoutil.EnumSize(recordActionField, int32(r.action))
for i := range r.filters { for i := range r.filters {
size += protoutil.NestedStructureSize(recordFiltersField, r.filters[i]) size += protoutil.NestedStructureSize(recordFiltersField, &r.filters[i])
} }
for i := range r.targets { for i := range r.targets {
size += protoutil.NestedStructureSize(recordTargetsField, r.targets[i]) size += protoutil.NestedStructureSize(recordTargetsField, &r.targets[i])
} }
return size return size

View file

@ -42,13 +42,13 @@ func GenerateTable(empty bool) *acl.Table {
return m return m
} }
func GenerateRecords(empty bool) []*acl.Record { func GenerateRecords(empty bool) []acl.Record {
var rs []*acl.Record var rs []acl.Record
if !empty { if !empty {
rs = append(rs, rs = append(rs,
GenerateRecord(false), *GenerateRecord(false),
GenerateRecord(false), *GenerateRecord(false),
) )
} }
@ -68,13 +68,13 @@ func GenerateRecord(empty bool) *acl.Record {
return m return m
} }
func GenerateFilters(empty bool) []*acl.HeaderFilter { func GenerateFilters(empty bool) []acl.HeaderFilter {
var fs []*acl.HeaderFilter var fs []acl.HeaderFilter
if !empty { if !empty {
fs = append(fs, fs = append(fs,
GenerateFilter(false), *GenerateFilter(false),
GenerateFilter(false), *GenerateFilter(false),
) )
} }
@ -94,13 +94,13 @@ func GenerateFilter(empty bool) *acl.HeaderFilter {
return m return m
} }
func GenerateTargets(empty bool) []*acl.Target { func GenerateTargets(empty bool) []acl.Target {
var ts []*acl.Target var ts []acl.Target
if !empty { if !empty {
ts = append(ts, ts = append(ts,
GenerateTarget(false), *GenerateTarget(false),
GenerateTarget(false), *GenerateTarget(false),
) )
} }

View file

@ -27,9 +27,9 @@ type Record struct {
action Action action Action
filters []*HeaderFilter filters []HeaderFilter
targets []*Target targets []Target
} }
// Table is a unified structure of EACLTable // Table is a unified structure of EACLTable
@ -39,7 +39,7 @@ type Table struct {
cid *refs.ContainerID cid *refs.ContainerID
records []*Record records []Record
} }
type TokenLifetime struct { type TokenLifetime struct {
@ -224,7 +224,7 @@ func (r *Record) SetAction(v Action) {
} }
} }
func (r *Record) GetFilters() []*HeaderFilter { func (r *Record) GetFilters() []HeaderFilter {
if r != nil { if r != nil {
return r.filters return r.filters
} }
@ -232,13 +232,13 @@ func (r *Record) GetFilters() []*HeaderFilter {
return nil return nil
} }
func (r *Record) SetFilters(v []*HeaderFilter) { func (r *Record) SetFilters(v []HeaderFilter) {
if r != nil { if r != nil {
r.filters = v r.filters = v
} }
} }
func (r *Record) GetTargets() []*Target { func (r *Record) GetTargets() []Target {
if r != nil { if r != nil {
return r.targets return r.targets
} }
@ -246,7 +246,7 @@ func (r *Record) GetTargets() []*Target {
return nil return nil
} }
func (r *Record) SetTargets(v []*Target) { func (r *Record) SetTargets(v []Target) {
if r != nil { if r != nil {
r.targets = v r.targets = v
} }
@ -280,7 +280,7 @@ func (t *Table) SetContainerID(v *refs.ContainerID) {
} }
} }
func (t *Table) GetRecords() []*Record { func (t *Table) GetRecords() []Record {
if t != nil { if t != nil {
return t.records return t.records
} }
@ -288,7 +288,7 @@ func (t *Table) GetRecords() []*Record {
return nil return nil
} }
func (t *Table) SetRecords(v []*Record) { func (t *Table) SetRecords(v []Record) {
if t != nil { if t != nil {
t.records = v t.records = v
} }