Lean Programs, Branch Mispredictions, and Sorting

According to a folk theorem, every program can be transformed into a program that produces the same output and only has one loop. We generalize this to a form where the resulting program has one loop and no other branches than the one associated with the loop control. For this branch, branch prediction is easy even for a static branch predictor. If the original program is of length κ, measured in the number of assembly-language instructions, and runs in t(n) time for an input of size n, the transformed program is of length O(κ) and runs in O(κt(n)) time. Normally sorting programs are short, but still κ may be too large for practical purposes. Therefore, we provide more efficient hand-tailored heapsort and mergesort programs. Our programs retain most features of the original programs--e.g. they perform the same number of element comparisons--and they induce O(1) branch mispredictions. On computers where branch mispredictions were expensive, some of our programs were, for integer data and small instances, faster than the counterparts in the GNU implementation of the C++ standard library.

[1]  David A. Patterson,et al.  Computer Organization and Design, Fourth Edition, Fourth Edition: The Hardware/Software Interface (The Morgan Kaufmann Series in Computer Architecture and Design) , 2008 .

[2]  Niklaus Wirth,et al.  Algorithms and Data Structures , 1989, Lecture Notes in Computer Science.

[3]  Jesper Larsson Träff,et al.  A Meticulous Analysis of Mergesort Programs , 1997, CIAC.

[4]  David Harel,et al.  On folk theorems , 1980, SIGA.

[5]  Herbert S. Wilf,et al.  Algorithms and Complexity , 2010, Lecture Notes in Computer Science.

[6]  Sophus Mortensen,et al.  Refining the Pure-C Cost Model , 2001 .

[7]  Sebastian Winkel,et al.  Super Scalar Sample Sort , 2004, ESA.

[8]  Robert Sedgewick,et al.  Algorithms in C - parts 1-4: fundamentals, data structures, sorting, searching (3. ed.) , 1997 .

[9]  Bjarne Stroustrup,et al.  The C++ programming language (2nd ed.) , 1991 .

[10]  David Gregg,et al.  An experimental study of sorting and branch prediction , 2008, JEAL.

[11]  Alfred V. Aho,et al.  Compilers: Principles, Techniques, and Tools , 1986, Addison-Wesley series in computer science / World student series edition.

[12]  장훈,et al.  [서평]「Computer Organization and Design, The Hardware/Software Interface」 , 1997 .

[13]  Brian W. Kernighan,et al.  The C Programming Language , 1978 .

[14]  Susanne Albers,et al.  Algorithms – ESA 2004 , 2004, Lecture Notes in Computer Science.

[15]  Robert Sedgewick,et al.  Algorithms in C++, Parts 1-4: Fundamentals, Data Structure, Sorting, Searching, Third Edition , 1998 .

[16]  Gerth Stølting Brodal,et al.  Tradeoffs Between Branch Mispredictions and Comparisons for Sorting Algorithms , 2005, WADS.