All of my recent computationally intensive and massively parallel
tests demonstrate that task group performance is indistinguishable
from concurrentPerform(…)
.
Cool.
Help me understand why not task group?
I think your benefiting from the fact that the work you’re doing is all CPU bound. In that case you’re correct, TaskGroup
and concurrentPerform(…)
should perform the same, because both are saturating all cores.
Where things change is when the work is not all CPU bound, where there’s a mixture of CPU and I/O operations. In that case Dispatch will overcommit — that is, it’ll start more worker threads than there are cores — but Swift concurrency won’t, and that’ll leave cores idle waiting for I/O to complete.
Processing work that includes a mixture of CPU and I/O operations optimally is a challenging task. Notably, this challenge is not unique to Swift concurrency. It’s a challenging task in Dispatch as well.
Fortunately, it seems like that’s not the case for you, so yay!
Oh, a couple of hints:
-
In both APIs, make sure that each item of work runs long enough to justify the dispatch overhead. That overhead should be less in Swift concurrency than in Dispatch, but if your work items are just a few instructions then the overhead will dominate in either API.
-
In Swift concurrency specifically, if your tasks run for a long time without any await
statements, make sure they at least check for cancellation. You may also want to have them yield.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"