Oleg Zabluda's blog
Wednesday, April 25, 2012
Academic research on sorting algorithms typically assumes either random initial conditions (average complexity) or...
Academic research on sorting algorithms typically assumes either random initial conditions (average complexity) or worst case conditions. However in real world, the sequence is almost always non-random and have large ascending or descending sub-sequences. Worst case can happen if it is common (i.e. reverse order for quicksort) or due to DOS via Algorithmic Complexity Attack. Which brings us to


Timsort is a hybrid sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was invented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsets of the data that are already ordered, and uses the subsets to sort the data more efficiently. [...] Timsort has been Python's standard sorting algorithm since version 2.3. It is now also used to sort arrays in Java SE 7, and on the Android platform.

Timsort has best case complexity of N, at the cost of requiring additional memory, as much as N/2 pointers for the random data.



Double-checked locking pattern (http://en.wikipedia.org/wiki/Double-checked_locking), was first published by Douglas...
Double-checked locking pattern (http://en.wikipedia.org/wiki/Double-checked_locking), was first published by Douglas C. Schmidt, Editor-in-chief C++ Report, in March 1996

That double-checked locking implementation was still considered correct in Oct 1999 Dr. Dobbs Journal (DDJ) article "Singleton Creation the Thread-safe Way".

The first time people started realizing that the canonical implementation is actually wrong, was around 2001.

By 2004, there was a good comprehensive article about it:
Scott Meyers and Andrei Alexandrescu September 2004 DDJ paper:
"C++ and the Perils of Double-Checked Locking"

At the time, it was quite instructive to me to see how that could have slipped by so many experts for so long, and how long it took people to figure out what is trivial now:

One thread writes to variable with the lock held, but another thread reads same variable without lock held. It's a data race, plane and simple.

The reason it is so trivial now is due to heroic efforts of many people, the most by Hans Boehm, who not only figured it out, but formalized C/C++ memory model and contributed to Java memory model. He also provides the best currently known implementation on the last slide of his, from his recent ACM Bay Area talk


C++11 fine-tuned double-checked locking

atomic x_init;
if (!x_init.load(memory_order_acquire) {
if (!x_init.load(memory_order_relaxed) {
initialize x;
x_init.store(true, memory_order_release);
use x;

Intel/AMD finally published their official memory models in 2007-2008.
Turns out on x86 the implementation above is "free" i.e. requires no additional fences, locks or nothing.

Also see:


Powered by Blogger