xpay: trim constraints: small optimization
What changed, and why it matters
This commit is a small internal code cleanup in Core Lightning's payment routing plugin. It replaces a loop that removes old routing hints one-by-one with a single bulk removal. There is no indication this fixes a security bug or changes externally visible behavior.
No security action needed. Treat as a normal performance/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors layer_trim_constraints() in plugins/askrene/layer.c. Previously the function iterated over an array of channel intelligence entries and removed each stale entry individually via tal_arr_remove(), tracking whether any change occurred. The new version counts how many leading entries are below the cutoff (relying on the array being sorted by timestamp), deletes the hash entry once, removes the leading range with tal_arr_remove_range(), and either frees the remaining array or re-adds it to the hash table. This is purely an optimization to reduce reallocations.
Changed components
plugins/askrene/layer.cInspect captured patch +17 / −17
diff --git a/plugins/askrene/layer.c b/plugins/askrene/layer.c
index fdfb96df..cfc7225b 100644
--- a/plugins/askrene/layer.c
+++ b/plugins/askrene/layer.c
@@ -1206,32 +1206,32 @@ size_t layer_trim_constraints(struct layer *layer, u64 cutoff)
for (intelarr = channel_intel_hash_first(layer->channel_intels, &intelit);
intelarr;
intelarr = channel_intel_hash_next(layer->channel_intels, &intelit)) {
- bool changed = false;
+ size_t count_old = 0;
+ /* We assume the array is sorted by timestamp */
for (size_t i = 0; i < tal_count(intelarr); i++) {
if (channel_intel_timestamp(&intelarr[i]) >= cutoff)
continue;
- /* Remove from table before realloc! */
- if (!changed)
- channel_intel_hash_del(layer->channel_intels, intelarr);
+ count_old++;
/* The pointer inside channel_intel has to be freed. */
tal_steal(tmpctx, intelarr[i].impression);
tal_steal(tmpctx, intelarr[i].constraint);
-
- tal_arr_remove(&intelarr, i);
- changed = true;
- num_removed++;
- i--;
}
- if (!changed)
- continue;
+ num_removed += count_old;
+ if(count_old){
+ /* Remove from table before realloc! */
+ channel_intel_hash_del(layer->channel_intels, intelarr);
+
+ tal_arr_remove_range(&intelarr, 0, count_old);
- /* We emptied it, just free. */
- if (tal_count(intelarr) == 0)
- tal_free(intelarr);
- else {
- /* Still has members, put it back. */
- channel_intel_hash_add(layer->channel_intels, intelarr);
+ /* We emptied it, just free. */
+ if (tal_count(intelarr) == 0)
+ tal_free(intelarr);
+ else {
+ /* Still has members, put it back. */
+ channel_intel_hash_add(layer->channel_intels,
+ intelarr);
+ }
}
}
Why this scored 11/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.