Skip to content

Commit 9f0c0e1

Browse files
committed
askrene: use a simple array as our queue.
We only ever visit each node once, so we can just use an array. This avoids calling tal() all the time, which is *especially* slow when we're memory tracking. I had an old canned gossmap which I benchmarked for these (and in particular one node was unreachable, and that was slow): Before: 17.27 seconds After: 5.80 seconds Signed-off-by: Rusty Russell <[email protected]>
1 parent 79fa3d3 commit 9f0c0e1

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

plugins/askrene/mcf.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <assert.h>
33
#include <ccan/bitmap/bitmap.h>
44
#include <ccan/list/list.h>
5-
#include <ccan/lqueue/lqueue.h>
65
#include <ccan/tal/str/str.h>
76
#include <ccan/tal/tal.h>
87
#include <common/utils.h>
@@ -687,13 +686,6 @@ init_linear_network(const tal_t *ctx, const struct pay_parameters *params)
687686
return linear_network;
688687
}
689688

690-
/* Simple queue to traverse the network. */
691-
struct queue_data
692-
{
693-
u32 idx;
694-
struct lqueue_link ql;
695-
};
696-
697689
// TODO(eduardo): unit test this
698690
/* Finds an admissible path from source to target, traversing arcs in the
699691
* residual network with capacity greater than 0.
@@ -705,25 +697,21 @@ find_admissible_path(const struct linear_network *linear_network,
705697
const u32 source, const u32 target, struct arc *prev)
706698
{
707699
bool target_found = false;
700+
/* Simple linear queue of node indexes */
701+
u32 *queue = tal_arr(tmpctx, u32, linear_network->max_num_arcs);
702+
size_t qstart, qend;
708703

709704
for(size_t i=0;i<tal_count(prev);++i)
710705
prev[i].idx=INVALID_INDEX;
711706

712707
// The graph is dense, and the farthest node is just a few hops away,
713708
// hence let's BFS search.
714-
LQUEUE(struct queue_data,ql) myqueue = LQUEUE_INIT;
715-
struct queue_data *qdata;
709+
queue[0] = source;
710+
qstart = 0;
711+
qend = 1;
716712

717-
qdata = tal(tmpctx, struct queue_data);
718-
qdata->idx = source;
719-
lqueue_enqueue(&myqueue,qdata);
720-
721-
while(!lqueue_empty(&myqueue))
722-
{
723-
qdata = lqueue_dequeue(&myqueue);
724-
u32 cur = qdata->idx;
725-
726-
tal_free(qdata);
713+
while (qstart < qend) {
714+
u32 cur = queue[qstart++];
727715

728716
if(cur==target)
729717
{
@@ -748,10 +736,8 @@ find_admissible_path(const struct linear_network *linear_network,
748736
continue;
749737

750738
prev[next] = arc;
751-
752-
qdata = tal(tmpctx, struct queue_data);
753-
qdata->idx = next;
754-
lqueue_enqueue(&myqueue,qdata);
739+
assert(qend < linear_network->max_num_arcs);
740+
queue[qend++] = next;
755741
}
756742
}
757743
return target_found;

0 commit comments

Comments
 (0)