[#58] object: Allow to set marshal data

Now it is possible set marshaled data to reduce memory
allocations.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-10-06 15:05:30 +03:00
parent 309aa4ac78
commit f50872f1bc
5 changed files with 94 additions and 4 deletions

View file

@ -322,6 +322,14 @@ func (o *Object) StableMarshal(buf []byte) []byte {
return []byte{}
}
if o.marshalData != nil {
if buf == nil {
return o.marshalData
}
copy(buf, o.marshalData)
return buf
}
if buf == nil {
buf = make([]byte, o.StableSize())
}
@ -336,6 +344,20 @@ func (o *Object) StableMarshal(buf []byte) []byte {
return buf
}
// SetMarshalData sets marshal data to reduce memory allocations.
//
// It is unsafe to modify object data after setting marshal data.
func (o *Object) SetMarshalData(data []byte) {
if o == nil {
return
}
if data == nil {
o.marshalData = o.StableMarshal(nil)
} else {
o.marshalData = data
}
}
func (o *Object) StableSize() (size int) {
if o == nil {
return 0
@ -1071,6 +1093,15 @@ func (r *PutSingleRequestBody) StableMarshal(buf []byte) []byte {
if r == nil {
return []byte{}
}
if r.marshalData != nil {
if buf == nil {
return r.marshalData
}
copy(buf, r.marshalData)
return buf
}
if buf == nil {
buf = make([]byte, r.StableSize())
}
@ -1082,6 +1113,23 @@ func (r *PutSingleRequestBody) StableMarshal(buf []byte) []byte {
return buf
}
// SetMarshalData sets marshal data to reduce memory allocations.
//
// It is unsafe to modify request data after setting marshal data.
func (r *PutSingleRequestBody) SetMarshalData(data []byte) {
if r == nil {
return
}
if data == nil {
r.marshalData = r.StableMarshal(nil)
} else {
r.marshalData = data
}
proto.NestedStructureSetMarshalData(putSingleReqObjectField, r.marshalData, r.object)
}
func (r *PutSingleRequestBody) StableSize() int {
if r == nil {
return 0