Advanced Swift Concurrency - Limit taskGroup concurrency
TaskGroups are great for running lots of work in parallel. While tasks are cheap to spin up, you might not want to lock up your entire system processing hundreds of expensive image processing or ML requests.
You can use this trick to limit the number of requests done simultaneously, reducing CPU load, peak memory, and freeing other threads to perform work (if you have enough cores).
While this example is a little verbose, the end result works just like the classic OperationQueue’s maxConcurrentOperationCount. Gone but not forgotten 🙏.
This example comes from WWDC 2024 - Discover Swift enhancements in the Vision framework. (Sorry, dark mode bros, but I am super lazy and screenshotted it directly)