await Task.Yield() killing performance
Closed this issue · 3 comments
Hey Mike,
I've been having some performance problems. As soon as the library establishes a connection my cpu goes flying into 80%.
I looked in the unity profiler and I saw this "Christmas tree"
Those are hundreds of _ThreadPoolWaitCallback.PerformWaitCallback() per frame, scattered among 40ish thread pool workers.
I followed the breadcrumbs to await Task.Yield(); in private async Task SendAsync() from DotNetWebSocket.cs
I guess since it's not on the main thread, that yield gets executed as fast as possible, distributed between all the workers.
For testing purposes I changed it to Task.Delay(10) which stops the issue from happening.
I'll leave it to you to find the appropriate fix.
Hey there. Thanks for reporting this. This definitely came up before and I decided to use Yield since it was not problematic in other call sites, and didn't hit my M1 Max MacBook's perf (in editor or builds IIRC) in the way you're describing. But I agree being on a thread must be impacting it since that's the main difference between this one and the other call sites.
What kind of machine did you use to get that result?
I'll probably just put in the Delay(10) for now as a stop gap and ruminate on whether there's a cleaner solution. I'll at least make this a configurable constant if I can't find a cleaner way.
OK two things
- I wasn't capturing FPS in my test project properly, which is why I didn't notice the perf issue earlier despite being there
- I ended up creating two releases for this, the second release, 2.0.2, actually moves send to the main thread, perf seems to be much better when idle now
I updated to 2.0.2 and can confirm the high CPU usage is no more!
Initially I was on my windows laptop for a while and I didn't notice this. Maybe it didn't happen? I Don't know.
Lately I switched to my desktop which is a fairly old I7-6700k 32gb ram and I noticed the problem because of the CPU fan spinning louder.