refactor: Avoid repeated sfPaymentRemaining lookups in makeRegularPayment

Cache the sfPaymentRemaining proxy once instead of re-fetching it at
each of the three places it's read in the loop condition, the final-
payment assert, and the overpayment guard. It's a write-through proxy,
so it still reflects doPayment's mutations each iteration.

Also explicitly zero-initialize totalPaid so the starting baseline
isn't left implicit.
This commit is contained in:
Vito
2026-07-16 12:28:53 +02:00
parent 6d60b12e55
commit 6e26766681

View File

@@ -1517,10 +1517,15 @@ makeRegularPayment(
// Keep a running total of the actual parts paid
LoanPaymentParts totalParts;
Number totalPaid;
Number totalPaid = kNumZero;
std::size_t numPayments = 0;
while ((amount >= (totalPaid + periodic.totalDue)) && loan->at(sfPaymentRemaining) > 0 &&
// Cached here (rather than re-looking up loan->at(sfPaymentRemaining) at each use) since it's
// read multiple times below. It's a write-through proxy, so it still reflects doPayment's
// mutations each iteration.
auto paymentRemainingProxy = loan->at(sfPaymentRemaining);
while ((amount >= (totalPaid + periodic.totalDue)) && paymentRemainingProxy > 0 &&
numPayments < kLoanMaximumPaymentsPerTransaction)
{
// Try to make more payments
@@ -1534,8 +1539,7 @@ makeRegularPayment(
++numPayments;
XRPL_ASSERT_PARTS(
(periodic.specialCase == PaymentSpecialCase::Final) ==
(loan->at(sfPaymentRemaining) == 0),
(periodic.specialCase == PaymentSpecialCase::Final) == (paymentRemainingProxy == 0),
"xrpl::detail::makeRegularPayment",
"final payment is the final payment");
@@ -1578,9 +1582,9 @@ makeRegularPayment(
bool const overpaymentSupported =
paymentType == LoanPaymentType::Overpayment && loan->isFlag(lsfLoanOverpayment);
bool const overpaymentAllowed = //
loan->at(sfPaymentRemaining) > 0 && //
totalPaid < roundedAmount && //
bool const overpaymentAllowed = //
paymentRemainingProxy > 0 && //
totalPaid < roundedAmount && //
numPayments < kLoanMaximumPaymentsPerTransaction;
if (overpaymentSupported && overpaymentAllowed)