Use u8 for TLV type when writing and reading our own structs
authorMatt Corallo <git@bluematt.me>
Fri, 28 May 2021 18:43:43 +0000 (18:43 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 30 May 2021 00:32:31 +0000 (00:32 +0000)
commit17204a0d7f2e2a38aa547711462459aa8e3dd5ac
tree5b3108dc6dd4eb44532a7ce4c71bee5579a8510d
parentbaeef9ce8bb5b0f7bdcf3ad2f60b30eb20b8a127
Use u8 for TLV type when writing and reading our own structs

Once inlined, a very nontrivial portion of the total code generated
for reading TLVs is BigSize deserialization. In some naive testing,
as much as 50% of total code size is dedicated to the combination
of type and length deserialization.

While we're stuck with BigSize deserialization for network messages
there is nothing requiring we use BigSizes for our own messages,
and specifically for types, unless we have > 127
required/non-required types the serialization format remains the
same.

Thus, we adapt our TLV macros to support either BigSize or
U8Wrapper (ie u8 with a wrapper to make it API-compatible with
BigSize) for types depending on the context.

This results in somewhat less code, though a dissapointingly small
performance improvement. Future work, however, can further compact
the deserialization logic by taking advantage of known lengths per
type.

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

test routing::network_graph::benches::read_network_graph  ... bench: 1,962,012,392 ns/iter (+/- 2,391,402)
test routing::network_graph::benches::write_network_graph ... bench: 166,772,461 ns/iter (+/- 295,302)
lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs