]> git.bitcoin.ninja Git - rust-lightning/commit
Impl `serialized_length()` without `LengthCalculatingWriter`
authorMatt Corallo <git@bluematt.me>
Sat, 29 May 2021 18:32:53 +0000 (18:32 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 29 May 2021 23:03:05 +0000 (23:03 +0000)
commit5b6137c36d89603823b94af23919c5a968c4400b
treeae83d8bd91bfe0e3f6835e78b1c59e43e51e0067
parenta27a2223f65ab4570c37e609ac2884ee6d001403
Impl `serialized_length()` without `LengthCalculatingWriter`

With the new `serialized_length()` method potentially being
significantly more efficient than `LengthCalculatingWriter`, this
commit ensures we call `serialized_length()` when calculating
length of a larger struct.

Specifically, prior to this commit a call to
`serialized_length()` on a large object serialized with
`impl_writeable`, `impl_writeable_len_match`, or
`encode_varint_length_prefixed_tlv` (and
`impl_writeable_tlv_based`) would always serialize all inner fields
of that object using `LengthCalculatingWriter`. This would ignore
any `serialized_length()` overrides by inner fields. Instead, we
override `serialized_length()` on all of the above by calculating
the serialized size using calls to `serialized_length()` on inner
fields.

Further, writes to `LengthCalculatingWriter` should never fail as
its `write` method never returns an error. Thus, any write failures
indicate a bug in an object's write method or in our
object-creation sanity checking. We `.expect()` such write calls
here.

As of this commit, on an Intel 2687W v3, the serialization
benchmarks take:

test routing::network_graph::benches::read_network_graph  ... bench: 2,039,451,296 ns/iter (+/- 4,329,821)
test routing::network_graph::benches::write_network_graph ... bench: 166,685,412 ns/iter (+/- 352,537)
lightning/src/util/ser_macros.rs