Skip to content

Commit

Permalink
don't reset before put, optimize repeated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Feb 6, 2025
1 parent b0fbdad commit 3104eef
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 72 deletions.
31 changes: 6 additions & 25 deletions tm2/pkg/amino/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,7 @@ func (cdc *Codec) MarshalSized(o interface{}) ([]byte, error) {

// Write the bytes here.
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

// Write the bz without length-prefixing.
bz, err := cdc.Marshal(o)
Expand Down Expand Up @@ -276,11 +273,7 @@ func (cdc *Codec) MarshalAnySized(o interface{}) ([]byte, error) {

// Write the bytes here.
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()

defer poolBytesBuffer.Put(buf)
// Write the bz without length-prefixing.
bz, err := cdc.MarshalAny(o)
if err != nil {
Expand Down Expand Up @@ -366,10 +359,7 @@ func (cdc *Codec) MarshalReflect(o interface{}) ([]byte, error) {
// Encode Amino:binary bytes.
var bz []byte
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

rt := rv.Type()
info, err := cdc.getTypeInfoWLock(rt)
Expand Down Expand Up @@ -457,10 +447,7 @@ func (cdc *Codec) MarshalAny(o interface{}) ([]byte, error) {

// Encode as interface.
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)
err = cdc.encodeReflectBinaryInterface(buf, iinfo, reflect.ValueOf(&ivar).Elem(), FieldOptions{}, true)
if err != nil {
return nil, err
Expand Down Expand Up @@ -788,10 +775,7 @@ func (cdc *Codec) JSONMarshal(o interface{}) ([]byte, error) {
}
rt := rv.Type()
w := poolBytesBuffer.Get()
defer func() {
w.Reset()
poolBytesBuffer.Put(w)
}()
defer poolBytesBuffer.Put(w)
info, err := cdc.getTypeInfoWLock(rt)
if err != nil {
return nil, err
Expand Down Expand Up @@ -831,10 +815,7 @@ func (cdc *Codec) MarshalJSONAny(o interface{}) ([]byte, error) {

// Encode as interface.
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

err = cdc.encodeReflectJSONInterface(buf, iinfo, reflect.ValueOf(&ivar).Elem(), FieldOptions{})
if err != nil {
Expand Down
47 changes: 12 additions & 35 deletions tm2/pkg/amino/binary_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ func (cdc *Codec) encodeReflectBinaryInterface(w io.Writer, iinfo *TypeInfo, rv
// For Proto3 compatibility, encode interfaces as google.protobuf.Any
// Write field #1, TypeURL
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

{
fnum := uint32(1)
Expand All @@ -278,10 +275,7 @@ func (cdc *Codec) encodeReflectBinaryInterface(w io.Writer, iinfo *TypeInfo, rv
// google.protobuf.Any values must be a struct, or an unpacked list which
// is indistinguishable from a struct.
buf2 := poolBytesBuffer.Get()
defer func() {
buf2.Reset()
poolBytesBuffer.Put(buf2)
}()
defer poolBytesBuffer.Put(buf2)

if !cinfo.IsStructOrUnpacked(fopts) {
writeEmpty := false
Expand Down Expand Up @@ -370,10 +364,7 @@ func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflec
// Proto3 byte-length prefixing incurs alloc cost on the encoder.
// Here we incur it for unpacked form for ease of dev.
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

// If elem is not already a ByteLength type, write in packed form.
// This is a Proto wart due to Proto backwards compatibility issues.
Expand Down Expand Up @@ -410,6 +401,9 @@ func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflec
einfo.Elem.ReprType.Type.Kind() != reflect.Uint8 &&
einfo.Elem.ReprType.GetTyp3(fopts) != Typ3ByteLength

elemBuf := poolBytesBuffer.Get()
defer poolBytesBuffer.Put(elemBuf)

// Write elems in unpacked form.
for i := 0; i < rv.Len(); i++ {
// Write elements as repeated fields of the parent struct.
Expand Down Expand Up @@ -448,28 +442,21 @@ func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflec
// form) are represented as lists of implicit structs.
if writeImplicit {
// Write field key for Value field of implicit struct.
buf2 := poolBytesBuffer.Get()
buf2Done := func() {
buf2.Reset()
poolBytesBuffer.Put(buf2)
}

err = encodeFieldNumberAndTyp3(buf2, 1, Typ3ByteLength)
err = encodeFieldNumberAndTyp3(elemBuf, 1, Typ3ByteLength)
if err != nil {
buf2Done()
return
}
// Write field value of implicit struct to buf2.
efopts := fopts
efopts.BinFieldNum = 0 // dontcare
err = cdc.encodeReflectBinary(buf2, einfo, derv, efopts, false, 0)
err = cdc.encodeReflectBinary(elemBuf, einfo, derv, efopts, false, 0)
if err != nil {
buf2Done()
return
}
// Write implicit struct to buf.
err = EncodeByteSlice(buf, buf2.Bytes())
buf2Done()
err = EncodeByteSlice(buf, elemBuf.Bytes())
elemBuf.Reset()
if err != nil {
return
}
Expand Down Expand Up @@ -523,10 +510,7 @@ func (cdc *Codec) encodeReflectBinaryStruct(w io.Writer, info *TypeInfo, rv refl
// Proto3 incurs a cost in writing non-root structs.
// Here we incur it for root structs as well for ease of dev.
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

for _, field := range info.Fields {
// Get type info for field.
Expand Down Expand Up @@ -581,15 +565,8 @@ func encodeFieldNumberAndTyp3(w io.Writer, num uint32, typ Typ3) (err error) {
return
}

type bufLike interface {
io.Writer
Len() int
Bytes() []byte
Set([]byte)
}

func (cdc *Codec) writeFieldIfNotEmpty(
buf bufLike,
buf *bytebufferpool.ByteBuffer,
fieldNum uint32,
finfo *TypeInfo,
structsFopts FieldOptions, // the wrapping struct's FieldOptions if any
Expand Down
5 changes: 1 addition & 4 deletions tm2/pkg/amino/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ func (info *TypeInfo) String() string {
return "<new TypeInfo>"
}
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

Check warning on line 117 in tm2/pkg/amino/codec.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/amino/codec.go#L115-L117

Added lines #L115 - L117 were not covered by tests
buf.Write([]byte("TypeInfo{"))
buf.Write([]byte(fmt.Sprintf("Type:%v,", info.Type)))
Expand Down
5 changes: 1 addition & 4 deletions tm2/pkg/amino/json_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ func (cdc *Codec) encodeReflectJSONInterface(w io.Writer, iinfo *TypeInfo, rv re

// Write Value to buffer
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

cdc.encodeReflectJSON(buf, cinfo, crv, fopts)
value := buf.Bytes()
Expand Down
5 changes: 1 addition & 4 deletions tm2/pkg/amino/wellknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,7 @@ func encodeReflectBinaryWellKnown(w io.Writer, info *TypeInfo, rv reflect.Value,
// Maybe recurse with length-prefixing.
if !bare {
buf := poolBytesBuffer.Get()
defer func() {
buf.Reset()
poolBytesBuffer.Put(buf)
}()
defer poolBytesBuffer.Put(buf)

ok, err = encodeReflectBinaryWellKnown(buf, info, rv, fopts, true)
if err != nil {
Expand Down

0 comments on commit 3104eef

Please sign in to comment.