Optimizing std::sort in libc++ Aditya Kumar Divya Shanmughan - - PowerPoint PPT Presentation

optimizing std sort in libc
SMART_READER_LITE
LIVE PREVIEW

Optimizing std::sort in libc++ Aditya Kumar Divya Shanmughan - - PowerPoint PPT Presentation

Optimizing std::sort in libc++ Aditya Kumar Divya Shanmughan Issues with std::sort (libc++) Worst case clang-libc++ O(N^2) vs. gcc-libstdc++ O(NlogN) Time complexity (worst case*) 40 gcc-libstdc++ 35 clang-libcxx 30 25 Time


slide-1
SLIDE 1

Optimizing std::sort in libc++

Aditya Kumar Divya Shanmughan

slide-2
SLIDE 2

Issues with std::sort (libc++)

  • Worst case

– clang-libc++ O(N^2) vs. gcc-libstdc++ O(NlogN)

5 10 15 20 25 30 35 40 32 64 128 256 512 1024 2048 4096 8192 16384 32768

Time (seconds) Data Size (Bytes)

Time complexity (worst case*)

gcc-libstdc++ clang-libcxx

* https://bugs.llvm.org/show_bug.cgi?id=20837

slide-3
SLIDE 3

Sorting Algorithm in libc++

  • Sorting algorithm currently implemented in

libc++ uses quicksort

  • Worst Case Complexity – O(N^2)
  • Recursion Stack Space – O(logN)
slide-4
SLIDE 4

Modifications done

  • Convert to introsort*

– Sorting technique, which begins with quicksort and switches to heapsort after recursion reaches a threshold – Worst case complexity of O(NlogN)

  • Eliminate recursion

– Replaced memory Intensive recursive calls with stack – std::stack uses std::deque, which uses std::algorithm :(

  • Improved worst case time complexity by a factor of 10

– https://reviews.llvm.org/D36423

https://en.wikipedia.org/wiki/Introsort

slide-5
SLIDE 5

Sorting Results Plot (With std-benchmark)

* https://github.com/hiraditya/std-benchmark

slide-6
SLIDE 6

Thank You