Skip to content

Instantly share code, notes, and snippets.

@Enricoza
Enricoza / DisaptchForestPremise.md
Last active April 26, 2024 10:15
[iOS - Swift] Organize serial DispatchQueue (Trees) in a small DispatchForest to improve performance and avoid Thread explosion

This code is my opinionated interpretation of an idea expressed by eskimo on this swift forum thread, to which I briefly partecipated. I'll try to summarize his viewpoint in this premise, as well as some of the suggestions from this gist.

  1. You should virtually never create a queue with DispatchQueue.global() nor with the concurrent flag.
  2. You should create a limited number (a small Forest) of SERIAL queues (the Trees), one for each subsystem in your program.
  3. Apart from those base queues (the Trees), you should never create any more queues without specifying the target queue.
  4. You can, instead, create as many queues (the Branches) as you want, as long as they are targeting those base queues (the Trees).
  5. You should dispatch large chunks of code, in order to reduce the overhead of the dispatch operations.
  6. Therefore, you should mov