From: Matt Corallo Date: Thu, 3 Oct 2024 16:54:14 +0000 (+0000) Subject: Marginally reduce allocations in `lightning-invoice` X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=052e7c3df06012e0787b65b11d740843e26c3c1a;p=rust-lightning Marginally reduce allocations in `lightning-invoice` In aa2f6b47df312f026213d0ceaaff20ffe955c377 we refactored `lightning-invoice` de/serialization to use the new version of `bech32`, but in order to keep the public API the same we introduced one allocation we could have skipped. Instead, here, we replace the public `Utf8Error` with `FromUtf8Error` which contains the original data which failed conversion, removing an allocation in the process. --- diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index 446fd5c71..da43edc5b 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -1,3 +1,4 @@ +use alloc::string; #[cfg(feature = "std")] use std::error; #[cfg(not(feature = "std"))] @@ -5,7 +6,6 @@ use core::convert::TryFrom; use core::fmt; use core::fmt::{Display, Formatter}; use core::num::ParseIntError; -use core::str; use core::str::FromStr; use bech32::primitives::decode::{CheckedHrpstring, CheckedHrpstringError}; @@ -613,7 +613,7 @@ impl FromBase32 for Description { fn from_base32(field_data: &[Fe32]) -> Result { let bytes = Vec::::from_base32(field_data)?; - let description = String::from(str::from_utf8(&bytes)?); + let description = String::from_utf8(bytes)?; Ok(Description::new(description).expect( "Max len is 639=floor(1023*5/8) since the len field is only 10bits long" )) @@ -824,7 +824,7 @@ macro_rules! from_error { from_error!(Bolt11ParseError::MalformedSignature, bitcoin::secp256k1::Error); from_error!(Bolt11ParseError::ParseAmountError, ParseIntError); -from_error!(Bolt11ParseError::DescriptionDecodeError, str::Utf8Error); +from_error!(Bolt11ParseError::DescriptionDecodeError, string::FromUtf8Error); impl From for Bolt11ParseError { fn from(e: CheckedHrpstringError) -> Self { diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index f696e3545..8baedbe26 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -51,7 +51,7 @@ use core::num::ParseIntError; use core::ops::Deref; use core::slice::Iter; use core::time::Duration; -use core::str; +use alloc::string; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error}; @@ -98,7 +98,7 @@ pub enum Bolt11ParseError { MalformedHRP, TooShortDataPart, UnexpectedEndOfTaggedFields, - DescriptionDecodeError(str::Utf8Error), + DescriptionDecodeError(string::FromUtf8Error), PaddingError, IntegerOverflowError, InvalidSegWitProgramLength,