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>
3 files changed
tree: 1e3e8ad7f8f760a950643f4970f2daee2364b1b7
  1. arm/
  2. compat/
  3. contrib/
  4. Documentation/
  5. git-gui/
  6. gitk-git/
  7. gitweb/
  8. mozilla-sha1/
  9. perl/
  10. ppc/
  11. t/
  12. templates/
  13. xdiff/
  14. .gitignore
  15. .mailmap
  16. alloc.c
  17. archive-tar.c
  18. archive-zip.c
  19. archive.c
  20. archive.h
  21. attr.c
  22. attr.h
  23. base85.c
  24. blob.c
  25. blob.h
  26. builtin-add.c
  27. builtin-annotate.c
  28. builtin-apply.c
  29. builtin-archive.c
  30. builtin-blame.c
  31. builtin-branch.c
  32. builtin-bundle.c
  33. builtin-cat-file.c
  34. builtin-check-attr.c
  35. builtin-check-ref-format.c
  36. builtin-checkout-index.c
  37. builtin-clean.c
  38. builtin-commit-tree.c
  39. builtin-commit.c
  40. builtin-config.c
  41. builtin-count-objects.c
  42. builtin-describe.c
  43. builtin-diff-files.c
  44. builtin-diff-index.c
  45. builtin-diff-tree.c
  46. builtin-diff.c
  47. builtin-fast-export.c
  48. builtin-fetch--tool.c
  49. builtin-fetch-pack.c
  50. builtin-fetch.c
  51. builtin-fmt-merge-msg.c
  52. builtin-for-each-ref.c
  53. builtin-fsck.c
  54. builtin-gc.c
  55. builtin-grep.c
  56. builtin-http-fetch.c
  57. builtin-init-db.c
  58. builtin-log.c
  59. builtin-ls-files.c
  60. builtin-ls-remote.c
  61. builtin-ls-tree.c
  62. builtin-mailinfo.c
  63. builtin-mailsplit.c
  64. builtin-merge-base.c
  65. builtin-merge-file.c
  66. builtin-merge-ours.c
  67. builtin-mv.c
  68. builtin-name-rev.c
  69. builtin-pack-objects.c
  70. builtin-pack-refs.c
  71. builtin-prune-packed.c
  72. builtin-prune.c
  73. builtin-push.c
  74. builtin-read-tree.c
  75. builtin-reflog.c
  76. builtin-rerere.c
  77. builtin-reset.c
  78. builtin-rev-list.c
  79. builtin-rev-parse.c
  80. builtin-revert.c
  81. builtin-rm.c
  82. builtin-send-pack.c
  83. builtin-shortlog.c
  84. builtin-show-branch.c
  85. builtin-show-ref.c
  86. builtin-stripspace.c
  87. builtin-symbolic-ref.c
  88. builtin-tag.c
  89. builtin-tar-tree.c
  90. builtin-unpack-objects.c
  91. builtin-update-index.c
  92. builtin-update-ref.c
  93. builtin-upload-archive.c
  94. builtin-verify-pack.c
  95. builtin-verify-tag.c
  96. builtin-write-tree.c
  97. builtin.h
  98. bundle.c
  99. bundle.h
  100. cache-tree.c
  101. cache-tree.h
  102. cache.h
  103. check-builtins.sh
  104. check-racy.c
  105. color.c
  106. color.h
  107. combine-diff.c
  108. command-list.txt
  109. commit.c
  110. commit.h
  111. config.c
  112. config.mak.in
  113. configure.ac
  114. connect.c
  115. convert.c
  116. copy.c
  117. COPYING
  118. csum-file.c
  119. csum-file.h
  120. ctype.c
  121. daemon.c
  122. date.c
  123. decorate.c
  124. decorate.h
  125. delta.h
  126. diff-delta.c
  127. diff-lib.c
  128. diff.c
  129. diff.h
  130. diffcore-break.c
  131. diffcore-delta.c
  132. diffcore-order.c
  133. diffcore-pickaxe.c
  134. diffcore-rename.c
  135. diffcore.h
  136. dir.c
  137. dir.h
  138. dump-cache-tree.c
  139. entry.c
  140. environment.c
  141. exec_cmd.c
  142. exec_cmd.h
  143. fast-import.c
  144. fetch-pack.h
  145. fixup-builtins
  146. generate-cmdlist.sh
  147. git-add--interactive.perl
  148. git-am.sh
  149. git-archimport.perl
  150. git-bisect.sh
  151. git-checkout.sh
  152. git-clone.sh
  153. git-compat-util.h
  154. git-cvsexportcommit.perl
  155. git-cvsimport.perl
  156. git-cvsserver.perl
  157. git-filter-branch.sh
  158. git-help--browse.sh
  159. git-instaweb.sh
  160. git-lost-found.sh
  161. git-merge-octopus.sh
  162. git-merge-one-file.sh
  163. git-merge-resolve.sh
  164. git-merge-stupid.sh
  165. git-merge.sh
  166. git-mergetool.sh
  167. git-parse-remote.sh
  168. git-pull.sh
  169. git-quiltimport.sh
  170. git-rebase--interactive.sh
  171. git-rebase.sh
  172. git-relink.perl
  173. git-remote.perl
  174. git-repack.sh
  175. git-request-pull.sh
  176. git-send-email.perl
  177. git-sh-setup.sh
  178. git-stash.sh
  179. git-submodule.sh
  180. git-svn.perl
  181. GIT-VERSION-GEN
  182. git.c
  183. git.spec.in
  184. grep.c
  185. grep.h
  186. hash-object.c
  187. hash.c
  188. hash.h
  189. help.c
  190. http-push.c
  191. http-walker.c
  192. http.c
  193. http.h
  194. ident.c
  195. imap-send.c
  196. index-pack.c
  197. INSTALL
  198. interpolate.c
  199. interpolate.h
  200. list-objects.c
  201. list-objects.h
  202. lockfile.c
  203. log-tree.c
  204. log-tree.h
  205. mailmap.c
  206. mailmap.h
  207. Makefile
  208. match-trees.c
  209. merge-file.c
  210. merge-index.c
  211. merge-recursive.c
  212. merge-tree.c
  213. mktag.c
  214. mktree.c
  215. object-refs.c
  216. object.c
  217. object.h
  218. pack-check.c
  219. pack-redundant.c
  220. pack-write.c
  221. pack.h
  222. pager.c
  223. parse-options.c
  224. parse-options.h
  225. patch-delta.c
  226. patch-id.c
  227. patch-ids.c
  228. patch-ids.h
  229. path-list.c
  230. path-list.h
  231. path.c
  232. pkt-line.c
  233. pkt-line.h
  234. pretty.c
  235. progress.c
  236. progress.h
  237. quote.c
  238. quote.h
  239. reachable.c
  240. reachable.h
  241. read-cache.c
  242. README
  243. receive-pack.c
  244. reflog-walk.c
  245. reflog-walk.h
  246. refs.c
  247. refs.h
  248. remote.c
  249. remote.h
  250. revision.c
  251. revision.h
  252. run-command.c
  253. run-command.h
  254. send-pack.h
  255. server-info.c
  256. setup.c
  257. sha1_file.c
  258. sha1_name.c
  259. shallow.c
  260. shell.c
  261. show-index.c
  262. sideband.c
  263. sideband.h
  264. strbuf.c
  265. strbuf.h
  266. symlinks.c
  267. tag.c
  268. tag.h
  269. tar.h
  270. test-absolute-path.c
  271. test-chmtime.c
  272. test-date.c
  273. test-delta.c
  274. test-genrandom.c
  275. test-match-trees.c
  276. test-parse-options.c
  277. test-sha1.c
  278. test-sha1.sh
  279. trace.c
  280. transport.c
  281. transport.h
  282. tree-diff.c
  283. tree-walk.c
  284. tree-walk.h
  285. tree.c
  286. tree.h
  287. unpack-file.c
  288. unpack-trees.c
  289. unpack-trees.h
  290. update-server-info.c
  291. upload-pack.c
  292. usage.c
  293. utf8.c
  294. utf8.h
  295. var.c
  296. walker.c
  297. walker.h
  298. write_or_die.c
  299. ws.c
  300. wt-status.c
  301. wt-status.h
  302. xdiff-interface.c
  303. xdiff-interface.h