From debcce6a0388bf89d68e04ce2f2a55038a081683 Mon Sep 17 00:00:00 2001 From: DanGould Date: Mon, 9 Jan 2023 23:10:07 -0500 Subject: [PATCH 1/3] Improve magic bytes push performance --- bitcoin/src/psbt/serialize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitcoin/src/psbt/serialize.rs b/bitcoin/src/psbt/serialize.rs index ddbe2ec8..61019f62 100644 --- a/bitcoin/src/psbt/serialize.rs +++ b/bitcoin/src/psbt/serialize.rs @@ -53,7 +53,7 @@ impl PartiallySignedTransaction { let mut buf: Vec = Vec::new(); // - buf.extend(b"psbt"); + buf.extend_from_slice(b"psbt"); buf.push(0xff_u8); From 81ca10701aeed5b6856f7f82877dc09bd4a6adf6 Mon Sep 17 00:00:00 2001 From: DanGould Date: Mon, 9 Jan 2023 23:17:04 -0500 Subject: [PATCH 2/3] Use as_ref() instead of costly clone() --- bitcoin/tests/psbt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitcoin/tests/psbt.rs b/bitcoin/tests/psbt.rs index 389f1727..e78b0b54 100644 --- a/bitcoin/tests/psbt.rs +++ b/bitcoin/tests/psbt.rs @@ -437,7 +437,7 @@ fn finalize_psbt(mut psbt: Psbt) -> Psbt { // Input 1: SegWit UTXO let script_sig = script::Builder::new() - .push_slice(psbt.inputs[1].redeem_script.clone().unwrap().as_bytes()) + .push_slice(psbt.inputs[1].redeem_script.as_ref().unwrap().as_bytes()) .into_script(); psbt.inputs[1].final_script_sig = Some(script_sig); From 1a409ecc8e953041588408526d6f13a50b6cc74b Mon Sep 17 00:00:00 2001 From: DanGould Date: Mon, 9 Jan 2023 23:21:20 -0500 Subject: [PATCH 3/3] Use &[u8] instead of Cursor as Read --- bitcoin/src/psbt/macros.rs | 2 +- bitcoin/src/psbt/raw.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bitcoin/src/psbt/macros.rs b/bitcoin/src/psbt/macros.rs index dd820e81..a5aedd3d 100644 --- a/bitcoin/src/psbt/macros.rs +++ b/bitcoin/src/psbt/macros.rs @@ -54,7 +54,7 @@ macro_rules! impl_psbtmap_deserialize { ($thing:ty) => { impl $crate::psbt::serialize::Deserialize for $thing { fn deserialize(bytes: &[u8]) -> Result { - let mut decoder = crate::io::Cursor::new(bytes); + let mut decoder = bytes; Self::decode(&mut decoder) } } diff --git a/bitcoin/src/psbt/raw.rs b/bitcoin/src/psbt/raw.rs index e9770b65..f8720057 100644 --- a/bitcoin/src/psbt/raw.rs +++ b/bitcoin/src/psbt/raw.rs @@ -11,7 +11,7 @@ use crate::prelude::*; use core::fmt; use core::convert::TryFrom; -use crate::io::{self, Cursor}; +use crate::io; use crate::consensus::encode::{self, ReadExt, WriteExt, Decodable, Encodable, VarInt, serialize, deserialize, MAX_VEC_SIZE}; use crate::psbt::Error; @@ -125,7 +125,7 @@ impl Serialize for Pair { impl Deserialize for Pair { fn deserialize(bytes: &[u8]) -> Result { - let mut decoder = Cursor::new(bytes); + let mut decoder = bytes; Pair::decode(&mut decoder) } }