Profile picture
Pierre Habouzit @pedantcoder
, 8 tweets, 2 min read Read on Twitter
Re the last discussions, dispatch_async() can be used for 3 different things:

(1) asynchronous state machines (onto the same queue hierarchy), which is a way to address C10k and is fast

(2) getting concurrency (a better pthread_create())

(3) parallelism (dispatch_apply()
(1) provided you use dispatch_async_f for the shortest things to avoid allocating blocks, dispatch is fast, and it's great almost whatever the size of your workitem (assuming you do something meaningful).
(2-3) is way tricker than it looks:

your workitem needs to represent enough work (100µs at the very least, 1ms is best)

your workitems if running concurrently need not to contend, else your perf sinks dramatically.
Contention takes many forms. Locks are obvious, but it really means use of shared resources that can be a bottle neck:
- IPC / daemons
- malloc (locks)
- shared memory (false sharing and other cacheline snoops between cores)
As we presented in WWDC'17: go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure (such as iOS low power mode to name one).
We have repeatedly measured that inefficient concurrency is commonly a 2x cost in time to completion.

It is not a 2x cost in instructions count though, it's just that concurrency kills your IPC rate and you spend a lot of time just waiting.
Lastly be very careful with micro benchmarks: calling your code 1M times in a loop makes the CPU ridiculously better at running it and while improving micro benchmarks is good, you should always have a macro benchmark to validate that it's a good idea.
CS-101 is a big lie: memory, CPU and disk resources aren't infinite, and you share them.

Writing your code as if you're the only one on the system, despite being taught just that in CS-101, is unforgivable.
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Pierre Habouzit
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member and get exclusive features!

Premium member ($30.00/year)

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!