When I got my first Mac a little over four years ago, the first thing I did was install the nightly build of Webkit. Since then I’ve used a host of different browsers, but recently came back to Safari. This article reminded me why I was using the nightly builds of Webkit back then. It’s noticeably faster and I doubt TCSpinLock
is the only reason.
Yes, we really did achieve a 3.7X speedup on a garbage collection benchmark by removing a call to sleep().
Why sleep()
?
The original TCSpinLock was benchmarked against ten threads running on four CPU cores. In this environment, one thread could monopolize a CPU core, while the thread holding the lock didn’t run at all. Sleeping a little bit was a simple way to guarantee that the thread holding the lock got the CPU.
Why not just use the standard library locks?
Our spinlocks take advantage of a critical optimization: Knowing they’ll be held for a short time. Since spinlocks never sleep, they recover from contention as quickly as possible. As the profile showed, standard locks are much more conservative, and they may even idle a CPU core.