diff --git a/object/attributes.go b/object/attributes.go index 2e701bc7..cc828738 100644 --- a/object/attributes.go +++ b/object/attributes.go @@ -108,26 +108,20 @@ func WriteNotificationInfo(o *Object, ni NotificationInfo) { attrs = attrs[:len(attrs)-1] } - notifyAttrs := make([]*Attribute, 0, 2) - if !changedEpoch { - eAttr := new(Attribute) - eAttr.SetKey(SysAttributeTickEpoch) - eAttr.SetValue(epoch) - - notifyAttrs = append(notifyAttrs, eAttr) + index := len(attrs) + attrs = append(attrs, Attribute{}) + attrs[index].SetKey(SysAttributeTickEpoch) + attrs[index].SetValue(epoch) } if !changedTopic && topic != "" { - tAttr := new(Attribute) - tAttr.SetKey(SysAttributeTickTopic) - tAttr.SetValue(topic) - - notifyAttrs = append(notifyAttrs, tAttr) + index := len(attrs) + attrs = append(attrs, Attribute{}) + attrs[index].SetKey(SysAttributeTickTopic) + attrs[index].SetValue(topic) } - attrs = append(attrs, notifyAttrs...) - h.SetAttributes(attrs) } diff --git a/object/attributes_test.go b/object/attributes_test.go index 8556f130..5bf411a5 100644 --- a/object/attributes_test.go +++ b/object/attributes_test.go @@ -42,7 +42,7 @@ func TestSetNotification(t *testing.T) { func TestGetNotification(t *testing.T) { o := new(Object) - attr := []*Attribute{ + attr := []Attribute{ {SysAttributeTickEpoch, "10"}, {SysAttributeTickTopic, "test"}, } diff --git a/object/bench_test.go b/object/bench_test.go index 417c0dfb..90fab730 100644 --- a/object/bench_test.go +++ b/object/bench_test.go @@ -3,6 +3,8 @@ package object import ( "math/rand" "testing" + + "github.com/stretchr/testify/require" ) func randString(n int) string { @@ -14,13 +16,13 @@ func randString(n int) string { } func BenchmarkAttributesMarshal(b *testing.B) { - attrs := make([]*Attribute, 50) + attrs := make([]Attribute, 50) for i := range attrs { - attrs[i] = new(Attribute) attrs[i].key = SysAttributePrefix + randString(10) attrs[i].val = randString(10) } raw := AttributesToGRPC(attrs) + require.Equal(b, len(raw), len(attrs)) b.Run("marshal", func(b *testing.B) { b.ReportAllocs() diff --git a/object/convert.go b/object/convert.go index 80213df6..d574e79b 100644 --- a/object/convert.go +++ b/object/convert.go @@ -142,7 +142,7 @@ func (a *Attribute) FromGRPCMessage(m grpc.Message) error { return nil } -func AttributesToGRPC(xs []*Attribute) (res []*object.Header_Attribute) { +func AttributesToGRPC(xs []Attribute) (res []*object.Header_Attribute) { if xs != nil { res = make([]*object.Header_Attribute, 0, len(xs)) @@ -154,23 +154,17 @@ func AttributesToGRPC(xs []*Attribute) (res []*object.Header_Attribute) { return } -func AttributesFromGRPC(xs []*object.Header_Attribute) (res []*Attribute, err error) { +func AttributesFromGRPC(xs []*object.Header_Attribute) (res []Attribute, err error) { if xs != nil { - res = make([]*Attribute, 0, len(xs)) + res = make([]Attribute, len(xs)) for i := range xs { - var x *Attribute - if xs[i] != nil { - x = new(Attribute) - - err = x.FromGRPCMessage(xs[i]) + err = res[i].FromGRPCMessage(xs[i]) if err != nil { return } } - - res = append(res, x) } } diff --git a/object/marshal.go b/object/marshal.go index 44783f02..24d8d86b 100644 --- a/object/marshal.go +++ b/object/marshal.go @@ -394,7 +394,7 @@ func (h *Header) StableMarshal(buf []byte) ([]byte, error) { offset += n for i := range h.attr { - n, err = proto.NestedStructureMarshal(hdrAttributesField, buf[offset:], h.attr[i]) + n, err = proto.NestedStructureMarshal(hdrAttributesField, buf[offset:], &h.attr[i]) if err != nil { return nil, err } @@ -425,7 +425,7 @@ func (h *Header) StableSize() (size int) { size += proto.NestedStructureSize(hdrHomomorphicHashField, h.homoHash) size += proto.NestedStructureSize(hdrSessionTokenField, h.sessionToken) for i := range h.attr { - size += proto.NestedStructureSize(hdrAttributesField, h.attr[i]) + size += proto.NestedStructureSize(hdrAttributesField, &h.attr[i]) } size += proto.NestedStructureSize(hdrSplitField, h.split) diff --git a/object/test/generate.go b/object/test/generate.go index d2f80cb3..298bc9a9 100644 --- a/object/test/generate.go +++ b/object/test/generate.go @@ -35,13 +35,13 @@ func GenerateAttribute(empty bool) *object.Attribute { return m } -func GenerateAttributes(empty bool) []*object.Attribute { - var res []*object.Attribute +func GenerateAttributes(empty bool) []object.Attribute { + var res []object.Attribute if !empty { res = append(res, - GenerateAttribute(false), - GenerateAttribute(false), + *GenerateAttribute(false), + *GenerateAttribute(false), ) } diff --git a/object/types.go b/object/types.go index ed3cdffe..596ed3ec 100644 --- a/object/types.go +++ b/object/types.go @@ -56,7 +56,7 @@ type Header struct { sessionToken *session.Token - attr []*Attribute + attr []Attribute split *SplitHeader } @@ -654,7 +654,7 @@ func (h *Header) SetSessionToken(v *session.Token) { } } -func (h *Header) GetAttributes() []*Attribute { +func (h *Header) GetAttributes() []Attribute { if h != nil { return h.attr } @@ -662,7 +662,7 @@ func (h *Header) GetAttributes() []*Attribute { return nil } -func (h *Header) SetAttributes(v []*Attribute) { +func (h *Header) SetAttributes(v []Attribute) { if h != nil { h.attr = v }