paint_down_to_common: use prio_queue

When we are traversing to find merge bases, we keep our usual commit_list of commits to process, sorted by their commit timestamp. As we add each parent to the list, we have to spend "O(width of history)" to do the insertion, where the width of history is the number of simultaneous lines of development. If we instead use a heap-based priority queue, we can do these insertions in "O(log width)" time. This provides minor speedups to merge-base calculations (timings in linux.git, warm cache, best-of-five): [before] $ git merge-base HEAD v2.6.12 real 0m3.251s user 0m3.148s sys 0m0.104s [after] $ git merge-base HEAD v2.6.12 real 0m3.234s user 0m3.108s sys 0m0.128s That's only an 0.5% speedup, but it does help protect us against pathological cases. While we are munging the "interesting" function, we also take the opportunity to give it a more descriptive name, and convert the return value to an int (we returned the first interesting commit, but nobody ever looked at it). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 14, 2014 at 01:53 UTC 73f43f220f0276012de50c84413fd61bf6aa307b
1 file changed +19 -23
commit.c
+19 -23
@@ -786,45 +786,41 @@ void sort_in_topological_order(struct commit_list **list, enum rev_sort_order so
786
787 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
788
789 -static struct commit *interesting(struct commit_list *list)
789 +static int queue_has_nonstale(struct prio_queue *queue)
790 {
791 - while (list) {
792 - struct commit *commit = list->item;
793 - list = list->next;
794 - if (commit->object.flags & STALE)
795 - continue;
796 - return commit;
791 + int i;
792 + for (i = 0; i < queue->nr; i++) {
793 + struct commit *commit = queue->array[i].data;
794 + if (!(commit->object.flags & STALE))
795 + return 1;
796 }
798 - return NULL;
797 + return 0;
798 }
799
800 /* all input commits in one and twos[] must have been parsed! */
801 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
802 {
804 - struct commit_list *list = NULL;
803 + struct prio_queue queue = { compare_commits_by_commit_date };
804 struct commit_list *result = NULL;
805 int i;
806
807 one->object.flags |= PARENT1;
809 - commit_list_insert_by_date(one, &list);
810 - if (!n)
811 - return list;
808 + if (!n) {
809 + commit_list_append(one, &result);
810 + return result;
811 + }
812 + prio_queue_put(&queue, one);
813 +
814 for (i = 0; i < n; i++) {
815 twos[i]->object.flags |= PARENT2;
814 - commit_list_insert_by_date(twos[i], &list);
816 + prio_queue_put(&queue, twos[i]);
817 }
818
817 - while (interesting(list)) {
818 - struct commit *commit;
819 + while (queue_has_nonstale(&queue)) {
820 + struct commit *commit = prio_queue_get(&queue);
821 struct commit_list *parents;
820 - struct commit_list *next;
822 int flags;
823
823 - commit = list->item;
824 - next = list->next;
825 - free(list);
826 - list = next;
827 -
824 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
825 if (flags == (PARENT1 | PARENT2)) {
826 if (!(commit->object.flags & RESULT)) {
@@ -843,11 +839,11 @@ static struct commit_list *paint_down_to_common(struct commit *one, int n, struc
839 if (parse_commit(p))
840 return NULL;
841 p->object.flags |= flags;
846 - commit_list_insert_by_date(p, &list);
842 + prio_queue_put(&queue, p);
843 }
844 }
845
850 - free_commit_list(list);
846 + clear_prio_queue(&queue);
847 return result;
848 }
849