Skip to content

Commit f59be3d

Browse files
committed
f: slice optimization
1 parent c329c45 commit f59be3d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lightning/src/ln/msgs.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -2777,16 +2777,21 @@ impl Writeable for OutboundTrampolinePayload {
27772777
});
27782778
},
27792779
Self::LegacyBlindedPathEntry { amt_to_forward, outgoing_cltv_value, payment_paths, invoice_features } => {
2780-
let mut blinded_path_serialization: Vec<u8> = Vec::with_capacity(2048);
2781-
for current_payment_path in payment_paths {
2782-
current_payment_path.inner_blinded_path().write(&mut blinded_path_serialization)?;
2783-
current_payment_path.payinfo.write(&mut blinded_path_serialization)?;
2784-
}
2780+
let mut blinded_path_serialization = [0u8; 2048]; // Fixed-length buffer on the stack
2781+
let serialization_length = {
2782+
let mut blinded_path_slice = &mut blinded_path_serialization[..];
2783+
for current_payment_path in payment_paths {
2784+
current_payment_path.inner_blinded_path().write(&mut blinded_path_slice)?;
2785+
current_payment_path.payinfo.write(&mut blinded_path_slice)?;
2786+
}
2787+
2048 - blinded_path_slice.len()
2788+
};
2789+
let blinded_path_serialization = &blinded_path_serialization[..serialization_length];
27852790
_encode_varint_length_prefixed_tlv!(w, {
27862791
(2, HighZeroBytesDroppedBigSize(*amt_to_forward), required),
27872792
(4, HighZeroBytesDroppedBigSize(*outgoing_cltv_value), required),
27882793
(21, invoice_features.as_ref().map(|m| WithoutLength(m)), option),
2789-
(22, WithoutLength(&blinded_path_serialization), required)
2794+
(22, WithoutLength(blinded_path_serialization), required)
27902795
});
27912796
},
27922797
}

0 commit comments

Comments
 (0)