diff --git a/accounting/marshal.go b/accounting/marshal.go index 6961b3e..028c7d7 100644 --- a/accounting/marshal.go +++ b/accounting/marshal.go @@ -21,13 +21,11 @@ func (d *Decimal) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, d.StableSize()) + buf = make([]byte, 0, d.StableSize()) } - var offset int - - offset += protoutil.Int64Marshal(decimalValueField, buf[offset:], d.val) - protoutil.UInt32Marshal(decimalPrecisionField, buf[offset:], d.prec) + buf = protoutil.Int64Marshal(decimalValueField, buf, d.val) + buf = protoutil.UInt32Marshal(decimalPrecisionField, buf, d.prec) return buf } @@ -53,10 +51,10 @@ func (b *BalanceRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, b.StableSize()) + buf = make([]byte, 0, b.StableSize()) } - protoutil.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID) + buf = protoutil.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID) return buf } @@ -81,10 +79,10 @@ func (br *BalanceResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, br.StableSize()) + buf = make([]byte, 0, br.StableSize()) } - protoutil.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal) + buf = protoutil.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal) return buf } diff --git a/acl/marshal.go b/acl/marshal.go index cd6c6dd..be9daf0 100644 --- a/acl/marshal.go +++ b/acl/marshal.go @@ -45,16 +45,14 @@ func (t *Table) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, t.StableSize()) + buf = make([]byte, 0, t.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(tableVersionField, buf[offset:], t.version) - offset += protoutil.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid) + buf = protoutil.NestedStructureMarshal(tableVersionField, buf, t.version) + buf = protoutil.NestedStructureMarshal(tableContainerIDField, buf, t.cid) for i := range t.records { - offset += protoutil.NestedStructureMarshal(tableRecordsField, buf[offset:], &t.records[i]) + buf = protoutil.NestedStructureMarshal(tableRecordsField, buf, &t.records[i]) } return buf @@ -88,20 +86,18 @@ func (r *Record) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += protoutil.EnumMarshal(recordOperationField, buf[offset:], int32(r.op)) - offset += protoutil.EnumMarshal(recordActionField, buf[offset:], int32(r.action)) + buf = protoutil.EnumMarshal(recordOperationField, buf, int32(r.op)) + buf = protoutil.EnumMarshal(recordActionField, buf, int32(r.action)) for i := range r.filters { - offset += protoutil.NestedStructureMarshal(recordFiltersField, buf[offset:], &r.filters[i]) + buf = protoutil.NestedStructureMarshal(recordFiltersField, buf, &r.filters[i]) } for i := range r.targets { - offset += protoutil.NestedStructureMarshal(recordTargetsField, buf[offset:], &r.targets[i]) + buf = protoutil.NestedStructureMarshal(recordTargetsField, buf, &r.targets[i]) } return buf @@ -139,15 +135,13 @@ func (f *HeaderFilter) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, f.StableSize()) + buf = make([]byte, 0, f.StableSize()) } - var offset int - - offset += protoutil.EnumMarshal(filterHeaderTypeField, buf[offset:], int32(f.hdrType)) - offset += protoutil.EnumMarshal(filterMatchTypeField, buf[offset:], int32(f.matchType)) - offset += protoutil.StringMarshal(filterNameField, buf[offset:], f.key) - protoutil.StringMarshal(filterValueField, buf[offset:], f.value) + buf = protoutil.EnumMarshal(filterHeaderTypeField, buf, int32(f.hdrType)) + buf = protoutil.EnumMarshal(filterMatchTypeField, buf, int32(f.matchType)) + buf = protoutil.StringMarshal(filterNameField, buf, f.key) + buf = protoutil.StringMarshal(filterValueField, buf, f.value) return buf } @@ -178,13 +172,11 @@ func (t *Target) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, t.StableSize()) + buf = make([]byte, 0, t.StableSize()) } - var offset int - - offset += protoutil.EnumMarshal(targetTypeField, buf[offset:], int32(t.role)) - protoutil.RepeatedBytesMarshal(targetKeysField, buf[offset:], t.keys) + buf = protoutil.EnumMarshal(targetTypeField, buf, int32(t.role)) + buf = protoutil.RepeatedBytesMarshal(targetKeysField, buf, t.keys) return buf } @@ -211,14 +203,12 @@ func (l *TokenLifetime) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, l.StableSize()) + buf = make([]byte, 0, l.StableSize()) } - var offset int - - offset += protoutil.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp) - offset += protoutil.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) - protoutil.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) + buf = protoutil.UInt64Marshal(lifetimeExpirationField, buf, l.exp) + buf = protoutil.UInt64Marshal(lifetimeNotValidBeforeField, buf, l.nbf) + buf = protoutil.UInt64Marshal(lifetimeIssuedAtField, buf, l.iat) return buf } @@ -245,15 +235,13 @@ func (bt *BearerTokenBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, bt.StableSize()) + buf = make([]byte, 0, bt.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(bearerTokenBodyACLField, buf[offset:], bt.eacl) - offset += protoutil.NestedStructureMarshal(bearerTokenBodyOwnerField, buf[offset:], bt.ownerID) - offset += protoutil.NestedStructureMarshal(bearerTokenBodyLifetimeField, buf[offset:], bt.lifetime) - protoutil.BoolMarshal(bearerTokenBodyImpersonate, buf[offset:], bt.impersonate) + buf = protoutil.NestedStructureMarshal(bearerTokenBodyACLField, buf, bt.eacl) + buf = protoutil.NestedStructureMarshal(bearerTokenBodyOwnerField, buf, bt.ownerID) + buf = protoutil.NestedStructureMarshal(bearerTokenBodyLifetimeField, buf, bt.lifetime) + buf = protoutil.BoolMarshal(bearerTokenBodyImpersonate, buf, bt.impersonate) return buf } @@ -281,13 +269,11 @@ func (bt *BearerToken) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, bt.StableSize()) + buf = make([]byte, 0, bt.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(bearerTokenBodyField, buf[offset:], bt.body) - protoutil.NestedStructureMarshal(bearerTokenSignatureField, buf[offset:], bt.sig) + buf = protoutil.NestedStructureMarshal(bearerTokenBodyField, buf, bt.body) + buf = protoutil.NestedStructureMarshal(bearerTokenSignatureField, buf, bt.sig) return buf } diff --git a/audit/marshal.go b/audit/marshal.go index a039baf..214fe71 100644 --- a/audit/marshal.go +++ b/audit/marshal.go @@ -33,25 +33,23 @@ func (a *DataAuditResult) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, a.StableSize()) + buf = make([]byte, 0, a.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(versionFNum, buf[offset:], a.version) - offset += proto.Fixed64Marshal(auditEpochFNum, buf[offset:], a.auditEpoch) - offset += proto.NestedStructureMarshal(cidFNum, buf[offset:], a.cid) - offset += proto.BytesMarshal(pubKeyFNum, buf[offset:], a.pubKey) - offset += proto.BoolMarshal(completeFNum, buf[offset:], a.complete) - offset += proto.UInt32Marshal(requestsFNum, buf[offset:], a.requests) - offset += proto.UInt32Marshal(retriesFNum, buf[offset:], a.retries) - offset += refs.ObjectIDNestedListMarshal(passSGFNum, buf[offset:], a.passSG) - offset += refs.ObjectIDNestedListMarshal(failSGFNum, buf[offset:], a.failSG) - offset += proto.UInt32Marshal(hitFNum, buf[offset:], a.hit) - offset += proto.UInt32Marshal(missFNum, buf[offset:], a.miss) - offset += proto.UInt32Marshal(failFNum, buf[offset:], a.fail) - offset += proto.RepeatedBytesMarshal(passNodesFNum, buf[offset:], a.passNodes) - proto.RepeatedBytesMarshal(failNodesFNum, buf[offset:], a.failNodes) + buf = proto.NestedStructureMarshal(versionFNum, buf, a.version) + buf = proto.Fixed64Marshal(auditEpochFNum, buf, a.auditEpoch) + buf = proto.NestedStructureMarshal(cidFNum, buf, a.cid) + buf = proto.BytesMarshal(pubKeyFNum, buf, a.pubKey) + buf = proto.BoolMarshal(completeFNum, buf, a.complete) + buf = proto.UInt32Marshal(requestsFNum, buf, a.requests) + buf = proto.UInt32Marshal(retriesFNum, buf, a.retries) + buf = refs.ObjectIDNestedListMarshal(passSGFNum, buf, a.passSG) + buf = refs.ObjectIDNestedListMarshal(failSGFNum, buf, a.failSG) + buf = proto.UInt32Marshal(hitFNum, buf, a.hit) + buf = proto.UInt32Marshal(missFNum, buf, a.miss) + buf = proto.UInt32Marshal(failFNum, buf, a.fail) + buf = proto.RepeatedBytesMarshal(passNodesFNum, buf, a.passNodes) + buf = proto.RepeatedBytesMarshal(failNodesFNum, buf, a.failNodes) return buf } diff --git a/container/marshal.go b/container/marshal.go index a7c57aa..c823270 100644 --- a/container/marshal.go +++ b/container/marshal.go @@ -57,13 +57,11 @@ func (a *Attribute) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, a.StableSize()) + buf = make([]byte, 0, a.StableSize()) } - var offset int - - offset += protoutil.StringMarshal(attributeKeyField, buf[offset:], a.key) - protoutil.StringMarshal(attributeValueField, buf[offset:], a.val) + buf = protoutil.StringMarshal(attributeKeyField, buf, a.key) + buf = protoutil.StringMarshal(attributeValueField, buf, a.val) return buf } @@ -89,21 +87,19 @@ func (c *Container) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, c.StableSize()) + buf = make([]byte, 0, c.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(containerVersionField, buf[offset:], c.version) - offset += protoutil.NestedStructureMarshal(containerOwnerField, buf[offset:], c.ownerID) - offset += protoutil.BytesMarshal(containerNonceField, buf[offset:], c.nonce) - offset += protoutil.UInt32Marshal(containerBasicACLField, buf[offset:], c.basicACL) + buf = protoutil.NestedStructureMarshal(containerVersionField, buf, c.version) + buf = protoutil.NestedStructureMarshal(containerOwnerField, buf, c.ownerID) + buf = protoutil.BytesMarshal(containerNonceField, buf, c.nonce) + buf = protoutil.UInt32Marshal(containerBasicACLField, buf, c.basicACL) for i := range c.attr { - offset += protoutil.NestedStructureMarshal(containerAttributesField, buf[offset:], &c.attr[i]) + buf = protoutil.NestedStructureMarshal(containerAttributesField, buf, &c.attr[i]) } - protoutil.NestedStructureMarshal(containerPlacementField, buf[offset:], c.policy) + buf = protoutil.NestedStructureMarshal(containerPlacementField, buf, c.policy) return buf } @@ -137,13 +133,11 @@ func (r *PutRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(putReqBodyContainerField, buf[offset:], r.cnr) - protoutil.NestedStructureMarshal(putReqBodySignatureField, buf[offset:], r.sig) + buf = protoutil.NestedStructureMarshal(putReqBodyContainerField, buf, r.cnr) + buf = protoutil.NestedStructureMarshal(putReqBodySignatureField, buf, r.sig) return buf } @@ -169,10 +163,10 @@ func (r *PutResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - protoutil.NestedStructureMarshal(putRespBodyIDField, buf, r.cid) + buf = protoutil.NestedStructureMarshal(putRespBodyIDField, buf, r.cid) return buf } @@ -197,13 +191,11 @@ func (r *DeleteRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(deleteReqBodyIDField, buf[offset:], r.cid) - protoutil.NestedStructureMarshal(deleteReqBodySignatureField, buf[offset:], r.sig) + buf = protoutil.NestedStructureMarshal(deleteReqBodyIDField, buf, r.cid) + buf = protoutil.NestedStructureMarshal(deleteReqBodySignatureField, buf, r.sig) return buf } @@ -241,10 +233,10 @@ func (r *GetRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - protoutil.NestedStructureMarshal(getReqBodyIDField, buf, r.cid) + buf = protoutil.NestedStructureMarshal(getReqBodyIDField, buf, r.cid) return buf } @@ -269,14 +261,12 @@ func (r *GetResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(getRespBodyContainerField, buf, r.cnr) - offset += protoutil.NestedStructureMarshal(getRespBodySignatureField, buf[offset:], r.sig) - protoutil.NestedStructureMarshal(getRespBodyTokenField, buf[offset:], r.token) + buf = protoutil.NestedStructureMarshal(getRespBodyContainerField, buf, r.cnr) + buf = protoutil.NestedStructureMarshal(getRespBodySignatureField, buf, r.sig) + buf = protoutil.NestedStructureMarshal(getRespBodyTokenField, buf, r.token) return buf } @@ -303,10 +293,10 @@ func (r *ListRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - protoutil.NestedStructureMarshal(listReqBodyOwnerField, buf, r.ownerID) + buf = protoutil.NestedStructureMarshal(listReqBodyOwnerField, buf, r.ownerID) return buf } @@ -331,13 +321,11 @@ func (r *ListResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - for i := range r.cidList { - offset += protoutil.NestedStructureMarshal(listRespBodyIDsField, buf[offset:], &r.cidList[i]) + buf = protoutil.NestedStructureMarshal(listRespBodyIDsField, buf, &r.cidList[i]) } return buf @@ -365,13 +353,11 @@ func (r *SetExtendedACLRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(setEACLReqBodyTableField, buf[offset:], r.eacl) - protoutil.NestedStructureMarshal(setEACLReqBodySignatureField, buf[offset:], r.sig) + buf = protoutil.NestedStructureMarshal(setEACLReqBodyTableField, buf, r.eacl) + buf = protoutil.NestedStructureMarshal(setEACLReqBodySignatureField, buf, r.sig) return buf } @@ -409,10 +395,10 @@ func (r *GetExtendedACLRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - protoutil.NestedStructureMarshal(getEACLReqBodyIDField, buf, r.cid) + buf = protoutil.NestedStructureMarshal(getEACLReqBodyIDField, buf, r.cid) return buf } @@ -437,14 +423,12 @@ func (r *GetExtendedACLResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(getEACLRespBodyTableField, buf[offset:], r.eacl) - offset += protoutil.NestedStructureMarshal(getEACLRespBodySignatureField, buf[offset:], r.sig) - protoutil.NestedStructureMarshal(getEACLRespBodyTokenField, buf[offset:], r.token) + buf = protoutil.NestedStructureMarshal(getEACLRespBodyTableField, buf, r.eacl) + buf = protoutil.NestedStructureMarshal(getEACLRespBodySignatureField, buf, r.sig) + buf = protoutil.NestedStructureMarshal(getEACLRespBodyTokenField, buf, r.token) return buf } @@ -471,14 +455,12 @@ func (a *UsedSpaceAnnouncement) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, a.StableSize()) + buf = make([]byte, 0, a.StableSize()) } - var offset int - - offset += protoutil.UInt64Marshal(usedSpaceAnnounceEpochField, buf[offset:], a.epoch) - offset += protoutil.NestedStructureMarshal(usedSpaceAnnounceCIDField, buf[offset:], a.cid) - protoutil.UInt64Marshal(usedSpaceAnnounceUsedSpaceField, buf[offset:], a.usedSpace) + buf = protoutil.UInt64Marshal(usedSpaceAnnounceEpochField, buf, a.epoch) + buf = protoutil.NestedStructureMarshal(usedSpaceAnnounceCIDField, buf, a.cid) + buf = protoutil.UInt64Marshal(usedSpaceAnnounceUsedSpaceField, buf, a.usedSpace) return buf } @@ -505,13 +487,11 @@ func (r *AnnounceUsedSpaceRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - for i := range r.announcements { - offset += protoutil.NestedStructureMarshal(usedSpaceReqBodyAnnouncementsField, buf[offset:], &r.announcements[i]) + buf = protoutil.NestedStructureMarshal(usedSpaceReqBodyAnnouncementsField, buf, &r.announcements[i]) } return buf diff --git a/netmap/marshal.go b/netmap/marshal.go index 4f2c1ce..e05d418 100644 --- a/netmap/marshal.go +++ b/netmap/marshal.go @@ -47,18 +47,16 @@ func (f *Filter) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, f.StableSize()) + buf = make([]byte, 0, f.StableSize()) } - var offset int - - offset += protoutil.StringMarshal(nameFilterField, buf[offset:], f.name) - offset += protoutil.StringMarshal(keyFilterField, buf[offset:], f.key) - offset += protoutil.EnumMarshal(opFilterField, buf[offset:], int32(f.op)) - offset += protoutil.StringMarshal(valueFilterField, buf[offset:], f.value) + buf = protoutil.StringMarshal(nameFilterField, buf, f.name) + buf = protoutil.StringMarshal(keyFilterField, buf, f.key) + buf = protoutil.EnumMarshal(opFilterField, buf, int32(f.op)) + buf = protoutil.StringMarshal(valueFilterField, buf, f.value) for i := range f.filters { - offset += protoutil.NestedStructureMarshal(filtersFilterField, buf[offset:], &f.filters[i]) + buf = protoutil.NestedStructureMarshal(filtersFilterField, buf, &f.filters[i]) } return buf @@ -90,16 +88,14 @@ func (s *Selector) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, s.StableSize()) + buf = make([]byte, 0, s.StableSize()) } - var offset int - - offset += protoutil.StringMarshal(nameSelectorField, buf[offset:], s.name) - offset += protoutil.UInt32Marshal(countSelectorField, buf[offset:], s.count) - offset += protoutil.EnumMarshal(clauseSelectorField, buf[offset:], int32(s.clause)) - offset += protoutil.StringMarshal(attributeSelectorField, buf[offset:], s.attribute) - protoutil.StringMarshal(filterSelectorField, buf[offset:], s.filter) + buf = protoutil.StringMarshal(nameSelectorField, buf, s.name) + buf = protoutil.UInt32Marshal(countSelectorField, buf, s.count) + buf = protoutil.EnumMarshal(clauseSelectorField, buf, int32(s.clause)) + buf = protoutil.StringMarshal(attributeSelectorField, buf, s.attribute) + buf = protoutil.StringMarshal(filterSelectorField, buf, s.filter) return buf } @@ -128,13 +124,11 @@ func (r *Replica) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += protoutil.UInt32Marshal(countReplicaField, buf[offset:], r.count) - protoutil.StringMarshal(selectorReplicaField, buf[offset:], r.selector) + buf = protoutil.UInt32Marshal(countReplicaField, buf, r.count) + buf = protoutil.StringMarshal(selectorReplicaField, buf, r.selector) return buf } @@ -160,26 +154,24 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, p.StableSize()) + buf = make([]byte, 0, p.StableSize()) } - var offset int - for i := range p.replicas { - offset += protoutil.NestedStructureMarshal(replicasPolicyField, buf[offset:], &p.replicas[i]) + buf = protoutil.NestedStructureMarshal(replicasPolicyField, buf, &p.replicas[i]) } - offset += protoutil.UInt32Marshal(backupPolicyField, buf[offset:], p.backupFactor) + buf = protoutil.UInt32Marshal(backupPolicyField, buf, p.backupFactor) for i := range p.selectors { - offset += protoutil.NestedStructureMarshal(selectorsPolicyField, buf[offset:], &p.selectors[i]) + buf = protoutil.NestedStructureMarshal(selectorsPolicyField, buf, &p.selectors[i]) } for i := range p.filters { - offset += protoutil.NestedStructureMarshal(filtersPolicyField, buf[offset:], &p.filters[i]) + buf = protoutil.NestedStructureMarshal(filtersPolicyField, buf, &p.filters[i]) } - protoutil.BoolMarshal(uniquePolicyField, buf[offset:], p.unique) + buf = protoutil.BoolMarshal(uniquePolicyField, buf, p.unique) return buf } @@ -218,16 +210,14 @@ func (a *Attribute) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, a.StableSize()) + buf = make([]byte, 0, a.StableSize()) } - var offset int - - offset += protoutil.StringMarshal(keyAttributeField, buf[offset:], a.key) - offset += protoutil.StringMarshal(valueAttributeField, buf[offset:], a.value) + buf = protoutil.StringMarshal(keyAttributeField, buf, a.key) + buf = protoutil.StringMarshal(valueAttributeField, buf, a.value) for i := range a.parents { - offset += protoutil.StringMarshal(parentsAttributeField, buf[offset:], a.parents[i]) + buf = protoutil.StringMarshal(parentsAttributeField, buf, a.parents[i]) } return buf @@ -258,19 +248,17 @@ func (ni *NodeInfo) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, ni.StableSize()) + buf = make([]byte, 0, ni.StableSize()) } - var offset int - - offset += protoutil.BytesMarshal(keyNodeInfoField, buf[offset:], ni.publicKey) - offset += protoutil.RepeatedStringMarshal(addressNodeInfoField, buf[offset:], ni.addresses) + buf = protoutil.BytesMarshal(keyNodeInfoField, buf, ni.publicKey) + buf = protoutil.RepeatedStringMarshal(addressNodeInfoField, buf, ni.addresses) for i := range ni.attributes { - offset += protoutil.NestedStructureMarshal(attributesNodeInfoField, buf[offset:], &ni.attributes[i]) + buf = protoutil.NestedStructureMarshal(attributesNodeInfoField, buf, &ni.attributes[i]) } - protoutil.EnumMarshal(stateNodeInfoField, buf[offset:], int32(ni.state)) + buf = protoutil.EnumMarshal(stateNodeInfoField, buf, int32(ni.state)) return buf } @@ -314,13 +302,11 @@ func (l *LocalNodeInfoResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, l.StableSize()) + buf = make([]byte, 0, l.StableSize()) } - var offset int - - offset += protoutil.NestedStructureMarshal(versionInfoResponseBodyField, buf[offset:], l.version) - protoutil.NestedStructureMarshal(nodeInfoResponseBodyField, buf[offset:], l.nodeInfo) + buf = protoutil.NestedStructureMarshal(versionInfoResponseBodyField, buf, l.version) + buf = protoutil.NestedStructureMarshal(nodeInfoResponseBodyField, buf, l.nodeInfo) return buf } @@ -352,13 +338,11 @@ func (x *NetworkParameter) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - var offset int - - offset += protoutil.BytesMarshal(netPrmKeyFNum, buf[offset:], x.k) - protoutil.BytesMarshal(netPrmValFNum, buf[offset:], x.v) + buf = protoutil.BytesMarshal(netPrmKeyFNum, buf, x.k) + buf = protoutil.BytesMarshal(netPrmValFNum, buf, x.v) return buf } @@ -385,13 +369,11 @@ func (x *NetworkConfig) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - var offset int - for i := range x.ps { - offset += protoutil.NestedStructureMarshal(netCfgPrmsFNum, buf[offset:], &x.ps[i]) + buf = protoutil.NestedStructureMarshal(netCfgPrmsFNum, buf, &x.ps[i]) } return buf @@ -423,15 +405,13 @@ func (i *NetworkInfo) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, i.StableSize()) + buf = make([]byte, 0, i.StableSize()) } - var offset int - - offset += protoutil.UInt64Marshal(netInfoCurEpochFNum, buf[offset:], i.curEpoch) - offset += protoutil.UInt64Marshal(netInfoMagicNumFNum, buf[offset:], i.magicNum) - offset += protoutil.Int64Marshal(netInfoMSPerBlockFNum, buf[offset:], i.msPerBlock) - protoutil.NestedStructureMarshal(netInfoCfgFNum, buf[offset:], i.netCfg) + buf = protoutil.UInt64Marshal(netInfoCurEpochFNum, buf, i.curEpoch) + buf = protoutil.UInt64Marshal(netInfoMagicNumFNum, buf, i.magicNum) + buf = protoutil.Int64Marshal(netInfoMSPerBlockFNum, buf, i.msPerBlock) + buf = protoutil.NestedStructureMarshal(netInfoCfgFNum, buf, i.netCfg) return buf } @@ -476,10 +456,10 @@ func (i *NetworkInfoResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, i.StableSize()) + buf = make([]byte, 0, i.StableSize()) } - protoutil.NestedStructureMarshal(netInfoRespBodyNetInfoFNum, buf, i.netInfo) + buf = protoutil.NestedStructureMarshal(netInfoRespBodyNetInfoFNum, buf, i.netInfo) return buf } @@ -510,13 +490,13 @@ func (x *NetMap) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - offset := protoutil.UInt64Marshal(fNumNetMapEpoch, buf, x.epoch) + buf = protoutil.UInt64Marshal(fNumNetMapEpoch, buf, x.epoch) for i := range x.nodes { - offset += protoutil.NestedStructureMarshal(fNumNetMapNodes, buf[offset:], &x.nodes[i]) + buf = protoutil.NestedStructureMarshal(fNumNetMapNodes, buf, &x.nodes[i]) } return buf @@ -553,10 +533,10 @@ func (x *SnapshotResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - protoutil.NestedStructureMarshal(fNumSnapshotResponseBodyNetMap, buf, x.netMap) + buf = protoutil.NestedStructureMarshal(fNumSnapshotResponseBodyNetMap, buf, x.netMap) return buf } diff --git a/object/lock.go b/object/lock.go index 62ba4a9..78ea1f5 100644 --- a/object/lock.go +++ b/object/lock.go @@ -54,13 +54,11 @@ func (x *Lock) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - var offset int - for i := range x.members { - offset += proto.NestedStructureMarshal(fNumLockMembers, buf[offset:], &x.members[i]) + buf = proto.NestedStructureMarshal(fNumLockMembers, buf, &x.members[i]) } return buf diff --git a/object/marshal.go b/object/marshal.go index fe0e232..e87a3a5 100644 --- a/object/marshal.go +++ b/object/marshal.go @@ -121,18 +121,16 @@ func (h *ShortHeader) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, h.StableSize()) + buf = make([]byte, 0, h.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(shortHdrVersionField, buf[offset:], h.version) - offset += proto.UInt64Marshal(shortHdrEpochField, buf[offset:], h.creatEpoch) - offset += proto.NestedStructureMarshal(shortHdrOwnerField, buf[offset:], h.ownerID) - offset += proto.EnumMarshal(shortHdrObjectTypeField, buf[offset:], int32(h.typ)) - offset += proto.UInt64Marshal(shortHdrPayloadLength, buf[offset:], h.payloadLen) - offset += proto.NestedStructureMarshal(shortHdrHashField, buf[offset:], h.payloadHash) - proto.NestedStructureMarshal(shortHdrHomoHashField, buf[offset:], h.homoHash) + buf = proto.NestedStructureMarshal(shortHdrVersionField, buf, h.version) + buf = proto.UInt64Marshal(shortHdrEpochField, buf, h.creatEpoch) + buf = proto.NestedStructureMarshal(shortHdrOwnerField, buf, h.ownerID) + buf = proto.EnumMarshal(shortHdrObjectTypeField, buf, int32(h.typ)) + buf = proto.UInt64Marshal(shortHdrPayloadLength, buf, h.payloadLen) + buf = proto.NestedStructureMarshal(shortHdrHashField, buf, h.payloadHash) + buf = proto.NestedStructureMarshal(shortHdrHomoHashField, buf, h.homoHash) return buf } @@ -163,13 +161,11 @@ func (a *Attribute) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, a.StableSize()) + buf = make([]byte, 0, a.StableSize()) } - var offset int - - offset += proto.StringMarshal(attributeKeyField, buf[offset:], a.key) - proto.StringMarshal(attributeValueField, buf[offset:], a.val) + buf = proto.StringMarshal(attributeKeyField, buf, a.key) + buf = proto.StringMarshal(attributeValueField, buf, a.val) return buf } @@ -195,17 +191,15 @@ func (h *SplitHeader) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, h.StableSize()) + buf = make([]byte, 0, h.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(splitHdrParentField, buf[offset:], h.par) - offset += proto.NestedStructureMarshal(splitHdrPreviousField, buf[offset:], h.prev) - offset += proto.NestedStructureMarshal(splitHdrParentSignatureField, buf[offset:], h.parSig) - offset += proto.NestedStructureMarshal(splitHdrParentHeaderField, buf[offset:], h.parHdr) - offset += refs.ObjectIDNestedListMarshal(splitHdrChildrenField, buf[offset:], h.children) - proto.BytesMarshal(splitHdrSplitIDField, buf[offset:], h.splitID) + buf = proto.NestedStructureMarshal(splitHdrParentField, buf, h.par) + buf = proto.NestedStructureMarshal(splitHdrPreviousField, buf, h.prev) + buf = proto.NestedStructureMarshal(splitHdrParentSignatureField, buf, h.parSig) + buf = proto.NestedStructureMarshal(splitHdrParentHeaderField, buf, h.parHdr) + buf = refs.ObjectIDNestedListMarshal(splitHdrChildrenField, buf, h.children) + buf = proto.BytesMarshal(splitHdrSplitIDField, buf, h.splitID) return buf } @@ -235,26 +229,24 @@ func (h *Header) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, h.StableSize()) + buf = make([]byte, 0, h.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(hdrVersionField, buf[offset:], h.version) - offset += proto.NestedStructureMarshal(hdrContainerIDField, buf[offset:], h.cid) - offset += proto.NestedStructureMarshal(hdrOwnerIDField, buf[offset:], h.ownerID) - offset += proto.UInt64Marshal(hdrEpochField, buf[offset:], h.creatEpoch) - offset += proto.UInt64Marshal(hdrPayloadLengthField, buf[offset:], h.payloadLen) - offset += proto.NestedStructureMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash) - offset += proto.EnumMarshal(hdrObjectTypeField, buf[offset:], int32(h.typ)) - offset += proto.NestedStructureMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash) - offset += proto.NestedStructureMarshal(hdrSessionTokenField, buf[offset:], h.sessionToken) + buf = proto.NestedStructureMarshal(hdrVersionField, buf, h.version) + buf = proto.NestedStructureMarshal(hdrContainerIDField, buf, h.cid) + buf = proto.NestedStructureMarshal(hdrOwnerIDField, buf, h.ownerID) + buf = proto.UInt64Marshal(hdrEpochField, buf, h.creatEpoch) + buf = proto.UInt64Marshal(hdrPayloadLengthField, buf, h.payloadLen) + buf = proto.NestedStructureMarshal(hdrPayloadHashField, buf, h.payloadHash) + buf = proto.EnumMarshal(hdrObjectTypeField, buf, int32(h.typ)) + buf = proto.NestedStructureMarshal(hdrHomomorphicHashField, buf, h.homoHash) + buf = proto.NestedStructureMarshal(hdrSessionTokenField, buf, h.sessionToken) for i := range h.attr { - offset += proto.NestedStructureMarshal(hdrAttributesField, buf[offset:], &h.attr[i]) + buf = proto.NestedStructureMarshal(hdrAttributesField, buf, &h.attr[i]) } - proto.NestedStructureMarshal(hdrSplitField, buf[offset:], h.split) + buf = proto.NestedStructureMarshal(hdrSplitField, buf, h.split) return buf } @@ -291,13 +283,11 @@ func (h *HeaderWithSignature) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, h.StableSize()) + buf = make([]byte, 0, h.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(hdrWithSigHeaderField, buf[offset:], h.header) - proto.NestedStructureMarshal(hdrWithSigSignatureField, buf[offset:], h.signature) + buf = proto.NestedStructureMarshal(hdrWithSigHeaderField, buf, h.header) + buf = proto.NestedStructureMarshal(hdrWithSigSignatureField, buf, h.signature) return buf } @@ -323,15 +313,13 @@ func (o *Object) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, o.StableSize()) + buf = make([]byte, 0, o.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(objIDField, buf[offset:], o.objectID) - offset += proto.NestedStructureMarshal(objSignatureField, buf[offset:], o.idSig) - offset += proto.NestedStructureMarshal(objHeaderField, buf[offset:], o.header) - proto.BytesMarshal(objPayloadField, buf[offset:], o.payload) + buf = proto.NestedStructureMarshal(objIDField, buf, o.objectID) + buf = proto.NestedStructureMarshal(objSignatureField, buf, o.idSig) + buf = proto.NestedStructureMarshal(objHeaderField, buf, o.header) + buf = proto.BytesMarshal(objPayloadField, buf, o.payload) return buf } @@ -359,14 +347,12 @@ func (s *SplitInfo) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, s.StableSize()) + buf = make([]byte, 0, s.StableSize()) } - var offset int - - offset += proto.BytesMarshal(splitInfoSplitIDField, buf[offset:], s.splitID) - offset += proto.NestedStructureMarshal(splitInfoLastPartField, buf[offset:], s.lastPart) - proto.NestedStructureMarshal(splitInfoLinkField, buf[offset:], s.link) + buf = proto.BytesMarshal(splitInfoSplitIDField, buf, s.splitID) + buf = proto.NestedStructureMarshal(splitInfoLastPartField, buf, s.lastPart) + buf = proto.NestedStructureMarshal(splitInfoLinkField, buf, s.link) return buf } @@ -393,13 +379,11 @@ func (r *GetRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(getReqBodyAddressField, buf[offset:], r.addr) - proto.BoolMarshal(getReqBodyRawFlagField, buf[offset:], r.raw) + buf = proto.NestedStructureMarshal(getReqBodyAddressField, buf, r.addr) + buf = proto.BoolMarshal(getReqBodyRawFlagField, buf, r.raw) return buf } @@ -425,14 +409,12 @@ func (r *GetObjectPartInit) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(getRespInitObjectIDField, buf[offset:], r.id) - offset += proto.NestedStructureMarshal(getRespInitSignatureField, buf[offset:], r.sig) - proto.NestedStructureMarshal(getRespInitHeaderField, buf[offset:], r.hdr) + buf = proto.NestedStructureMarshal(getRespInitObjectIDField, buf, r.id) + buf = proto.NestedStructureMarshal(getRespInitSignatureField, buf, r.sig) + buf = proto.NestedStructureMarshal(getRespInitHeaderField, buf, r.hdr) return buf } @@ -459,19 +441,19 @@ func (r *GetResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } switch v := r.objPart.(type) { case nil: case *GetObjectPartInit: - proto.NestedStructureMarshal(getRespBodyInitField, buf, v) + buf = proto.NestedStructureMarshal(getRespBodyInitField, buf, v) case *GetObjectPartChunk: if v != nil { - proto.BytesMarshal(getRespBodyChunkField, buf, v.chunk) + buf = proto.BytesMarshal(getRespBodyChunkField, buf, v.chunk) } case *SplitInfo: - proto.NestedStructureMarshal(getRespBodySplitInfoField, buf, v) + buf = proto.NestedStructureMarshal(getRespBodySplitInfoField, buf, v) default: panic("unknown one of object get response body type") } @@ -511,15 +493,13 @@ func (r *PutObjectPartInit) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(putReqInitObjectIDField, buf[offset:], r.id) - offset += proto.NestedStructureMarshal(putReqInitSignatureField, buf[offset:], r.sig) - offset += proto.NestedStructureMarshal(putReqInitHeaderField, buf[offset:], r.hdr) - proto.RepeatedUInt32Marshal(putReqInitCopiesNumField, buf[offset:], r.copyNum) + buf = proto.NestedStructureMarshal(putReqInitObjectIDField, buf, r.id) + buf = proto.NestedStructureMarshal(putReqInitSignatureField, buf, r.sig) + buf = proto.NestedStructureMarshal(putReqInitHeaderField, buf, r.hdr) + buf = proto.RepeatedUInt32Marshal(putReqInitCopiesNumField, buf, r.copyNum) return buf } @@ -549,16 +529,16 @@ func (r *PutRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } switch v := r.objPart.(type) { case nil: case *PutObjectPartInit: - proto.NestedStructureMarshal(putReqBodyInitField, buf, v) + buf = proto.NestedStructureMarshal(putReqBodyInitField, buf, v) case *PutObjectPartChunk: if v != nil { - proto.BytesMarshal(putReqBodyChunkField, buf, v.chunk) + buf = proto.BytesMarshal(putReqBodyChunkField, buf, v.chunk) } default: panic("unknown one of object put request body type") @@ -597,10 +577,10 @@ func (r *PutResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - proto.NestedStructureMarshal(putRespBodyObjectIDField, buf, r.id) + buf = proto.NestedStructureMarshal(putRespBodyObjectIDField, buf, r.id) return buf } @@ -625,10 +605,10 @@ func (r *DeleteRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - proto.NestedStructureMarshal(deleteReqBodyAddressField, buf, r.addr) + buf = proto.NestedStructureMarshal(deleteReqBodyAddressField, buf, r.addr) return buf } @@ -653,10 +633,10 @@ func (r *DeleteResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - proto.NestedStructureMarshal(deleteRespBodyTombstoneFNum, buf, r.tombstone) + buf = proto.NestedStructureMarshal(deleteRespBodyTombstoneFNum, buf, r.tombstone) return buf } @@ -681,14 +661,12 @@ func (r *HeadRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(headReqBodyAddressField, buf[offset:], r.addr) - offset += proto.BoolMarshal(headReqBodyMainFlagField, buf[offset:], r.mainOnly) - proto.BoolMarshal(headReqBodyRawFlagField, buf[offset:], r.raw) + buf = proto.NestedStructureMarshal(headReqBodyAddressField, buf, r.addr) + buf = proto.BoolMarshal(headReqBodyMainFlagField, buf, r.mainOnly) + buf = proto.BoolMarshal(headReqBodyRawFlagField, buf, r.raw) return buf } @@ -715,22 +693,22 @@ func (r *HeadResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } switch v := r.hdrPart.(type) { case nil: case *HeaderWithSignature: if v != nil { - proto.NestedStructureMarshal(headRespBodyHeaderField, buf, v) + buf = proto.NestedStructureMarshal(headRespBodyHeaderField, buf, v) } case *ShortHeader: if v != nil { - proto.NestedStructureMarshal(headRespBodyShortHeaderField, buf, v) + buf = proto.NestedStructureMarshal(headRespBodyShortHeaderField, buf, v) } case *SplitInfo: if v != nil { - proto.NestedStructureMarshal(headRespBodySplitInfoField, buf, v) + buf = proto.NestedStructureMarshal(headRespBodySplitInfoField, buf, v) } default: panic("unknown one of object put request body type") @@ -775,14 +753,12 @@ func (f *SearchFilter) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, f.StableSize()) + buf = make([]byte, 0, f.StableSize()) } - var offset int - - offset += proto.EnumMarshal(searchFilterMatchField, buf[offset:], int32(f.matchType)) - offset += proto.StringMarshal(searchFilterNameField, buf[offset:], f.key) - proto.StringMarshal(searchFilterValueField, buf[offset:], f.val) + buf = proto.EnumMarshal(searchFilterMatchField, buf, int32(f.matchType)) + buf = proto.StringMarshal(searchFilterNameField, buf, f.key) + buf = proto.StringMarshal(searchFilterValueField, buf, f.val) return buf } @@ -809,16 +785,14 @@ func (r *SearchRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(searchReqBodyContainerIDField, buf[offset:], r.cid) - offset += proto.UInt32Marshal(searchReqBodyVersionField, buf[offset:], r.version) + buf = proto.NestedStructureMarshal(searchReqBodyContainerIDField, buf, r.cid) + buf = proto.UInt32Marshal(searchReqBodyVersionField, buf, r.version) for i := range r.filters { - offset += proto.NestedStructureMarshal(searchReqBodyFiltersField, buf[offset:], &r.filters[i]) + buf = proto.NestedStructureMarshal(searchReqBodyFiltersField, buf, &r.filters[i]) } return buf @@ -849,12 +823,10 @@ func (r *SearchResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - refs.ObjectIDNestedListMarshal(searchRespBodyObjectIDsField, buf[offset:], r.idList) + buf = refs.ObjectIDNestedListMarshal(searchRespBodyObjectIDsField, buf, r.idList) return buf } @@ -879,13 +851,11 @@ func (r *Range) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.UInt64Marshal(rangeOffsetField, buf[offset:], r.off) - proto.UInt64Marshal(rangeLengthField, buf[offset:], r.len) + buf = proto.UInt64Marshal(rangeOffsetField, buf, r.off) + buf = proto.UInt64Marshal(rangeLengthField, buf, r.len) return buf } @@ -911,14 +881,12 @@ func (r *GetRangeRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(getRangeReqBodyAddressField, buf[offset:], r.addr) - offset += proto.NestedStructureMarshal(getRangeReqBodyRangeField, buf[offset:], r.rng) - proto.BoolMarshal(getRangeReqBodyRawField, buf[offset:], r.raw) + buf = proto.NestedStructureMarshal(getRangeReqBodyAddressField, buf, r.addr) + buf = proto.NestedStructureMarshal(getRangeReqBodyRangeField, buf, r.rng) + buf = proto.BoolMarshal(getRangeReqBodyRawField, buf, r.raw) return buf } @@ -945,18 +913,18 @@ func (r *GetRangeResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } switch v := r.rngPart.(type) { case nil: case *GetRangePartChunk: if v != nil { - proto.BytesMarshal(getRangeRespChunkField, buf, v.chunk) + buf = proto.BytesMarshal(getRangeRespChunkField, buf, v.chunk) } case *SplitInfo: if v != nil { - proto.NestedStructureMarshal(getRangeRespSplitInfoField, buf, v) + buf = proto.NestedStructureMarshal(getRangeRespSplitInfoField, buf, v) } default: panic("unknown one of object get range request body type") @@ -997,19 +965,17 @@ func (r *GetRangeHashRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(getRangeHashReqBodyAddressField, buf[offset:], r.addr) + buf = proto.NestedStructureMarshal(getRangeHashReqBodyAddressField, buf, r.addr) for i := range r.rngs { - offset += proto.NestedStructureMarshal(getRangeHashReqBodyRangesField, buf[offset:], &r.rngs[i]) + buf = proto.NestedStructureMarshal(getRangeHashReqBodyRangesField, buf, &r.rngs[i]) } - offset += proto.BytesMarshal(getRangeHashReqBodySaltField, buf[offset:], r.salt) - proto.EnumMarshal(getRangeHashReqBodyTypeField, buf[offset:], int32(r.typ)) + buf = proto.BytesMarshal(getRangeHashReqBodySaltField, buf, r.salt) + buf = proto.EnumMarshal(getRangeHashReqBodyTypeField, buf, int32(r.typ)) return buf } @@ -1041,13 +1007,11 @@ func (r *GetRangeHashResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.EnumMarshal(getRangeHashRespBodyTypeField, buf, int32(r.typ)) - proto.RepeatedBytesMarshal(getRangeHashRespBodyHashListField, buf[offset:], r.hashList) + buf = proto.EnumMarshal(getRangeHashRespBodyTypeField, buf, int32(r.typ)) + buf = proto.RepeatedBytesMarshal(getRangeHashRespBodyHashListField, buf, r.hashList) return buf } @@ -1072,12 +1036,11 @@ func (r *PutSingleRequestBody) StableMarshal(buf []byte) []byte { return []byte{} } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - offset += proto.NestedStructureMarshal(putSingleReqObjectField, buf[offset:], r.object) - proto.RepeatedUInt32Marshal(putSingleReqCopiesNumberField, buf[offset:], r.copyNum) + buf = proto.NestedStructureMarshal(putSingleReqObjectField, buf, r.object) + buf = proto.RepeatedUInt32Marshal(putSingleReqCopiesNumberField, buf, r.copyNum) return buf } @@ -1105,7 +1068,7 @@ func (r *PutSingleResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } return buf diff --git a/refs/bench_test.go b/refs/bench_test.go index b24e674..f7e5782 100644 --- a/refs/bench_test.go +++ b/refs/bench_test.go @@ -43,9 +43,9 @@ func benchmarkObjectIDSlice(b *testing.B, size int) { b.Run("marshal", func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - buf := make([]byte, ObjectIDNestedListSize(1, ids)) - n := ObjectIDNestedListMarshal(1, buf, ids) - if n != len(buf) { + buf := make([]byte, 0, ObjectIDNestedListSize(1, ids)) + buf = ObjectIDNestedListMarshal(1, buf, ids) + if len(buf) != cap(buf) { b.FailNow() } } diff --git a/refs/marshal.go b/refs/marshal.go index ef197b3..6168a96 100644 --- a/refs/marshal.go +++ b/refs/marshal.go @@ -1,8 +1,6 @@ package refs import ( - "encoding/binary" - refs "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs/grpc" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto" @@ -36,10 +34,10 @@ func (o *OwnerID) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, o.StableSize()) + buf = make([]byte, 0, o.StableSize()) } - proto.BytesMarshal(ownerIDValField, buf, o.val) + buf = proto.BytesMarshal(ownerIDValField, buf, o.val) return buf } @@ -62,10 +60,10 @@ func (c *ContainerID) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, c.StableSize()) + buf = make([]byte, 0, c.StableSize()) } - proto.BytesMarshal(containerIDValField, buf, c.val) + buf = proto.BytesMarshal(containerIDValField, buf, c.val) return buf } @@ -88,10 +86,10 @@ func (o *ObjectID) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, o.StableSize()) + buf = make([]byte, 0, o.StableSize()) } - proto.BytesMarshal(objectIDValField, buf, o.val) + buf = proto.BytesMarshal(objectIDValField, buf, o.val) return buf } @@ -116,17 +114,16 @@ func (o *ObjectID) StableSize() int { // ObjectIDNestedListMarshal writes protobuf repeated ObjectID field // with fNum number to buf. -func ObjectIDNestedListMarshal(fNum int64, buf []byte, ids []ObjectID) (off int) { - prefix := protowire.EncodeTag(protowire.Number(fNum), protowire.BytesType) +func ObjectIDNestedListMarshal(fNum int64, buf []byte, ids []ObjectID) []byte { for i := range ids { - off += binary.PutUvarint(buf[off:], prefix) + buf = protowire.AppendTag(buf, protowire.Number(fNum), protowire.BytesType) n := ids[i].StableSize() - off += binary.PutUvarint(buf[off:], uint64(n)) - off += proto.BytesMarshal(objectIDValField, buf[off:], ids[i].val) + buf = protowire.AppendVarint(buf, uint64(n)) + buf = proto.BytesMarshal(objectIDValField, buf, ids[i].val) } - return + return buf } func (o *ObjectID) Unmarshal(data []byte) error { @@ -139,13 +136,11 @@ func (a *Address) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, a.StableSize()) + buf = make([]byte, 0, a.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(addressContainerField, buf[offset:], a.cid) - proto.NestedStructureMarshal(addressObjectField, buf[offset:], a.oid) + buf = proto.NestedStructureMarshal(addressContainerField, buf, a.cid) + buf = proto.NestedStructureMarshal(addressObjectField, buf, a.oid) return buf } @@ -171,13 +166,11 @@ func (c *Checksum) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, c.StableSize()) + buf = make([]byte, 0, c.StableSize()) } - var offset int - - offset += proto.EnumMarshal(checksumTypeField, buf[offset:], int32(c.typ)) - proto.BytesMarshal(checksumValueField, buf[offset:], c.sum) + buf = proto.EnumMarshal(checksumTypeField, buf, int32(c.typ)) + buf = proto.BytesMarshal(checksumValueField, buf, c.sum) return buf } @@ -203,14 +196,12 @@ func (s *Signature) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, s.StableSize()) + buf = make([]byte, 0, s.StableSize()) } - var offset int - - offset += proto.BytesMarshal(signatureKeyField, buf[offset:], s.key) - offset += proto.BytesMarshal(signatureValueField, buf[offset:], s.sign) - proto.EnumMarshal(signatureSchemeField, buf[offset:], int32(s.scheme)) + buf = proto.BytesMarshal(signatureKeyField, buf, s.key) + buf = proto.BytesMarshal(signatureValueField, buf, s.sign) + buf = proto.EnumMarshal(signatureSchemeField, buf, int32(s.scheme)) return buf } @@ -237,13 +228,11 @@ func (v *Version) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, v.StableSize()) + buf = make([]byte, 0, v.StableSize()) } - var offset int - - offset += proto.UInt32Marshal(versionMajorField, buf[offset:], v.major) - proto.UInt32Marshal(versionMinorField, buf[offset:], v.minor) + buf = proto.UInt32Marshal(versionMajorField, buf, v.major) + buf = proto.UInt32Marshal(versionMinorField, buf, v.minor) return buf } diff --git a/refs/test/generate.go b/refs/test/generate.go index 6217a96..f0c8084 100644 --- a/refs/test/generate.go +++ b/refs/test/generate.go @@ -1,8 +1,6 @@ package refstest import ( - "math/rand" - "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" ) @@ -90,7 +88,7 @@ func GenerateSignature(empty bool) *refs.Signature { if !empty { m.SetKey([]byte{1}) m.SetSign([]byte{2}) - m.SetScheme(refs.SignatureScheme(rand.Int31() % 3)) + m.SetScheme(refs.SignatureScheme(2)) } return m diff --git a/session/marshal.go b/session/marshal.go index cda9579..ca4d718 100644 --- a/session/marshal.go +++ b/session/marshal.go @@ -67,13 +67,11 @@ func (c *CreateRequestBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, c.StableSize()) + buf = make([]byte, 0, c.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(createReqBodyOwnerField, buf[offset:], c.ownerID) - proto.UInt64Marshal(createReqBodyExpirationField, buf[offset:], c.expiration) + buf = proto.NestedStructureMarshal(createReqBodyOwnerField, buf, c.ownerID) + buf = proto.UInt64Marshal(createReqBodyExpirationField, buf, c.expiration) return buf } @@ -99,13 +97,11 @@ func (c *CreateResponseBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, c.StableSize()) + buf = make([]byte, 0, c.StableSize()) } - var offset int - - offset += proto.BytesMarshal(createRespBodyIDField, buf[offset:], c.id) - proto.BytesMarshal(createRespBodyKeyField, buf[offset:], c.sessionKey) + buf = proto.BytesMarshal(createRespBodyIDField, buf, c.id) + buf = proto.BytesMarshal(createRespBodyKeyField, buf, c.sessionKey) return buf } @@ -131,13 +127,11 @@ func (x *XHeader) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - var offset int - - offset += proto.StringMarshal(xheaderKeyField, buf[offset:], x.key) - proto.StringMarshal(xheaderValueField, buf[offset:], x.val) + buf = proto.StringMarshal(xheaderKeyField, buf, x.key) + buf = proto.StringMarshal(xheaderValueField, buf, x.val) return buf } @@ -168,14 +162,12 @@ func (l *TokenLifetime) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, l.StableSize()) + buf = make([]byte, 0, l.StableSize()) } - var offset int - - offset += proto.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp) - offset += proto.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) - proto.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) + buf = proto.UInt64Marshal(lifetimeExpirationField, buf, l.exp) + buf = proto.UInt64Marshal(lifetimeNotValidBeforeField, buf, l.nbf) + buf = proto.UInt64Marshal(lifetimeIssuedAtField, buf, l.iat) return buf } @@ -207,11 +199,11 @@ func (c *ObjectSessionContext) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, c.StableSize()) + buf = make([]byte, 0, c.StableSize()) } - offset := proto.EnumMarshal(objectCtxVerbField, buf, int32(c.verb)) - proto.NestedStructureMarshal(objectCtxTargetField, buf[offset:], objectSessionContextTarget{ + buf = proto.EnumMarshal(objectCtxVerbField, buf, int32(c.verb)) + buf = proto.NestedStructureMarshal(objectCtxTargetField, buf, objectSessionContextTarget{ cnr: c.cnr, objs: c.objs, }) @@ -255,14 +247,12 @@ func (x *ContainerSessionContext) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - var offset int - - offset += proto.EnumMarshal(cnrCtxVerbFNum, buf[offset:], int32(ContainerSessionVerbToGRPCField(x.verb))) - offset += proto.BoolMarshal(cnrCtxWildcardFNum, buf[offset:], x.wildcard) - proto.NestedStructureMarshal(cnrCtxCidFNum, buf[offset:], x.cid) + buf = proto.EnumMarshal(cnrCtxVerbFNum, buf, int32(ContainerSessionVerbToGRPCField(x.verb))) + buf = proto.BoolMarshal(cnrCtxWildcardFNum, buf, x.wildcard) + buf = proto.NestedStructureMarshal(cnrCtxCidFNum, buf, x.cid) return buf } @@ -289,22 +279,20 @@ func (t *TokenBody) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, t.StableSize()) + buf = make([]byte, 0, t.StableSize()) } - var offset int - - offset += proto.BytesMarshal(sessionTokenBodyIDField, buf[offset:], t.id) - offset += proto.NestedStructureMarshal(sessionTokenBodyOwnerField, buf[offset:], t.ownerID) - offset += proto.NestedStructureMarshal(sessionTokenBodyLifetimeField, buf[offset:], t.lifetime) - offset += proto.BytesMarshal(sessionTokenBodyKeyField, buf[offset:], t.sessionKey) + buf = proto.BytesMarshal(sessionTokenBodyIDField, buf, t.id) + buf = proto.NestedStructureMarshal(sessionTokenBodyOwnerField, buf, t.ownerID) + buf = proto.NestedStructureMarshal(sessionTokenBodyLifetimeField, buf, t.lifetime) + buf = proto.BytesMarshal(sessionTokenBodyKeyField, buf, t.sessionKey) if t.ctx != nil { switch v := t.ctx.(type) { case *ObjectSessionContext: - proto.NestedStructureMarshal(sessionTokenBodyObjectCtxField, buf[offset:], v) + buf = proto.NestedStructureMarshal(sessionTokenBodyObjectCtxField, buf, v) case *ContainerSessionContext: - proto.NestedStructureMarshal(sessionTokenBodyCnrCtxField, buf[offset:], v) + buf = proto.NestedStructureMarshal(sessionTokenBodyCnrCtxField, buf, v) default: panic("cannot marshal unknown session token context") } @@ -352,13 +340,11 @@ func (t *Token) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, t.StableSize()) + buf = make([]byte, 0, t.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(sessionTokenBodyField, buf[offset:], t.body) - proto.NestedStructureMarshal(sessionTokenSignatureField, buf[offset:], t.sig) + buf = proto.NestedStructureMarshal(sessionTokenBodyField, buf, t.body) + buf = proto.NestedStructureMarshal(sessionTokenSignatureField, buf, t.sig) return buf } @@ -389,23 +375,21 @@ func (r *RequestMetaHeader) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(reqMetaHeaderVersionField, buf[offset:], r.version) - offset += proto.UInt64Marshal(reqMetaHeaderEpochField, buf[offset:], r.epoch) - offset += proto.UInt32Marshal(reqMetaHeaderTTLField, buf[offset:], r.ttl) + buf = proto.NestedStructureMarshal(reqMetaHeaderVersionField, buf, r.version) + buf = proto.UInt64Marshal(reqMetaHeaderEpochField, buf, r.epoch) + buf = proto.UInt32Marshal(reqMetaHeaderTTLField, buf, r.ttl) for i := range r.xHeaders { - offset += proto.NestedStructureMarshal(reqMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) + buf = proto.NestedStructureMarshal(reqMetaHeaderXHeadersField, buf, &r.xHeaders[i]) } - offset += proto.NestedStructureMarshal(reqMetaHeaderSessionTokenField, buf[offset:], r.sessionToken) - offset += proto.NestedStructureMarshal(reqMetaHeaderBearerTokenField, buf[offset:], r.bearerToken) - offset += proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf[offset:], r.origin) - proto.UInt64Marshal(reqMetaHeaderNetMagicField, buf[offset:], r.netMagic) + buf = proto.NestedStructureMarshal(reqMetaHeaderSessionTokenField, buf, r.sessionToken) + buf = proto.NestedStructureMarshal(reqMetaHeaderBearerTokenField, buf, r.bearerToken) + buf = proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf, r.origin) + buf = proto.UInt64Marshal(reqMetaHeaderNetMagicField, buf, r.netMagic) return buf } @@ -449,15 +433,13 @@ func (r *RequestVerificationHeader) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(reqVerifHeaderBodySignatureField, buf[offset:], r.bodySig) - offset += proto.NestedStructureMarshal(reqVerifHeaderMetaSignatureField, buf[offset:], r.metaSig) - offset += proto.NestedStructureMarshal(reqVerifHeaderOriginSignatureField, buf[offset:], r.originSig) - proto.NestedStructureMarshal(reqVerifHeaderOriginField, buf[offset:], r.origin) + buf = proto.NestedStructureMarshal(reqVerifHeaderBodySignatureField, buf, r.bodySig) + buf = proto.NestedStructureMarshal(reqVerifHeaderMetaSignatureField, buf, r.metaSig) + buf = proto.NestedStructureMarshal(reqVerifHeaderOriginSignatureField, buf, r.originSig) + buf = proto.NestedStructureMarshal(reqVerifHeaderOriginField, buf, r.origin) return buf } @@ -490,21 +472,19 @@ func (r *ResponseMetaHeader) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(respMetaHeaderVersionField, buf[offset:], r.version) - offset += proto.UInt64Marshal(respMetaHeaderEpochField, buf[offset:], r.epoch) - offset += proto.UInt32Marshal(respMetaHeaderTTLField, buf[offset:], r.ttl) + buf = proto.NestedStructureMarshal(respMetaHeaderVersionField, buf, r.version) + buf = proto.UInt64Marshal(respMetaHeaderEpochField, buf, r.epoch) + buf = proto.UInt32Marshal(respMetaHeaderTTLField, buf, r.ttl) for i := range r.xHeaders { - offset += proto.NestedStructureMarshal(respMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) + buf = proto.NestedStructureMarshal(respMetaHeaderXHeadersField, buf, &r.xHeaders[i]) } - offset += proto.NestedStructureMarshal(respMetaHeaderOriginField, buf[offset:], r.origin) - proto.NestedStructureMarshal(respMetaHeaderStatusField, buf[offset:], r.status) + buf = proto.NestedStructureMarshal(respMetaHeaderOriginField, buf, r.origin) + buf = proto.NestedStructureMarshal(respMetaHeaderStatusField, buf, r.status) return buf } @@ -546,15 +526,13 @@ func (r *ResponseVerificationHeader) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, r.StableSize()) + buf = make([]byte, 0, r.StableSize()) } - var offset int - - offset += proto.NestedStructureMarshal(respVerifHeaderBodySignatureField, buf[offset:], r.bodySig) - offset += proto.NestedStructureMarshal(respVerifHeaderMetaSignatureField, buf[offset:], r.metaSig) - offset += proto.NestedStructureMarshal(respVerifHeaderOriginSignatureField, buf[offset:], r.originSig) - proto.NestedStructureMarshal(respVerifHeaderOriginField, buf[offset:], r.origin) + buf = proto.NestedStructureMarshal(respVerifHeaderBodySignatureField, buf, r.bodySig) + buf = proto.NestedStructureMarshal(respVerifHeaderMetaSignatureField, buf, r.metaSig) + buf = proto.NestedStructureMarshal(respVerifHeaderOriginSignatureField, buf, r.originSig) + buf = proto.NestedStructureMarshal(respVerifHeaderOriginField, buf, r.origin) return buf } diff --git a/session/types.go b/session/types.go index 19cfecc..c54e905 100644 --- a/session/types.go +++ b/session/types.go @@ -55,13 +55,13 @@ const ( func (x objectSessionContextTarget) StableMarshal(buf []byte) []byte { if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } - offset := proto.NestedStructureMarshal(fNumObjectTargetContainer, buf, x.cnr) + buf = proto.NestedStructureMarshal(fNumObjectTargetContainer, buf, x.cnr) for i := range x.objs { - offset += proto.NestedStructureMarshal(fNumObjectTargetObjects, buf[offset:], &x.objs[i]) + buf = proto.NestedStructureMarshal(fNumObjectTargetObjects, buf, &x.objs[i]) } return buf diff --git a/status/marshal.go b/status/marshal.go index 2908e0d..cbefb17 100644 --- a/status/marshal.go +++ b/status/marshal.go @@ -18,13 +18,13 @@ func (x *Detail) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } var offset int - offset += protoutil.UInt32Marshal(detailIDFNum, buf[offset:], x.id) - protoutil.BytesMarshal(detailValueFNum, buf[offset:], x.val) + buf = protoutil.UInt32Marshal(detailIDFNum, buf[offset:], x.id) + buf = protoutil.BytesMarshal(detailValueFNum, buf[offset:], x.val) return buf } @@ -57,16 +57,16 @@ func (x *Status) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, x.StableSize()) + buf = make([]byte, 0, x.StableSize()) } var offset int - offset += protoutil.UInt32Marshal(statusCodeFNum, buf[offset:], CodeToGRPC(x.code)) - offset += protoutil.StringMarshal(statusMsgFNum, buf[offset:], x.msg) + buf = protoutil.UInt32Marshal(statusCodeFNum, buf[offset:], CodeToGRPC(x.code)) + buf = protoutil.StringMarshal(statusMsgFNum, buf[offset:], x.msg) for i := range x.details { - offset += protoutil.NestedStructureMarshal(statusDetailsFNum, buf[offset:], &x.details[i]) + buf = protoutil.NestedStructureMarshal(statusDetailsFNum, buf[offset:], &x.details[i]) } return buf diff --git a/tombstone/marshal.go b/tombstone/marshal.go index ae4405c..3bc06e6 100644 --- a/tombstone/marshal.go +++ b/tombstone/marshal.go @@ -20,16 +20,16 @@ func (s *Tombstone) StableMarshal(buf []byte) []byte { } if buf == nil { - buf = make([]byte, s.StableSize()) + buf = make([]byte, 0, s.StableSize()) } var offset int - offset += proto.UInt64Marshal(expFNum, buf[offset:], s.exp) - offset += proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID) + buf = proto.UInt64Marshal(expFNum, buf[offset:], s.exp) + buf = proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID) for i := range s.members { - offset += proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i]) + buf = proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i]) } return buf diff --git a/util/proto/marshal.go b/util/proto/marshal.go index 606086a..f70c57b 100644 --- a/util/proto/marshal.go +++ b/util/proto/marshal.go @@ -7,7 +7,6 @@ runtime function calls. package proto import ( - "encoding/binary" "math" "math/bits" @@ -21,7 +20,7 @@ type ( } ) -func BytesMarshal(field int, buf, v []byte) int { +func BytesMarshal(field int, buf, v []byte) []byte { return bytesMarshal(field, buf, v) } @@ -29,23 +28,30 @@ func BytesSize(field int, v []byte) int { return bytesSize(field, v) } -func bytesMarshal[T ~[]byte | ~string](field int, buf []byte, v T) int { +func bytesMarshal(field int, buf []byte, v []byte) []byte { if len(v) == 0 { - return 0 + return buf } return bytesMarshalNoCheck(field, buf, v) } -func bytesMarshalNoCheck[T ~[]byte | ~string](field int, buf []byte, v T) int { - prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType) +func stringMarshal(field int, buf []byte, v string) []byte { + if len(v) == 0 { + return buf + } + return stringMarshalNoCheck(field, buf, v) +} - // buf length check can prevent panic at PutUvarint, but it will make - // marshaller a bit slower. - i := binary.PutUvarint(buf, uint64(prefix)) - i += binary.PutUvarint(buf[i:], uint64(len(v))) - i += copy(buf[i:], v) +func stringMarshalNoCheck(field int, buf []byte, v string) []byte { + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.BytesType) + buf = protowire.AppendString(buf, v) + return buf +} - return i +func bytesMarshalNoCheck(field int, buf []byte, v []byte) []byte { + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.BytesType) + buf = protowire.AppendBytes(buf, v) + return buf } func bytesSize[T ~[]byte | ~string](field int, v T) int { @@ -59,28 +65,25 @@ func bytesSizeNoCheck[T ~[]byte | ~string](field int, v T) int { return protowire.SizeGroup(protowire.Number(field), protowire.SizeBytes(len(v))) } -func StringMarshal(field int, buf []byte, v string) int { - return bytesMarshal(field, buf, v) +func StringMarshal(field int, buf []byte, v string) []byte { + return stringMarshal(field, buf, v) } func StringSize(field int, v string) int { return bytesSize(field, v) } -func BoolMarshal(field int, buf []byte, v bool) int { +func BoolMarshal(field int, buf []byte, v bool) []byte { if !v { - return 0 + return buf } - prefix := protowire.EncodeTag(protowire.Number(field), protowire.VarintType) - - // buf length check can prevent panic at PutUvarint, but it will make - // marshaller a bit slower. - i := binary.PutUvarint(buf, uint64(prefix)) const boolTrueValue = 0x1 - buf[i] = boolTrueValue - return i + 1 + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.VarintType) + buf = protowire.AppendVarint(buf, boolTrueValue) + + return buf } func BoolSize(field int, v bool) int { @@ -91,19 +94,15 @@ func BoolSize(field int, v bool) int { return protowire.SizeGroup(protowire.Number(field), boolLength) } -func UInt64Marshal(field int, buf []byte, v uint64) int { +func UInt64Marshal(field int, buf []byte, v uint64) []byte { if v == 0 { - return 0 + return buf } - prefix := protowire.EncodeTag(protowire.Number(field), protowire.VarintType) + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.VarintType) + buf = protowire.AppendVarint(buf, v) - // buf length check can prevent panic at PutUvarint, but it will make - // marshaller a bit slower. - i := binary.PutUvarint(buf, uint64(prefix)) - i += binary.PutUvarint(buf[i:], v) - - return i + return buf } func UInt64Size(field int, v uint64) int { @@ -113,7 +112,7 @@ func UInt64Size(field int, v uint64) int { return protowire.SizeGroup(protowire.Number(field), protowire.SizeVarint(v)) } -func Int64Marshal(field int, buf []byte, v int64) int { +func Int64Marshal(field int, buf []byte, v int64) []byte { return UInt64Marshal(field, buf, uint64(v)) } @@ -121,7 +120,7 @@ func Int64Size(field int, v int64) int { return UInt64Size(field, uint64(v)) } -func UInt32Marshal(field int, buf []byte, v uint32) int { +func UInt32Marshal(field int, buf []byte, v uint32) []byte { return UInt64Marshal(field, buf, uint64(v)) } @@ -129,7 +128,7 @@ func UInt32Size(field int, v uint32) int { return UInt64Size(field, uint64(v)) } -func Int32Marshal(field int, buf []byte, v int32) int { +func Int32Marshal(field int, buf []byte, v int32) []byte { return UInt64Marshal(field, buf, uint64(v)) } @@ -137,7 +136,7 @@ func Int32Size(field int, v int32) int { return UInt64Size(field, uint64(v)) } -func EnumMarshal(field int, buf []byte, v int32) int { +func EnumMarshal(field int, buf []byte, v int32) []byte { return UInt64Marshal(field, buf, uint64(v)) } @@ -145,14 +144,12 @@ func EnumSize(field int, v int32) int { return UInt64Size(field, uint64(v)) } -func RepeatedBytesMarshal(field int, buf []byte, v [][]byte) int { - var offset int - +func RepeatedBytesMarshal(field int, buf []byte, v [][]byte) []byte { for i := range v { - offset += bytesMarshalNoCheck(field, buf[offset:], v[i]) + buf = bytesMarshalNoCheck(field, buf, v[i]) } - return offset + return buf } func RepeatedBytesSize(field int, v [][]byte) (size int) { @@ -163,14 +160,14 @@ func RepeatedBytesSize(field int, v [][]byte) (size int) { return size } -func RepeatedStringMarshal(field int, buf []byte, v []string) int { +func RepeatedStringMarshal(field int, buf []byte, v []string) []byte { var offset int for i := range v { - offset += bytesMarshalNoCheck(field, buf[offset:], v[i]) + buf = stringMarshalNoCheck(field, buf[offset:], v[i]) } - return offset + return buf } func RepeatedStringSize(field int, v []string) (size int) { @@ -195,24 +192,23 @@ func repeatedUIntSize[T ~uint64 | ~int64 | ~uint32 | ~int32](field int, v []T) ( return } -func repeatedUIntMarshal[T ~uint64 | ~int64 | ~uint32 | ~int32](field int, buf []byte, v []T) int { +func repeatedUIntMarshal[T ~uint64 | ~int64 | ~uint32 | ~int32](field int, buf []byte, v []T) []byte { if len(v) == 0 { - return 0 + return buf } - prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType) - offset := binary.PutUvarint(buf, uint64(prefix)) + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.BytesType) _, arrSize := repeatedUIntSize(field, v) - offset += binary.PutUvarint(buf[offset:], uint64(arrSize)) + buf = protowire.AppendVarint(buf, uint64(arrSize)) for i := range v { - offset += binary.PutUvarint(buf[offset:], uint64(v[i])) + buf = protowire.AppendVarint(buf, uint64(v[i])) } - return offset + return buf } -func RepeatedUInt64Marshal(field int, buf []byte, v []uint64) int { +func RepeatedUInt64Marshal(field int, buf []byte, v []uint64) []byte { return repeatedUIntMarshal(field, buf, v) } @@ -220,7 +216,7 @@ func RepeatedUInt64Size(field int, v []uint64) (size, arraySize int) { return repeatedUIntSize(field, v) } -func RepeatedInt64Marshal(field int, buf []byte, v []int64) int { +func RepeatedInt64Marshal(field int, buf []byte, v []int64) []byte { return repeatedUIntMarshal(field, buf, v) } @@ -228,7 +224,7 @@ func RepeatedInt64Size(field int, v []int64) (size, arraySize int) { return repeatedUIntSize(field, v) } -func RepeatedUInt32Marshal(field int, buf []byte, v []uint32) int { +func RepeatedUInt32Marshal(field int, buf []byte, v []uint32) []byte { return repeatedUIntMarshal(field, buf, v) } @@ -236,7 +232,7 @@ func RepeatedUInt32Size(field int, v []uint32) (size, arraySize int) { return repeatedUIntSize(field, v) } -func RepeatedInt32Marshal(field int, buf []byte, v []int32) int { +func RepeatedInt32Marshal(field int, buf []byte, v []int32) []byte { return repeatedUIntMarshal(field, buf, v) } @@ -249,18 +245,17 @@ func VarUIntSize(x uint64) int { return (bits.Len64(x|1) + 6) / 7 } -func NestedStructureMarshal[T stableMarshaller](field int64, buf []byte, v T) int { +func NestedStructureMarshal[T stableMarshaller](field int64, buf []byte, v T) []byte { n := v.StableSize() if n == 0 { - return 0 + return buf } - prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType) - offset := binary.PutUvarint(buf, prefix) - offset += binary.PutUvarint(buf[offset:], uint64(n)) - v.StableMarshal(buf[offset:]) + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.BytesType) + buf = protowire.AppendVarint(buf, uint64(n)) + buf = v.StableMarshal(buf) - return offset + n + return buf } func NestedStructureSize[T stableMarshaller](field int64, v T) (size int) { @@ -272,19 +267,15 @@ func NestedStructureSize[T stableMarshaller](field int64, v T) (size int) { return } -func Fixed64Marshal(field int, buf []byte, v uint64) int { +func Fixed64Marshal(field int, buf []byte, v uint64) []byte { if v == 0 { - return 0 + return buf } - prefix := protowire.EncodeTag(protowire.Number(field), protowire.Fixed64Type) + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.Fixed64Type) + buf = protowire.AppendFixed64(buf, v) - // buf length check can prevent panic at PutUvarint, but it will make - // marshaller a bit slower. - i := binary.PutUvarint(buf, uint64(prefix)) - binary.LittleEndian.PutUint64(buf[i:], v) - - return i + 8 + return buf } func Fixed64Size(fNum int, v uint64) int { @@ -294,17 +285,15 @@ func Fixed64Size(fNum int, v uint64) int { return protowire.SizeGroup(protowire.Number(fNum), protowire.SizeFixed64()) } -func Float64Marshal(field int, buf []byte, v float64) int { +func Float64Marshal(field int, buf []byte, v float64) []byte { if v == 0 { - return 0 + return buf } - prefix := protowire.EncodeTag(protowire.Number(field), protowire.Fixed64Type) + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.Fixed64Type) + buf = protowire.AppendFixed64(buf, math.Float64bits(v)) - i := binary.PutUvarint(buf, uint64(prefix)) - binary.LittleEndian.PutUint64(buf[i:], math.Float64bits(v)) - - return i + 8 + return buf } func Float64Size(fNum int, v float64) int { @@ -318,19 +307,15 @@ func Float64Size(fNum int, v float64) int { // and writes it to specified buffer. Returns number of bytes written. // // Panics if the buffer is undersized. -func Fixed32Marshal(field int, buf []byte, v uint32) int { +func Fixed32Marshal(field int, buf []byte, v uint32) []byte { if v == 0 { - return 0 + return buf } - prefix := protowire.EncodeTag(protowire.Number(field), protowire.Fixed32Type) + buf = protowire.AppendTag(buf, protowire.Number(field), protowire.Fixed32Type) + buf = protowire.AppendFixed32(buf, v) - // buf length check can prevent panic at PutUvarint, but it will make - // marshaller a bit slower. - i := binary.PutUvarint(buf, uint64(prefix)) - binary.LittleEndian.PutUint32(buf[i:], v) - - return i + 4 + return buf } // Fixed32Size returns number of bytes required to encode uint32 value to Protocol Buffers fixed32 field diff --git a/util/proto/marshal_test.go b/util/proto/marshal_test.go index 11a4d8f..3b57032 100644 --- a/util/proto/marshal_test.go +++ b/util/proto/marshal_test.go @@ -47,7 +47,7 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e } if buf == nil { - buf = make([]byte, s.stableSize()) + buf = make([]byte, 0, s.stableSize()) } var ( @@ -58,62 +58,62 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e if wrongField { fieldNum++ } - i += proto.BytesMarshal(fieldNum, buf, s.FieldA) + buf = proto.BytesMarshal(fieldNum, buf, s.FieldA) fieldNum = 2 if wrongField { fieldNum++ } - i += proto.StringMarshal(fieldNum, buf, s.FieldB) + buf = proto.StringMarshal(fieldNum, buf, s.FieldB) fieldNum = 200 if wrongField { fieldNum++ } - i += proto.BoolMarshal(fieldNum, buf, s.FieldC) + buf = proto.BoolMarshal(fieldNum, buf, s.FieldC) fieldNum = 201 if wrongField { fieldNum++ } - i += proto.Int32Marshal(fieldNum, buf, s.FieldD) + buf = proto.Int32Marshal(fieldNum, buf, s.FieldD) fieldNum = 202 if wrongField { fieldNum++ } - i += proto.UInt32Marshal(fieldNum, buf, s.FieldE) + buf = proto.UInt32Marshal(fieldNum, buf, s.FieldE) fieldNum = 203 if wrongField { fieldNum++ } - i += proto.Int64Marshal(fieldNum, buf, s.FieldF) + buf = proto.Int64Marshal(fieldNum, buf, s.FieldF) fieldNum = 204 if wrongField { fieldNum++ } - i += proto.UInt64Marshal(fieldNum, buf, s.FieldG) + buf = proto.UInt64Marshal(fieldNum, buf, s.FieldG) fieldNum = 205 if wrongField { fieldNum++ } - i += proto.Fixed64Marshal(fieldNum, buf, s.FieldI) + buf = proto.Fixed64Marshal(fieldNum, buf, s.FieldI) fieldNum = 206 if wrongField { fieldNum++ } - i += proto.Float64Marshal(fieldNum, buf, s.FieldJ) + buf = proto.Float64Marshal(fieldNum, buf, s.FieldJ) fieldNum = 207 if wrongField { fieldNum++ } - offset = proto.Fixed32Marshal(fieldNum, buf, s.FieldK) + buf = proto.Fixed32Marshal(fieldNum, buf, s.FieldK) i += offset @@ -121,7 +121,7 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e if wrongField { fieldNum++ } - i += proto.EnumMarshal(fieldNum, buf, int32(s.FieldH)) + buf = proto.EnumMarshal(fieldNum, buf, int32(s.FieldH)) return buf, nil } @@ -146,46 +146,46 @@ func (s *stableRepPrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte } if buf == nil { - buf = make([]byte, s.stableSize()) + buf = make([]byte, 0, s.stableSize()) } - var i, fieldNum int + var fieldNum int fieldNum = 1 if wrongField { fieldNum++ } - i += proto.RepeatedBytesMarshal(fieldNum, buf, s.FieldA) + buf = proto.RepeatedBytesMarshal(fieldNum, buf, s.FieldA) fieldNum = 2 if wrongField { fieldNum++ } - i += proto.RepeatedStringMarshal(fieldNum, buf, s.FieldB) + buf = proto.RepeatedStringMarshal(fieldNum, buf, s.FieldB) fieldNum = 3 if wrongField { fieldNum++ } - i += proto.RepeatedInt32Marshal(fieldNum, buf, s.FieldC) + buf = proto.RepeatedInt32Marshal(fieldNum, buf, s.FieldC) fieldNum = 4 if wrongField { fieldNum++ } - i += proto.RepeatedUInt32Marshal(fieldNum, buf, s.FieldD) + buf = proto.RepeatedUInt32Marshal(fieldNum, buf, s.FieldD) fieldNum = 5 if wrongField { fieldNum++ } - i += proto.RepeatedInt64Marshal(fieldNum, buf, s.FieldE) + buf = proto.RepeatedInt64Marshal(fieldNum, buf, s.FieldE) fieldNum = 6 if wrongField { fieldNum++ } - i += proto.RepeatedUInt64Marshal(fieldNum, buf, s.FieldF) + buf = proto.RepeatedUInt64Marshal(fieldNum, buf, s.FieldF) return buf, nil }