primitives: Replace double alloc in script formatting with iterators
What changed, and why it matters
This is a routine performance cleanup, not a security fix. It removes an unnecessary intermediate memory allocation when converting a Bitcoin script to a hex string by using an iterator directly. There is no change to behavior, parsing, validation, or cryptographic handling.
No security action needed. Treat as a normal performance/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit replaces encoding::encode_to_vec(self) followed by BytesToHexIter::new(v.iter(), ...) with encoding::EncodableByteIter::new(self) piped directly into BytesToHexIter. This eliminates one vector allocation and a TODO comment. The output remains the same lower-case hex string prefixed by the script encoding. No security boundary is crossed, no input is newly trusted, and no parsing/validation logic changes.
Changed components
primitives/src/script/borrowed.rsScript::to_hex_string_prefixedInspect captured patch +2 / −3
diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs
index cab33adf..07feb004 100644
--- a/primitives/src/script/borrowed.rs
+++ b/primitives/src/script/borrowed.rs
@@ -129,9 +129,8 @@ impl<T> Script<T> {
pub fn to_hex_string_prefixed(&self) -> String {
use hex_unstable::{BytesToHexIter, Case};
- // TODO: Can we remove allocation and use an iterator (like in `hex_codec`)?
- let v = encoding::encode_to_vec(self);
- BytesToHexIter::new(v.iter(), Case::Lower).collect()
+ let iter = encoding::EncodableByteIter::new(self);
+ BytesToHexIter::new(iter, Case::Lower).collect()
}
/// Encodes the script as lower-case hex.
Why this scored 15/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.