]> git.ipfire.org Git - thirdparty/git.git/commitdiff
prio_queue_reverse: don't swap elements with themselves
authorJeff King <peff@peff.net>
Mon, 24 Apr 2017 11:49:20 +0000 (07:49 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 25 Apr 2017 04:16:44 +0000 (21:16 -0700)
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
prio-queue.c

index e4365b00d6c3366e6753fc6da3fe1a165ab1222d..76782b66d4dde5bf01190963b19c403856756860 100644 (file)
@@ -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);
 }