From 093f9fbf38f1ac21b83f8085a6c565b1b87f2dce Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 3 Oct 2024 16:54:20 +0000 Subject: [PATCH] Hold a reference to byte arrays when serializing to bech32 When we serialize from a byte array to bech32 in `lightning-invoice`, we can either copy the array itself into the iterator or hold a reference to the array and iterate through that. In aa2f6b47df312f026213d0ceaaff20ffe955c377 we opted to copy the array into the iterator, which is fine for the current array sizes we're working with, but does result in additional memory on the stack if, in the future, we end up writing large arrays. Instead, here, we switch to using the slice serialization code when writing arrays, (very marginally) reducing code size and reducing stack usage. --- lightning-invoice/src/ser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightning-invoice/src/ser.rs b/lightning-invoice/src/ser.rs index 3d79a253314..84e5c97c136 100644 --- a/lightning-invoice/src/ser.rs +++ b/lightning-invoice/src/ser.rs @@ -29,7 +29,7 @@ pub(crate) trait Base32Len: Base32Iterable { impl Base32Iterable for [u8; N] { fn fe_iter<'s>(&'s self) -> Box + 's> { - Box::new((*self).into_iter().bytes_to_fes()) + Box::new(self.iter().copied().bytes_to_fes()) } }