]> git.bitcoin.ninja Git - rust-lightning/commit
Avoid calling libsecp serialization fns when calculating length
authorMatt Corallo <git@bluematt.me>
Sat, 29 May 2021 18:24:16 +0000 (18:24 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 29 May 2021 23:02:36 +0000 (23:02 +0000)
commita27a2223f65ab4570c37e609ac2884ee6d001403
treedeed5f086734826510b3ef6c2ac44935b63cb12f
parent80e91e5f9afb1475abaf24425aa9e466453d6470
Avoid calling libsecp serialization fns when calculating length

When writing out libsecp256k1 objects during serialization in a
TLV, we potentially calculate the TLV length twice before
performing the actual serialization (once when calculating the
total TLV-stream length and once when calculating the length of the
secp256k1-object-containing TLV). Because the lengths of secp256k1
objects is a constant, we'd ideally like LLVM to entirely optimize
out those calls and simply know the expected length. However,
without cross-language LTO, there is no way for LLVM to verify that
there are no side-effects of the calls to libsecp256k1, leaving
LLVM with no way to optimize them out.

This commit adds a new method to `Writeable` which returns the
length of an object once serialized. It is implemented by default
using `LengthCalculatingWriter` (which LLVM generally optimizes out
for Rust objects) and overrides it for libsecp256k1 objects.

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

test routing::network_graph::benches::read_network_graph  ... bench: 2,035,402,164 ns/iter (+/- 1,855,357)
test routing::network_graph::benches::write_network_graph ... bench: 308,235,267 ns/iter (+/- 140,202)
lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs