From 2f0eee96fcd9d092278fe3e2369115f24248dfad Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 1 Mar 2022 16:41:42 +0300 Subject: [PATCH] [#376] netmap: Replace slices of pointers with structs Signed-off-by: Evgenii Stratonikov --- netmap/convert.go | 54 ++++++++++++----------------------------- netmap/marshal.go | 20 +++++++-------- netmap/test/generate.go | 34 +++++++++++++------------- netmap/types.go | 30 +++++++++++------------ 4 files changed, 57 insertions(+), 81 deletions(-) diff --git a/netmap/convert.go b/netmap/convert.go index 8325c949..2f1f68bb 100644 --- a/netmap/convert.go +++ b/netmap/convert.go @@ -45,7 +45,7 @@ func (f *Filter) FromGRPCMessage(m grpc.Message) error { return nil } -func FiltersToGRPC(fs []*Filter) (res []*netmap.Filter) { +func FiltersToGRPC(fs []Filter) (res []*netmap.Filter) { if fs != nil { res = make([]*netmap.Filter, 0, len(fs)) @@ -57,23 +57,17 @@ func FiltersToGRPC(fs []*Filter) (res []*netmap.Filter) { return } -func FiltersFromGRPC(fs []*netmap.Filter) (res []*Filter, err error) { +func FiltersFromGRPC(fs []*netmap.Filter) (res []Filter, err error) { if fs != nil { - res = make([]*Filter, 0, len(fs)) + res = make([]Filter, len(fs)) for i := range fs { - var f *Filter - if fs[i] != nil { - f = new(Filter) - - err = f.FromGRPCMessage(fs[i]) + err = res[i].FromGRPCMessage(fs[i]) if err != nil { return } } - - res = append(res, f) } } @@ -111,7 +105,7 @@ func (s *Selector) FromGRPCMessage(m grpc.Message) error { return nil } -func SelectorsToGRPC(ss []*Selector) (res []*netmap.Selector) { +func SelectorsToGRPC(ss []Selector) (res []*netmap.Selector) { if ss != nil { res = make([]*netmap.Selector, 0, len(ss)) @@ -123,23 +117,17 @@ func SelectorsToGRPC(ss []*Selector) (res []*netmap.Selector) { return } -func SelectorsFromGRPC(ss []*netmap.Selector) (res []*Selector, err error) { +func SelectorsFromGRPC(ss []*netmap.Selector) (res []Selector, err error) { if ss != nil { - res = make([]*Selector, 0, len(ss)) + res = make([]Selector, len(ss)) for i := range ss { - var s *Selector - if ss[i] != nil { - s = new(Selector) - - err = s.FromGRPCMessage(ss[i]) + err = res[i].FromGRPCMessage(ss[i]) if err != nil { return } } - - res = append(res, s) } } @@ -171,7 +159,7 @@ func (r *Replica) FromGRPCMessage(m grpc.Message) error { return nil } -func ReplicasToGRPC(rs []*Replica) (res []*netmap.Replica) { +func ReplicasToGRPC(rs []Replica) (res []*netmap.Replica) { if rs != nil { res = make([]*netmap.Replica, 0, len(rs)) @@ -183,23 +171,17 @@ func ReplicasToGRPC(rs []*Replica) (res []*netmap.Replica) { return } -func ReplicasFromGRPC(rs []*netmap.Replica) (res []*Replica, err error) { +func ReplicasFromGRPC(rs []*netmap.Replica) (res []Replica, err error) { if rs != nil { - res = make([]*Replica, 0, len(rs)) + res = make([]Replica, len(rs)) for i := range rs { - var r *Replica - if rs[i] != nil { - r = new(Replica) - - err = r.FromGRPCMessage(rs[i]) + err = res[i].FromGRPCMessage(rs[i]) if err != nil { return } } - - res = append(res, r) } } @@ -583,27 +565,21 @@ func (x *NetworkConfig) FromGRPCMessage(m grpc.Message) error { } var ( - ps []*NetworkParameter + ps []NetworkParameter psV2 = v.GetParameters() ) if psV2 != nil { ln := len(psV2) - ps = make([]*NetworkParameter, 0, ln) + ps = make([]NetworkParameter, ln) for i := 0; i < ln; i++ { - var p *NetworkParameter - if psV2[i] != nil { - p = new(NetworkParameter) - - if err := p.FromGRPCMessage(psV2[i]); err != nil { + if err := ps[i].FromGRPCMessage(psV2[i]); err != nil { return err } } - - ps = append(ps, p) } } diff --git a/netmap/marshal.go b/netmap/marshal.go index b29e7507..165af238 100644 --- a/netmap/marshal.go +++ b/netmap/marshal.go @@ -84,7 +84,7 @@ func (f *Filter) StableMarshal(buf []byte) ([]byte, error) { offset += n for i := range f.filters { - n, err = protoutil.NestedStructureMarshal(filtersFilterField, buf[offset:], f.filters[i]) + n, err = protoutil.NestedStructureMarshal(filtersFilterField, buf[offset:], &f.filters[i]) if err != nil { return nil, err } @@ -101,7 +101,7 @@ func (f *Filter) StableSize() (size int) { size += protoutil.EnumSize(opFilterField, int32(f.op)) size += protoutil.StringSize(valueFilterField, f.value) for i := range f.filters { - size += protoutil.NestedStructureSize(filtersFilterField, f.filters[i]) + size += protoutil.NestedStructureSize(filtersFilterField, &f.filters[i]) } return size @@ -230,7 +230,7 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) ([]byte, error) { ) for i := range p.replicas { - n, err = protoutil.NestedStructureMarshal(replicasPolicyField, buf[offset:], p.replicas[i]) + n, err = protoutil.NestedStructureMarshal(replicasPolicyField, buf[offset:], &p.replicas[i]) if err != nil { return nil, err } @@ -246,7 +246,7 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) ([]byte, error) { offset += n for i := range p.selectors { - n, err = protoutil.NestedStructureMarshal(selectorsPolicyField, buf[offset:], p.selectors[i]) + n, err = protoutil.NestedStructureMarshal(selectorsPolicyField, buf[offset:], &p.selectors[i]) if err != nil { return nil, err } @@ -255,7 +255,7 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) ([]byte, error) { } for i := range p.filters { - n, err = protoutil.NestedStructureMarshal(filtersPolicyField, buf[offset:], p.filters[i]) + n, err = protoutil.NestedStructureMarshal(filtersPolicyField, buf[offset:], &p.filters[i]) if err != nil { return nil, err } @@ -273,17 +273,17 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) ([]byte, error) { func (p *PlacementPolicy) StableSize() (size int) { for i := range p.replicas { - size += protoutil.NestedStructureSize(replicasPolicyField, p.replicas[i]) + size += protoutil.NestedStructureSize(replicasPolicyField, &p.replicas[i]) } size += protoutil.UInt32Size(backupPolicyField, p.backupFactor) for i := range p.selectors { - size += protoutil.NestedStructureSize(selectorsPolicyField, p.selectors[i]) + size += protoutil.NestedStructureSize(selectorsPolicyField, &p.selectors[i]) } for i := range p.filters { - size += protoutil.NestedStructureSize(filtersPolicyField, p.filters[i]) + size += protoutil.NestedStructureSize(filtersPolicyField, &p.filters[i]) } size += protoutil.NestedStructureSize(subnetIDPolicyField, p.subnetID) @@ -542,7 +542,7 @@ func (x *NetworkConfig) StableMarshal(buf []byte) ([]byte, error) { ) for i := range x.ps { - n, err = protoutil.NestedStructureMarshal(netCfgPrmsFNum, buf[offset:], x.ps[i]) + n, err = protoutil.NestedStructureMarshal(netCfgPrmsFNum, buf[offset:], &x.ps[i]) if err != nil { return nil, err } @@ -559,7 +559,7 @@ func (x *NetworkConfig) StableSize() (size int) { } for i := range x.ps { - size += protoutil.NestedStructureSize(netCfgPrmsFNum, x.ps[i]) + size += protoutil.NestedStructureSize(netCfgPrmsFNum, &x.ps[i]) } return size diff --git a/netmap/test/generate.go b/netmap/test/generate.go index 93e2ff38..0042dbb8 100644 --- a/netmap/test/generate.go +++ b/netmap/test/generate.go @@ -20,9 +20,9 @@ func generateFilter(empty, withSub bool) *netmap.Filter { m.SetOp(1) if withSub { - m.SetFilters([]*netmap.Filter{ - generateFilter(empty, false), - generateFilter(empty, false), + m.SetFilters([]netmap.Filter{ + *generateFilter(empty, false), + *generateFilter(empty, false), }) } } @@ -30,13 +30,13 @@ func generateFilter(empty, withSub bool) *netmap.Filter { return m } -func GenerateFilters(empty bool) []*netmap.Filter { - var res []*netmap.Filter +func GenerateFilters(empty bool) []netmap.Filter { + var res []netmap.Filter if !empty { res = append(res, - GenerateFilter(false), - GenerateFilter(false), + *GenerateFilter(false), + *GenerateFilter(false), ) } @@ -57,13 +57,13 @@ func GenerateSelector(empty bool) *netmap.Selector { return m } -func GenerateSelectors(empty bool) []*netmap.Selector { - var res []*netmap.Selector +func GenerateSelectors(empty bool) []netmap.Selector { + var res []netmap.Selector if !empty { res = append(res, - GenerateSelector(false), - GenerateSelector(false), + *GenerateSelector(false), + *GenerateSelector(false), ) } @@ -81,13 +81,13 @@ func GenerateReplica(empty bool) *netmap.Replica { return m } -func GenerateReplicas(empty bool) []*netmap.Replica { - var res []*netmap.Replica +func GenerateReplicas(empty bool) []netmap.Replica { + var res []netmap.Replica if !empty { res = append(res, - GenerateReplica(false), - GenerateReplica(false), + *GenerateReplica(false), + *GenerateReplica(false), ) } @@ -205,8 +205,8 @@ func GenerateNetworkConfig(empty bool) *netmap.NetworkConfig { if !empty { m.SetParameters( - GenerateNetworkParameter(empty), - GenerateNetworkParameter(empty), + *GenerateNetworkParameter(empty), + *GenerateNetworkParameter(empty), ) } diff --git a/netmap/types.go b/netmap/types.go index dc328053..5282765e 100644 --- a/netmap/types.go +++ b/netmap/types.go @@ -36,7 +36,7 @@ type Filter struct { key string op Operation value string - filters []*Filter + filters []Filter } type Selector struct { @@ -55,10 +55,10 @@ type Replica struct { type Operation uint32 type PlacementPolicy struct { - replicas []*Replica + replicas []Replica backupFactor uint32 - selectors []*Selector - filters []*Filter + selectors []Selector + filters []Filter subnetID *refs.SubnetID } @@ -114,7 +114,7 @@ const ( Distinct ) -func (f *Filter) GetFilters() []*Filter { +func (f *Filter) GetFilters() []Filter { if f != nil { return f.filters } @@ -122,7 +122,7 @@ func (f *Filter) GetFilters() []*Filter { return nil } -func (f *Filter) SetFilters(filters []*Filter) { +func (f *Filter) SetFilters(filters []Filter) { if f != nil { f.filters = filters } @@ -281,7 +281,7 @@ func (r *Replica) SetCount(count uint32) { } } -func (p *PlacementPolicy) GetFilters() []*Filter { +func (p *PlacementPolicy) GetFilters() []Filter { if p != nil { return p.filters } @@ -289,13 +289,13 @@ func (p *PlacementPolicy) GetFilters() []*Filter { return nil } -func (p *PlacementPolicy) SetFilters(filters []*Filter) { +func (p *PlacementPolicy) SetFilters(filters []Filter) { if p != nil { p.filters = filters } } -func (p *PlacementPolicy) GetSelectors() []*Selector { +func (p *PlacementPolicy) GetSelectors() []Selector { if p != nil { return p.selectors } @@ -303,7 +303,7 @@ func (p *PlacementPolicy) GetSelectors() []*Selector { return nil } -func (p *PlacementPolicy) SetSelectors(selectors []*Selector) { +func (p *PlacementPolicy) SetSelectors(selectors []Selector) { if p != nil { p.selectors = selectors } @@ -323,11 +323,11 @@ func (p *PlacementPolicy) SetContainerBackupFactor(backupFactor uint32) { } } -func (p *PlacementPolicy) GetReplicas() []*Replica { +func (p *PlacementPolicy) GetReplicas() []Replica { return p.replicas } -func (p *PlacementPolicy) SetReplicas(replicas []*Replica) { +func (p *PlacementPolicy) SetReplicas(replicas []Replica) { p.replicas = replicas } @@ -565,7 +565,7 @@ func (x *NetworkParameter) SetValue(v []byte) { // NetworkConfig represents NeoFS network configuration. type NetworkConfig struct { - ps []*NetworkParameter + ps []NetworkParameter } // NumberOfParameters returns number of network parameters. @@ -584,7 +584,7 @@ func (x *NetworkConfig) NumberOfParameters() int { func (x *NetworkConfig) IterateParameters(f func(*NetworkParameter) bool) { if x != nil { for i := range x.ps { - if f(x.ps[i]) { + if f(&x.ps[i]) { break } } @@ -592,7 +592,7 @@ func (x *NetworkConfig) IterateParameters(f func(*NetworkParameter) bool) { } // SetParameters sets list of network parameters. -func (x *NetworkConfig) SetParameters(v ...*NetworkParameter) { +func (x *NetworkConfig) SetParameters(v ...NetworkParameter) { if x != nil { x.ps = v }