Skip to content

Commit 43fe901

Browse files
bdowninggitster
authored andcommitted
compat: Add simplified merge sort implementation from glibc
qsort in Windows 2000 (and various other C libraries) is a Quicksort with the usual O(n^2) worst case. Unfortunately, sorting Git trees seems to get very close to that worst case quite often: $ /git/gitbad runstatus # On branch master qsort, nmemb = 30842 done, 237838087 comparisons. This patch adds a simplified version of the merge sort that is glibc's qsort(3). As a merge sort, this needs a temporary array equal in size to the array that is to be sorted, but has a worst-case performance of O(n log n). The complexity that was removed is: * Doing direct stores for word-size and -aligned data. * Falling back to quicksort if the allocation required to perform the merge sort would likely push the machine into swap. Even with these simplifications, this seems to outperform the Windows qsort(3) implementation, even in Windows XP (where it is "fixed" and doesn't trigger O(n^2) complexity on trees). [jes: moved into compat/qsort.c, as per Johannes Sixt's suggestion] [bcd: removed gcc-ism, thanks to Edgar Toernig. renamed make variable per Junio's comment.] Signed-off-by: Brian Downing <bdowning@lavos.net> Signed-off-by: Steffen Prohaska <prohaska@zib.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7a2078b commit 43fe901

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ all::
137137
# Define THREADED_DELTA_SEARCH if you have pthreads and wish to exploit
138138
# parallel delta searching when packing objects.
139139
#
140+
# Define INTERNAL_QSORT to use Git's implementation of qsort(), which
141+
# is a simplified version of the merge sort used in glibc. This is
142+
# recommended if Git triggers O(n^2) behavior in your platform's qsort().
143+
#
140144

141145
GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
142146
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -722,6 +726,10 @@ ifdef NO_MEMMEM
722726
COMPAT_CFLAGS += -DNO_MEMMEM
723727
COMPAT_OBJS += compat/memmem.o
724728
endif
729+
ifdef INTERNAL_QSORT
730+
COMPAT_CFLAGS += -DINTERNAL_QSORT
731+
COMPAT_OBJS += compat/qsort.o
732+
endif
725733

726734
ifdef THREADED_DELTA_SEARCH
727735
BASIC_CFLAGS += -DTHREADED_DELTA_SEARCH

compat/qsort.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "../git-compat-util.h"
2+
3+
/*
4+
* A merge sort implementation, simplified from the qsort implementation
5+
* by Mike Haertel, which is a part of the GNU C Library.
6+
*/
7+
8+
static void msort_with_tmp(void *b, size_t n, size_t s,
9+
int (*cmp)(const void *, const void *),
10+
char *t)
11+
{
12+
char *tmp;
13+
char *b1, *b2;
14+
size_t n1, n2;
15+
16+
if (n <= 1)
17+
return;
18+
19+
n1 = n / 2;
20+
n2 = n - n1;
21+
b1 = b;
22+
b2 = (char *)b + (n1 * s);
23+
24+
msort_with_tmp(b1, n1, s, cmp, t);
25+
msort_with_tmp(b2, n2, s, cmp, t);
26+
27+
tmp = t;
28+
29+
while (n1 > 0 && n2 > 0) {
30+
if (cmp(b1, b2) <= 0) {
31+
memcpy(tmp, b1, s);
32+
tmp += s;
33+
b1 += s;
34+
--n1;
35+
} else {
36+
memcpy(tmp, b2, s);
37+
tmp += s;
38+
b2 += s;
39+
--n2;
40+
}
41+
}
42+
if (n1 > 0)
43+
memcpy(tmp, b1, n1 * s);
44+
memcpy(b, t, (n - n2) * s);
45+
}
46+
47+
void git_qsort(void *b, size_t n, size_t s,
48+
int (*cmp)(const void *, const void *))
49+
{
50+
const size_t size = n * s;
51+
char buf[1024];
52+
53+
if (size < sizeof(buf)) {
54+
/* The temporary array fits on the small on-stack buffer. */
55+
msort_with_tmp(b, n, s, cmp, buf);
56+
} else {
57+
/* It's somewhat large, so malloc it. */
58+
char *tmp = malloc(size);
59+
msort_with_tmp(b, n, s, cmp, tmp);
60+
free(tmp);
61+
}
62+
}

git-compat-util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,10 @@ static inline int strtol_i(char const *s, int base, int *result)
426426
return 0;
427427
}
428428

429+
#ifdef INTERNAL_QSORT
430+
void git_qsort(void *base, size_t nmemb, size_t size,
431+
int(*compar)(const void *, const void *));
432+
#define qsort git_qsort
433+
#endif
434+
429435
#endif

0 commit comments

Comments
 (0)