Skip to content

Commit

Permalink
Merge rust-bitcoin/rust-bitcoin#1071: Optimize Witness Serialization
Browse files Browse the repository at this point in the history
9bf9591 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 rust-bitcoin/rust-bitcoin#1068

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

Tree-SHA512: 14553dfed20aee50bb6361d44986b38556cbb3e112e1b4d9e3b401c3da831b6bdf159089966254cf371c225ae929fc78516c96a6114b40a7bc1fda7305295e4a
  • Loading branch information
apoelstra committed Jun 29, 2022
2 parents 6f89b71 + 6154496 commit af29d51
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/blockdata/witness.rs
Expand Up @@ -11,9 +11,6 @@ use crate::prelude::*;
use secp256k1::ecdsa;
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)
///
/// Can be logically seen as an array of byte-arrays `Vec<Vec<u8>>` and indeed you can convert from
Expand Down Expand Up @@ -282,8 +279,15 @@ impl serde::Serialize for Witness {
where
S: serde::Serializer,
{
let vec: Vec<_> = self.to_vec();
serde::Serialize::serialize(&vec, serializer)
use serde::ser::SerializeSeq;

let mut seq = serializer.serialize_seq(Some(self.witness_elements))?;

for elem in self.iter() {
seq.serialize_element(&elem)?;
}

seq.end()
}
}
#[cfg(feature = "serde")]
Expand Down

0 comments on commit af29d51

Please sign in to comment.