Skip to content

Commit 1f9e18b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f0e802c commit 1f9e18b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

prio-queue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void prio_queue_reverse(struct prio_queue *queue)
2323

2424
if (queue->compare != NULL)
2525
die("BUG: prio_queue_reverse() on non-LIFO queue");
26-
for (i = 0; i <= (j = (queue->nr - 1) - i); i++)
26+
for (i = 0; i < (j = (queue->nr - 1) - i); i++)
2727
swap(queue, i, j);
2828
}
2929

0 commit comments

Comments
 (0)