cosmos-sdk/codec
Emmanuel T Odeke cbb68fc6ef
codec: remove unnecessary allocations in ProtoCodec.MarshalBinaryLengthPrefixed (#6877)
* codec: remove unnecessary allocations in ProtoCodec.MarshalBinaryLengthPrefixed

Improve ProtoCodec.MarshalBinaryLengthPrefixed by removing the need
to use a *bytes.Buffer and instead use an array and invoke
binary.PutUvarint, and directly create the respective length prefixed
concatentation.

With this change we get the following improvement in all dimenions
of throughput, bytes allocated, number of allocations per operation.

```shell
$ benchstat before.txt after.txt
name                                     old time/op    new time/op    delta
ProtoCodecMarshalBinaryLengthPrefixed-8     295ns ± 2%     177ns ± 3%  -39.92%  (p=0.000 n=20+20)

name                                     old speed      new speed      delta
ProtoCodecMarshalBinaryLengthPrefixed-8   505MB/s ± 2%   841MB/s ± 3%  +66.44%  (p=0.000 n=20+20)

name                                     old alloc/op   new alloc/op   delta
ProtoCodecMarshalBinaryLengthPrefixed-8      576B ± 0%      336B ± 0%  -41.67%  (p=0.000 n=20+20)

name                                     old allocs/op  new allocs/op  delta
ProtoCodecMarshalBinaryLengthPrefixed-8      5.00 ± 0%      3.00 ± 0%  -40.00%  (p=0.000 n=20+20)
```

Fixes #6875

* Address @marbar3778's feedback

Use binary.MaxVarintLen64 instead of 10

Co-authored-by: Marko <marbar3778@yahoo.com>

Co-authored-by: Marko <marbar3778@yahoo.com>
Co-authored-by: Aaron Craelius <aaron@regen.network>
2020-07-29 14:39:34 +00:00
..
legacy Simapp Localnet (#6421) 2020-06-12 16:23:08 +00:00
types x/auth/ante: Migrate tests to use the new client.TxConfig (#6661) 2020-07-20 08:30:12 -04:00
amino_codec_test.go x/auth/ante: Migrate tests to use the new client.TxConfig (#6661) 2020-07-20 08:30:12 -04:00
amino_codec.go Update x/gov to use Any (#6147) 2020-05-19 20:17:29 +00:00
amino.go codec: add as much visible coverage (#6670) 2020-07-10 13:01:25 +02:00
any_test.go x/auth/ante: Migrate tests to use the new client.TxConfig (#6661) 2020-07-20 08:30:12 -04:00
any.go Start removing HybridCodec (init + auth, bank, distribution) (#6838) 2020-07-24 19:04:29 +00:00
codec.go codec: remove unnecessary allocations in ProtoCodec.MarshalBinaryLengthPrefixed (#6877) 2020-07-29 14:39:34 +00:00
hybrid_codec_test.go x/auth/ante: Migrate tests to use the new client.TxConfig (#6661) 2020-07-20 08:30:12 -04:00
hybrid_codec.go Merge PR #6076: Proto Any init + evidence 2020-05-05 10:28:20 -04:00
json.go Disable automatic camelCasing in proto JSON (#6833) 2020-07-23 22:16:44 +00:00
proto_codec_test.go codec: remove unnecessary allocations in ProtoCodec.MarshalBinaryLengthPrefixed (#6877) 2020-07-29 14:39:34 +00:00
proto_codec.go codec: remove unnecessary allocations in ProtoCodec.MarshalBinaryLengthPrefixed (#6877) 2020-07-29 14:39:34 +00:00