News for hackers. Stuff that matters.
Re:On mutex performance and WTF::Lock by ajross on Sunday April 02, 2017 @02:36PM (#14018322)
So the takeaway here is apparently that POSIX mutexes on macOS/iOS are really slow due to an oddball design decision (fairness), and the browser vendors are racing to see who can reimplment them best. Windows and Linux do this just fine AFAICT.

Seems like it would be more effective for the WebKit folks to just work on their Darwin compatriots to fix the C library locks, no?

Re:On mutex performance and WTF::Lock by rbehrends on Sunday April 02, 2017 @04:38PM (#14018923)
It's not as simple as that. What makes locks expensive (no matter their policy regarding fairness) is blocking. Blocking leads to context switches, which are extremely expensive. Linux not using fairness does not eliminate that problem, it mitigates it (which is a good thing, just not a panacea).

I have a simple benchmark that loses performance the more you parallelize it beyond 4-6 cores, until eventually it becomes slower than the single-threaded case [1]. And contention on that is not actually that high. That's still better than the macOS case, where performance goes to hell for even two threads, of course.

Here's the output of time for 32 threads (running on a 48-core system):

  ./a.out -p 32  7.72s user 75.04s system 2603% cpu 3.179 total
as opposed to the sequential case:

  ./a.out -p 0  1.94s user 0.00s system 99% cpu 1.942 total
As you can see, the program spends the vast majority of the time in the kernel due to blocking constantly. (Note that other factors add to the overhead, too.)

In short, you do not want a high degree of contention, anyway. The macOS locks just make contention hurt much more.

[1] https://gist.github.com/rbehrends/27a1ca27ddec7016cd42428b9c...

Re:On mutex performance and WTF::Lock by Jweb_Guru on Sunday April 02, 2017 @02:39PM (#14018331)
Fairness isn't an oddball design decision, it's an important assumption that many realtime systems rely on.
Re:On mutex performance and WTF::Lock by ajross on Sunday April 02, 2017 @02:41PM (#14018345)
It's oddball in the sense that literally no one else does it. So I don't know what systems you mean, since by definition they don't run on Linux (glibc or bionic) or Windows.

I'm not saying there's no use case for a fair mutex anywhere (though I'll come really close and say that if you want fairness a lock is the wrong abstraction), I'm saying that stuffing a context switch into every contended call to pthread_mutex_unlock() is a terrible design decision.

Re:On mutex performance and WTF::Lock by Jweb_Guru on Sunday April 02, 2017 @02:44PM (#14018362)
If you don't need fairness, don't use a fair lock. The default assumption should be fairness because it doesn't require you to think (just like the default transaction isolation level should be serializable). As the blog post points out, OS X provides unfair mutexes too.

Windows locks are fair by default too, BTW (IIRC anyway). People just use the unfair version when they need performance.

Re:On mutex performance and WTF::Lock by striking on Sunday April 02, 2017 @03:46PM (#14018666)
http://www.pcdoctor-community.com/wp/posts/2007/10/fairness-...

Windows XP tried to implement some amount of fairness in its locks but did not guarantee it. Windows Vista gave up on fairness entirely.

You should just be using unfair locks. Thread starvation is rare in most OSes at this point.

Re:On mutex performance and WTF::Lock by eridius on Sunday April 02, 2017 @04:11PM (#14018800)
Thread starvation is still very much a thing on a lot of devices. macOS and iOS even have a Quality Of Service implementation in the kernel specifically to ensure that high-priority threads get to run when they'd otherwise be starved, and this can lead to low-priority threads being suspended for many seconds at a time under high workload. In fact, it's now really dangerous to use a regular spinlock on iOS and macOS because you can get into a priority inversion livelock if threads from different QoS levels are contending the same lock. The Obj-C runtime was seeing delays of tens of seconds in some cases when using spinlocks, so it now uses a private "os_lock" which is a spinlock that uses SPI to "donate" the QoS of the waiting thread to the one that holds the spinlock.
Re:On mutex performance and WTF::Lock by LeoNatan25 on Sunday April 02, 2017 @06:14PM (#14019354)
As of iOS 10 and macOS 10.12, it is no longer private: `os_unfair_lock`.

https://developer.apple.com/reference/os/os_unfair_lock

Re:On mutex performance and WTF::Lock by eridius on Sunday April 02, 2017 @09:24PM (#14020026)
Oh good! I'll have to do some research at some point to find out if this is literally the same thing as the os_lock that the Obj-C runtime uses, but either way, I'm glad it's available now.
Re:On mutex performance and WTF::Lock by LeoNatan25 on Monday April 03, 2017 @03:56AM (#14021236)
It uses the same private API (or at least, what you described in your previous post).