From 1f9e18b77282070e8fef6dbe6983a8c94a3b0efa Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 24 Apr 2017 07:49:20 -0400 Subject: [PATCH] prio_queue_reverse: don't swap elements with themselves Our array-reverse algorithm does the usual "walk from both ends, swapping elements". We can quit when the two indices are equal, since: 1. Swapping an element with itself is a noop. 2. If i and j are equal, then in the next iteration i is guaranteed to be bigge than j, and we will exit the loop. So exiting the loop on equality is slightly more efficient. And more importantly, the new SWAP() macro does not expect to handle noop swaps; it will call memcpy() with the same src and dst pointers in this case. It's unclear whether that causes a problem on any platforms by violating the "overlapping memory" constraint of memcpy, but it does cause valgrind to complain. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- prio-queue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prio-queue.c b/prio-queue.c index e4365b00d6..76782b66d4 100644 --- a/prio-queue.c +++ b/prio-queue.c @@ -23,7 +23,7 @@ void prio_queue_reverse(struct prio_queue *queue) if (queue->compare != NULL) die("BUG: prio_queue_reverse() on non-LIFO queue"); - for (i = 0; i <= (j = (queue->nr - 1) - i); i++) + for (i = 0; i < (j = (queue->nr - 1) - i); i++) swap(queue, i, j); } -- 2.11.4.GIT