Remove double allocation from ScriptBuf::from_hex_prefixed
What changed, and why it matters
This commit is a routine performance optimization. It changes one function so that it decodes a hex string directly into the final data structure instead of first creating a temporary byte buffer and then copying it. There is no security-relevant change visible in the diff.
No security action required. Treat as a normal performance/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors ScriptBuf::
Changed components
primitives/src/script/owned.rsScriptBuf::from_hex_prefixedInspect captured patch +7 / −2
diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs
index a9fc9c8f..79970207 100644
--- a/primitives/src/script/owned.rs
+++ b/primitives/src/script/owned.rs
@@ -60,8 +60,13 @@ impl<T> ScriptBuf<T> {
/// * If the parsed bytes cannot be decoded as a valid script (incl.the length prefix).
#[cfg(feature = "hex")]
pub fn from_hex_prefixed(s: &str) -> Result<Self, FromHexError> {
- let v = hex::decode_to_vec(s)?;
- Ok(encoding::decode_from_slice(&v)?)
+ use crate::hex_codec::{HexPrimitive, ParsePrimitiveError as P};
+
+ HexPrimitive::<Self>::from_str(s).map_err(|err| match err {
+ P::OddLengthString(e) => FromHexError::Hex(e.into()),
+ P::InvalidChar(e) => FromHexError::Hex(e.into()),
+ P::Decode(e) => e.into(),
+ })
}
/// Constructs a new [`ScriptBuf`] from a hex string.
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.