Merge rust-bitcoin/rust-bitcoin#1071: Optimize `Witness` Serialization

9bf959180b Optimize Witness Serialization (DanGould)

Pull request description:

  fix #942
  > self.to_vec() allocates, it should be possible to avoid it - just feed the items into serializer.

  based on https://github.com/rust-bitcoin/rust-bitcoin/pull/1068

ACKs for top commit:
  Kixunil:
    ACK 9bf959180b
  apoelstra:
    ACK 9bf959180b

Tree-SHA512: 14553dfed20aee50bb6361d44986b38556cbb3e112e1b4d9e3b401c3da831b6bdf159089966254cf371c225ae929fc78516c96a6114b40a7bc1fda7305295e4a
This commit is contained in:
Andrew Poelstra 2022-06-29 19:26:13 +00:00
commit 4f5fcd3b37
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 9 additions and 5 deletions

View File

@ -11,9 +11,6 @@ use crate::prelude::*;
use secp256k1::ecdsa; use secp256k1::ecdsa;
use crate::VarInt; use crate::VarInt;
#[cfg(feature = "serde")]
use serde;
/// The Witness is the data used to unlock bitcoins since the [segwit upgrade](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki) /// The Witness is the data used to unlock bitcoins since the [segwit upgrade](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki)
/// ///
/// Can be logically seen as an array of byte-arrays `Vec<Vec<u8>>` and indeed you can convert from /// Can be logically seen as an array of byte-arrays `Vec<Vec<u8>>` and indeed you can convert from
@ -282,8 +279,15 @@ impl serde::Serialize for Witness {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
let vec: Vec<_> = self.to_vec(); use serde::ser::SerializeSeq;
serde::Serialize::serialize(&vec, serializer)
let mut seq = serializer.serialize_seq(Some(self.witness_elements))?;
for elem in self.iter() {
seq.serialize_element(&elem)?;
}
seq.end()
} }
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]