Skip to content

Commit b415372

Browse files
committed
Use rounded capacity for offer and refund building
This avoids heap fragmentation as it will avoid re-allocations when using OfferBuilder and RefundBuilder, though it may over-allocate.
1 parent 6d4b4b2 commit b415372

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

lightning/src/offers/offer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ use crate::ln::channelmanager::PaymentId;
9191
use crate::types::features::OfferFeatures;
9292
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
9393
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
94+
use crate::offers::alloc::WithRoundedCapacity;
9495
use crate::offers::merkle::{TaggedHash, TlvRecord, TlvStream};
9596
use crate::offers::nonce::Nonce;
9697
use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
@@ -438,7 +439,7 @@ macro_rules! offer_builder_methods { (
438439
}
439440
}
440441

441-
let mut bytes = Vec::new();
442+
let mut bytes = Vec::with_rounded_capacity($self.offer.serialized_length());
442443
$self.offer.write(&mut bytes).unwrap();
443444

444445
let id = OfferId::from_valid_offer_tlv_stream(&bytes);

lightning/src/offers/refund.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ use crate::ln::channelmanager::PaymentId;
9898
use crate::types::features::InvoiceRequestFeatures;
9999
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
100100
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
101+
use crate::offers::alloc::WithRoundedCapacity;
101102
use crate::offers::invoice_request::{ExperimentalInvoiceRequestTlvStream, ExperimentalInvoiceRequestTlvStreamRef, InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef};
102103
use crate::offers::nonce::Nonce;
103104
use crate::offers::offer::{ExperimentalOfferTlvStream, ExperimentalOfferTlvStreamRef, OfferTlvStream, OfferTlvStreamRef};
@@ -338,7 +339,7 @@ macro_rules! refund_builder_methods { (
338339
$self.refund.payer.0 = metadata;
339340
}
340341

341-
let mut bytes = Vec::new();
342+
let mut bytes = Vec::with_rounded_capacity($self.refund.serialized_length());
342343
$self.refund.write(&mut bytes).unwrap();
343344

344345
Ok(Refund {

0 commit comments

Comments
 (0)