1

I'm working on an application that does high frequency sequential calculations. Since in some parts of the process we have I/O tasks, all methods are written async. Since the application was facing some performance issues I ran diagnostics while debugging and found out that the WorkerThread.WorkerThreadStart() is the hottest path of the application as you can see in the screenshot.

enter image description here

We know that this method is responsible for executing work items queued to the thread pool. Is this report showing that we are abusing the async/await all the way in the application or it can be seen as normal? In such case what would you suggest to improve the performance?

CageE
  • 419
  • 4
  • 13
  • 2
    Without seeing your code it's hard to say. This normally happens when too many tasks are queued to be run at once. Are you overusing `Task.Run` somewhere? Or perhaps blocking in the middle of a task (rather than using `await`)? – Charlieface Jul 02 '23 at 10:35
  • I use Task.Run here and there but not sure how much they impact. Do you this is more because of using Task.Run than having all hundreds of methods async/await? The calculation process has relatively long stack chains of many small methods that are all async. – CageE Jul 02 '23 at 11:15
  • 2
    This is a likely problem if there are a lot of tasks that are doing synchronous blocking, rather than doing `await`. You say "calculation" is it a complex CPU-bound operation? Please show some relevant code. – Charlieface Jul 02 '23 at 11:19
  • this is kind of a big application. I can't really choose a part that could give you the bigger picture. Yes there are several methods that do cpu-bound operations especially using Linq to iterate over arrays. I would have guessed that these parts would consume the most CPU but as we see it's the thread pool. As I said the calculations part involves long async/await call stacks (chain of async methods calling each other and awaiting). So my question is this behavior because of the long asnyc/await stacks or something else like using Task.Run as you suggested? – CageE Jul 02 '23 at 11:30
  • 1
    The async/await itself should not cause this, if anything the opposite, because as soon as you hit an `await` it will offload the task completely, not using any thread at all. If you have long-running CPU-bound operations then maybe use `TaskCreationOptions.LongRunning` see also https://stackoverflow.com/a/37611613/14868997 Note that this is not a panacea, it should only be used on very computationally heavy tasks, not tasks that are inherently IO bound, such as disk or network access. – Charlieface Jul 02 '23 at 15:01

0 Answers0