Skip to content

Commit 070f752

Browse files
committed
f: Use iter clone instead of Vector allocation
1. This allows save heap memory allocation improving the function's efficiency.
1 parent 5f043eb commit 070f752

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct ForwardNode {
4343

4444
/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
4545
/// route, they are encoded into [`BlindedHop::encrypted_payload`].
46+
#[derive(Clone)]
4647
pub(crate) struct ForwardTlvs {
4748
/// The next hop in the onion message's path.
4849
pub(crate) next_hop: NextMessageHop,
@@ -52,6 +53,7 @@ pub(crate) struct ForwardTlvs {
5253
}
5354

5455
/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
56+
#[derive(Clone)]
5557
pub(crate) struct ReceiveTlvs {
5658
/// If `context` is `Some`, it is used to identify the blinded path that this onion message is
5759
/// sending to. This is useful for receivers to check that said blinded path is being used in
@@ -136,23 +138,22 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
136138
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
137139
let pks = intermediate_nodes.iter().map(|node| &node.node_id)
138140
.chain(core::iter::once(&recipient_node_id));
139-
let tlvs: Vec<ControlTlvs> = pks.clone()
141+
let tlvs = pks.clone()
140142
.skip(1) // The first node's TLVs contains the next node's pubkey
141143
.zip(intermediate_nodes.iter().map(|node| node.short_channel_id))
142144
.map(|(pubkey, scid)| match scid {
143145
Some(scid) => NextMessageHop::ShortChannelId(scid),
144146
None => NextMessageHop::NodeId(*pubkey),
145147
})
146148
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
147-
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })))
148-
.collect();
149+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })));
149150

150-
let max_length = tlvs.iter()
151+
let max_length = tlvs.clone()
151152
.max_by_key(|c| c.serialized_length())
152153
.map(|c| c.serialized_length())
153154
.unwrap_or(0);
154155

155-
let length_tlvs = tlvs.into_iter().map(move |tlv| (max_length, tlv));
156+
let length_tlvs = tlvs.map(|tlv| (max_length, tlv));
156157

157158
utils::construct_blinded_hops(secp_ctx, pks, length_tlvs, session_priv)
158159
}

lightning/src/onion_message/packet.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ for Payload<ParsedOnionMessageContents<<H as CustomOnionMessageHandler>::CustomM
306306
/// or received. Thus we read a `ControlTlvs` rather than reading a [`ForwardTlvs`] or
307307
/// [`ReceiveTlvs`] directly. Also useful on the encoding side to keep forward and receive TLVs in
308308
/// the same iterator.
309+
#[derive(Clone)]
309310
pub(crate) enum ControlTlvs {
310311
/// This onion message is intended to be forwarded.
311312
Forward(ForwardTlvs),

0 commit comments

Comments
 (0)